我是想将资料导出EXCEL或是由EXCEL导入DGV
在打开文件以话框时出现此错误
出错代码:
try
{
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(); OpenFileDialog open = new OpenFileDialog();
open.Title = "打开";
open.Filter = ".xls|*.xls";
string filename = "";
if (open.ShowDialog() == DialogResult.OK)
{ filename = open.FileName; }
if (filename == "" || filename == null)
{ return; }
// string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filename.Trim() + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
con.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source = " + filename.Trim() + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
con.Open();
string sqlstr = "select * from [餐订库存$]";
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sqlstr, con);
DataTable dt = new DataTable();
da.Fill(dt);
cdgv.DataSource = dt;
con.Close(); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
此代码我以前用过的,没错误
今天拿来用就错,导出也一样!
在打开文件以话框时出现此错误
出错代码:
try
{
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(); OpenFileDialog open = new OpenFileDialog();
open.Title = "打开";
open.Filter = ".xls|*.xls";
string filename = "";
if (open.ShowDialog() == DialogResult.OK)
{ filename = open.FileName; }
if (filename == "" || filename == null)
{ return; }
// string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filename.Trim() + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
con.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source = " + filename.Trim() + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
con.Open();
string sqlstr = "select * from [餐订库存$]";
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sqlstr, con);
DataTable dt = new DataTable();
da.Fill(dt);
cdgv.DataSource = dt;
con.Close(); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
此代码我以前用过的,没错误
今天拿来用就错,导出也一样!
换成
Application.Run(new frmStockRestaurant());
你试试
如果真是这样,我也想知道原因?
我干脆不调试了,做好后放入框架中去执行,OK,没问题
奶奶的,搞不懂!