110,545
社区成员
发帖
与我相关
我的任务
分享
#region 读取Excel 文件
//连接字符串 //03版 Microsoft.Jet.OLEDB.4.0
const string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Extended Properties='{1};HDR=Yes;IMEX=2'";
OleDbConnection conn = new OleDbConnection(string.Format(strConn, ofd.FileName, "Excel 8.0"));
try
{
conn.Open();
}
catch
{
conn = new OleDbConnection(string.Format(strConn, ofd.FileName, "Excel 12.0"));
try
{
conn.Open();
}
catch (Exception ee)
{
MessageBox.Show("连接Excel文件错误:" + ee.Message);
return;
}
}
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
DataSet ds = new DataSet();
foreach (DataRow dr in dt.Rows)
{
string sql = " SELECT 流水号,进销,日期,单据编号,摘要,客户内码,客户名称,行号,入库数量,出库数量,单价,金额 "
+ " ,批号,批次,生产日期,有效期,商品内码,商品编号,商品名称,商品规格,生产厂家,供方内码 "
+ " ,供货单位,采购员,'0' AS 过滤 from [" + dr["TABLE_NAME"].ToString() + "]";
try
{
OleDbDataAdapter aper = new OleDbDataAdapter(sql, conn);
aper.Fill(ds, dr["TABLE_NAME"].ToString());
if (ds.Tables[dr["TABLE_NAME"].ToString()].Columns.Count < 2)
{
continue;
}
//dt = myset.Tables[dr["TABLE_NAME"].ToString()];
if (ds == null || ds.Tables[0].Rows.Count < 1)
{
MessageBox.Show("连接Excel文件错误:");
return;
}
}
catch {
MessageBox.Show("读取Excel表格数据错误,请重新打开!","运行提示");
return;
}
}
conn.Close();
conn.Dispose();
#endregion