gpt4 book ai didi

c# - 在 C# 中读取 excel (.xlsx) 文件

转载 作者:行者123 更新时间:2023-11-30 12:11:17 24 4
gpt4 key购买 nike

我正在像这样使用 C# 读取 .xlsx 文件

string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName +
";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1\";";

var output = new DataSet();

using (var conn = new OleDbConnection(strConn))
{
conn.Open();

var dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

foreach (DataRow row in dt.Rows)
{
string sheet = row["TABLE_NAME"].ToString();

var cmd = new OleDbCommand("SELECT * FROM [+"+sheet+"+]", conn);
cmd.CommandType = CommandType.Text;

OleDbDataAdapter xlAdapter = new OleDbDataAdapter(cmd);

xlAdapter.Fill(output,"School");
}
}

但是我在 xlAdapter.Fill(output,"School") 处遇到错误;错误是

The Microsoft Office Access database engine could not find the object '+_xlnm.Print_Area+'.  Make sure the object exists and that you spell its name and the path name correctly.

我无法弄清楚代码中发生了什么错误。

最佳答案

我相信您的工作表名为 _xlnm.Print_Area。尝试更改此行

var cmd = new OleDbCommand("SELECT * FROM [+"+sheet+"+]", conn);

var cmd = new OleDbCommand("SELECT * FROM ["+sheet+"]", conn);

关于c# - 在 C# 中读取 excel (.xlsx) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15780463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com