Read Excel file content is displayed on the Web, read excel content web
Click Event code. cs
protected void Button1_Click(object sender, EventArgs e) { string strPath = "d:/test.xls"; string mystring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = '" + strPath + "';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; //"Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + strPath + "';Extended Properties=Excel 8.0"; OleDbConnection cnnxls = new OleDbConnection(mystring); OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls); DataSet myDs = new DataSet(); myDa.Fill(myDs); DataGrid1.DataSource = myDs.Tables[0]; DataGrid1.DataBind(); }
If you use the classic"Provider = Microsoft. Jet. OLEDB.4.0; Data Source = '" + strPath + "'; Extended Properties = Excel 8.0" an error is returned: The External table is not in the expected format.
This is because: Microsoft. jet. OLEDB.4.0 is the Microsoft Jet Engine, which is applicable to version 2003 (I did not install it Before Version 2003, so I do not know which version can be adapted to). In version 2007, microsoft modified its Access and Excel file formats and renamed them. accdb (Access 2007 database file) and. xlsx (Excel 2007 file) is not supported by the Microsoft Jet Engine, but Microsoft quickly proposed Microsoft Office 2007 Desktop Drivers: Data Connectivity Components.
The solution is to change the data provider in the connection string to Microsoft. ACE. OLEDB.12.0.
Excel show unreadable content
Click the office icon (File) in the upper left corner -- options -- trust center settings -- protected view --
Enable protected views for files from Interet.
Enable protected views for files located in a location that may be insecure.
Enable protected views for outlook attachments.
You need to remove the preceding check box.
Try it
Js reads the content of a cell in excel, and then displays it on the webpage.
Save the Excel file as Html.
Response. ContentType tells the browser what format the content transmitted by the server is
The default value is text/html.
You need to set browser permissions on all clients, such as how to modify IE:
Enter the Custom Level of Internet property = Security =, and change all options under ActiveX Control and plug-in to enable
Because js is a client, the server cannot control
I usually use the server to generate an Excel file in html format, and then set
Response. ContentType = "application/vnd. ms-excel ";
Response. AppendHeader ("Content-Disposition", "inline?filename=abc.xls ");
In this way, you do not need to modify the IE settings of the client.