谁能指出一下代码的错误
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Execl files (*.xls)|*.xls";
openFileDialog.ShowDialog();
if (openFileDialog.FileName.IndexOf(".") < 0)
{
return;
}
string path = openFileDialog.FileName;
string fileName = path.Substring(path.LastIndexOf("\\") + 1);
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
string strExcel = "select * from [Sheet1$]";
try
{
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn);
adapter.Fill(ds);
dgvCustomer.DataSource = ds;
this.dgvCustomer.DataMember = ds.Tables[0].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
conn.Close();
}
最后总是出错
说外部表不是预期的格式
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Execl files (*.xls)|*.xls";
openFileDialog.ShowDialog();
if (openFileDialog.FileName.IndexOf(".") < 0)
{
return;
}
string path = openFileDialog.FileName;
string fileName = path.Substring(path.LastIndexOf("\\") + 1);
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
string strExcel = "select * from [Sheet1$]";
try
{
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn);
adapter.Fill(ds);
dgvCustomer.DataSource = ds;
this.dgvCustomer.DataMember = ds.Tables[0].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
conn.Close();
}
最后总是出错
说外部表不是预期的格式
解决方案 »
- c#通过在textbox中输入一个与datagridview列值相等内容,在datagridview选中该行数据显示出来?
- 请问这个类如何使用
- 求格式化数据的正则表达式
- 如何编程实现Word的字数统计功能呢?
- 随即加载.txt里面的内容(最后分求,天啊搞不定的话要炒鱿鱼了)
- 一个可能要遇到的问题 .NET
- dataGridView中显示的数据要怎么进行增删改
- 为什么我写的ListViewItem lvi = new ListViewItem(); 必须要有参数才行?
- 关于主进程控制子进程
- 没有分了,女朋友在广州,我要去广州谋生了,请广州做c#/.net的朋友报报广州工资水平,好让我开价
- 如何实现选择日期
- union union all 有什么区别?
Excel要符合二维表的形式
Excel第一列应为列名