excel导入问题!~
客户发来的EXCEL版本不能确定连接字符川应该怎么写呢?
string ConnString = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + source + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"";
这样写肯定是不行了,要是发来 EXCEL2。1 就报外部文件格式不正确了
--------------------编程问答-------------------- 加Try Catch 要是第一个连接字符串不行,再试第二个~~~ --------------------编程问答-------------------- 不要用异常控制程序流程 --------------------编程问答-------------------- EXCEL2。1 应该用 多少版本的 3.0 5.0都不行啊 --------------------编程问答-------------------- private string Getpath()
{
string str_name = File_Upload.FileName;
string str_Type = str_name.Substring(str_name.LastIndexOf(".") + 1);
string str_ipath = Server.MapPath("b2b_Excel") + "\\" + str_name;
string str_fpath = "b2b_Excel" + "\\" + str_name;
if (str_Type == "xls")
{
try
{
File_Upload.SaveAs(str_ipath);
return str_ipath;
}
catch (Exception ex)
{
//throw new Exception (ex.Message);
System.Windows.Forms.MessageBox.Show("文件路徑不存在,請重新選擇!");
}
}
else
{
return "";
}
return str_ipath;
}
private DataSet CreateDataSource()
{
DataSet myds = new DataSet();
try
{
string strCon;
//C:\\Inetpub\\wwwroot\\excel.xls;
//strCon = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"excel.xls") + "; Extended Properties=Excel 7.0;HDR=Yes;IMEX=1;";
strCon = " Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Getpath() + "; Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\"";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
myda.Fill(myds, "[Sheet1$]");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("excel文檔格式不對,請重新上傳一個");
}
return myds;
}
private void PrinSqlForExcel()
{
DataSet ds = (DataSet)CreateDataSource();
int count = ds.Tables["[Sheet1$]"].Rows.Count;
try
{
for (int i = 0; i < count; i++)
{
//el009, el016;
Session["est_dd"] = "";
Session["itm"] = "";
string el001, el002, el003, el004, el005, el006, el007;
el001 = ds.Tables["[Sheet1$]"].Rows[i]["受訂/計劃單號"].ToString();
el002 = ds.Tables["[Sheet1$]"].Rows[i]["採購單號"].ToString();
el003 = ds.Tables["[Sheet1$]"].Rows[i]["料號"].ToString();
el004 = ds.Tables["[Sheet1$]"].Rows[i]["品名"].ToString();
el005 = ds.Tables["[Sheet1$]"].Rows[i]["規格"].ToString();
el006 = ds.Tables["[Sheet1$]"].Rows[i]["採購數量"].ToString();
el007 = ds.Tables["[Sheet1$]"].Rows[i]["新交貨日"].ToString();
//讀出項次和原交期lbl_El006
//QTY='" + el006.Trim() + "' or
string str_Seltf_pos = "select itm,est_dd from tf_pos where os_id='PO' and os_no='" + el002.Trim() + "' and Prd_no='" + el003 + "'";
SqlConnection sqlcon = new SqlConnection(SqlHaper.SqlConString);
SqlDataReader dr = SqlHaper.Sqldr(str_Seltf_pos, sqlcon);
if (dr.Read())
{
Session["est_dd"] = dr["est_dd"].ToString();
Session["itm"]= dr["itm"].ToString();
}
sqlcon.Close();
string excelsql = "insert into K_Excel(el001, el002, el003, el004, el005, el006, el007, el009, el010,el012,el013,el014,el016) values ('" + el001 + "','" + el002 + "','" + el003 + "','" + el004 + "','" + el005 + "','" + el006 + "','" + el007 + "','" + Session["est_dd"].ToString() + "','0','" + this.dp_Gony.SelectedValue.ToString() + "','" + Session["User_Id"].ToString() + "','Del','" + Session["itm"].ToString() + "') ";
SqlHaper.Sqlcmd(excelsql);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("excel文件標頭對不上,請確認標頭後再重新載入!");
}
} --------------------编程问答-------------------- 楼上的,你这写法 EXCEL2。1 能行? --------------------编程问答-------------------- string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FileName + ";" + @"Extended Properties=""Excel 8.0;HDR=Yes;"""; --------------------编程问答-------------------- 大大门
string ConnString = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + source + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"";
这样的连接 是过不了 EXCEL2.1的呀 版本 太低 --------------------编程问答-------------------- ....帮顶, --------------------编程问答-------------------- 自己顶一下 --------------------编程问答-------------------- 自己顶一下
补充:.NET技术 , ASP.NET