【发布时间】:2018-11-02 12:40:15
【问题描述】:
当我输入标题时,下面的代码给了我以下错误
我在这里做错了什么?
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string huru = openFileDialog1.FileName;
this.textBox1.Text = huru;
string pathConn;
OleDbConnection conn;
DataTable spreadSheetData;
string sheetName = "";
OleDbCommand onlineConnection;
OleDbDataAdapter myDataAdapter;
DataTable dt = new DataTable();
if (huru.Substring(huru.Length - 3) == "lsx")
{
pathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + huru
+ ";Extended Properties = \"Excel 12.0 Xml;HDR=YES\"; ";
}
else
{
pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + huru
+ ";Extended Properties=\"Excel 8.0;HDR=yes;\";";
}
conn = new OleDbConnection(pathConn);
spreadSheetData = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach (DataRow dr in spreadSheetData.Rows)
{
sheetName = dr["TABLE_NAME"].ToString();
sheetName = sheetName.Replace("'", "");
if (sheetName.EndsWith("$"))
{
onlineConnection = new OleDbCommand("SELECT * FROM [" + sheetName + "]", conn);
myDataAdapter = new OleDbDataAdapter(onlineConnection);
dt = new DataTable();
dt.TableName = sheetName;
myDataAdapter.Fill(dt);
ds.Tables.Add(dt);
}
}
}
spreadSheetData 开始下降为空
我的代码参考Excel to DataGridView JohnG 回答的第一个问题
还有这个视频https://www.youtube.com/watch?v=CfNMPDJVjPI
感谢您的帮助!
【问题讨论】:
-
你应该描述你想用你的代码做什么,将错误作为文本插入,而不是作为图像链接(有很多原因),并向我们展示你自己的一些调试工作
标签: c# excel import datagridview