ASP.net code connecting Excel
- 2020-05-10 17:56:19
- OfStack
First, add the namespace
asp.net implementation code that reads and displays excel data
Create dynamic Excel with C# in Asp.net
asp.net operates on the implementation code for excel
using System.Data.OleDb;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
strfile = Request.QueryString["filename"];// File path from other pages
Excel(strfile);
}
}
private void Excel(string filepath)
{
try
{
Dataset ds = new DataSet();
string ConnStr = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + filepath +
";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";// The connection Excel The string
string query = "SELECT * FROM [student$]";//Excel Table name in
OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(ConnStr));
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
oleAdapter.Fill(ds, "[student$]");
rowcount = ds.Tables[0].Rows.Count;
gridview1.DataSource = ds;
gridview1.DataBind();
lblmes.Text = " The upload was successful, the data is as follows, please confirm: ";//lblmes for label , display the prompt message
}
catch (OleDbException)
{
string filename = filepath.Substring(filepath.LastIndexOf('/') + 1);
lblmes.Text = " Error! Please confirm whether the uploaded file is correct! The file currently uploaded is: " + filename;
lbtnSure.Visible = false;
}
catch(Exception ee)
{
lblmes.Text = ee.Message;
}
}
asp.net implementation code that reads and displays excel data
Create dynamic Excel with C# in Asp.net
asp.net operates on the implementation code for excel