外部表格不是预期的格式c#(External table is not in the expected format c#)

编程入门 行业动态 更新时间:2024-10-27 22:27:58
外部表格不是预期的格式c#(External table is not in the expected format c#)

谷歌搜索时我看到了更多的答案,但无法解决我的问题。 这是我编写的将excel表插入sql server表的代码。

string strConnection = ConfigurationManager.ConnectionStrings[0].ToString(); string path = @"D:/Projects/sample.xlsx"; string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";"; OleDbConnection excelConnection = new OleDbConnection(excelConnectionString); //Create OleDbCommand to fetch data from Excel OleDbCommand cmd = new OleDbCommand("Select [Name], [City], [Address], [Designation] from [Sheet1$]", excelConnection); excelConnection.Open(); OleDbDataReader dReader; dReader = cmd.ExecuteReader(); SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection); //Give your Destination table name sqlBulk.DestinationTableName = "temp1"; sqlBulk.WriteToServer(dReader); excelConnection.Close();

执行“excelConnection.Open();”行时。 我收到一个错误“外部表格不是预期的格式。”

而“sample.xlsx”实际上是一个ods格式文件。 我将其格式更改为xlsx。

这段代码会出错。 请建议我。

I have seen more answers while googling it, but couldn't solved my problem. This is the code I have written to insert excel sheet into sql server table.

string strConnection = ConfigurationManager.ConnectionStrings[0].ToString(); string path = @"D:/Projects/sample.xlsx"; string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";"; OleDbConnection excelConnection = new OleDbConnection(excelConnectionString); //Create OleDbCommand to fetch data from Excel OleDbCommand cmd = new OleDbCommand("Select [Name], [City], [Address], [Designation] from [Sheet1$]", excelConnection); excelConnection.Open(); OleDbDataReader dReader; dReader = cmd.ExecuteReader(); SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection); //Give your Destination table name sqlBulk.DestinationTableName = "temp1"; sqlBulk.WriteToServer(dReader); excelConnection.Close();

While executing the line "excelConnection.Open();". I am getting an error "External table is not in the expected format."

And "sample.xlsx" is actually an ods format file. I changed its format as xlsx.

What would be wrong in this code. Please suggest me.

最满意答案

根据评论中的对话,您必须将ODS文件转换为XLS或XLSX格式,而不仅仅是重命名它。 您可以使用Excel或Online tools转换ODS文件。

As per the conversation in the comments, you have to convert the ODS file into XLS or XLSX format not just renaming it. You can use Excel or Online tools to convert ODS files.

更多推荐

本文发布于:2023-08-03 10:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1386905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表格   格式   External   format   expected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!