在我的应用程序中,我必须阅读OpenOffice电子表格,并在电子表格中的网格视图中显示电子表格中的数据,以下是我使用的代码,但它提供了一个例外,即外部表的格式不正确,我该如何解决
<div>
Import Excel File:
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:GridView ID="gvExcelFile" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
//Coneection String by default empty
string ConStr = "";
//Extantion of the file upload control saving into ext because
//there are two types of extation .xls and .xlsx of Excel
string ext = Path.GetExtension(FileUpload1.FileName).ToLower();
//getting the path of the file
string path = Server.MapPath("~/MyFolder/" + FileUpload1.FileName);
//saving the file inside the MyFolder of the server
FileUpload1.SaveAs(path);
Label1.Text = FileUpload1.FileName + "\'s Data showing into the GridView";
//checking that extantion is .xls or .xlsx
if (ext.Trim() == ".ods")
{
//connection string for that file which extantion is .xls
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (ext.Trim() == ".xlsx")
{
//connection string for that file which extantion is .xlsx
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
//making query
string query = "SELECT * FROM [Sheet1$]";
//Providing connection
OleDbConnection conn = new OleDbConnection(ConStr);
//checking that connection state is closed or not if closed the
//open the connection
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
//create command object
OleDbCommand cmd = new OleDbCommand(query, conn);
// create a data adapter and get the data into dataadapter
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
//fill the Excel data to data set
da.Fill(ds);
//set data source of the grid view
gvExcelFile.DataSource = ds.Tables[0];
//binding the gridview
gvExcelFile.DataBind();
//close the connection
conn.Close();
最佳答案
ActiveXObject
仅适用于Internet Explorer,如here所述。
对于其他浏览器,Silverlight具有AutomationFactory可能可以做到的。但是,由于涉及安全风险,所以我没有尝试过。
另一种方法是将ReadExcel()
函数放在以.js
结尾的文件中,然后在文件末尾添加行ReadExcel();
。将excelFile
设置为电子表格文件的路径。我双击运行它,然后它成功加载了OpenOffice文档。
编辑:
查看以下行是否引起相同的错误:
var objShell = new ActiveXObject("WScript.shell");
如果是这样,则问题与OpenOffice或您的代码无关。也许您需要授予Internet Explorer权限才能安全运行。请参见https://social.technet.microsoft.com/Forums/ie/en-US/8db7ec28-45ca-4859-b051-f0571a4da14e/error-automation-server-cant-create-object?forum=ieitpropriorver。
关于javascript - 读取OpenOffice电子表格时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38048626/