62,074
社区成员
发帖
与我相关
我的任务
分享
// 读取Excel
private DataTable GetExcelTable(string uploadPath)
{
DataSet ds = new DataSet();
string Xls_ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + uploadPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";//HDR为yes 则第一数据行为列名,为no 则自动为列加列名F1 F2 F3
OleDbConnection Conn = new OleDbConnection(Xls_ConnStr);
try
{
Conn.Open();
string sql_str = "select * from [Sheet1$]";
OleDbDataAdapter da = new OleDbDataAdapter(sql_str, Conn);
da.Fill(ds, "excel_data");
Conn.Close();
}
catch
{
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
return null;
}
finally
{
Conn.Dispose();
}
if (ds == null)
{
return null;
}
if (ds.Tables.Count < 1)
{
return null;
}
return ds.Tables[0];
}
try
{
ProductService.productAllDelete();
string mystring = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + Server.MapPath("upexcel/uplexcl.xls") + "; Extended Properties=\"Excel 8.0; HDR=No; IMEX=1;\"";
OleDbConnection cnnxls = new OleDbConnection(mystring);
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [0.3-FJ-VS-VVS$]", cnnxls);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
DataTable table = myDs.Tables[0];
foreach (DataRow row in table.Rows)
{
if (row[3].ToString() == "形状")
{
continue;
}
if (row[3].ToString() == "")
{
break;
}
Product pd = new Product();
pd.Product_bianhao1 = row[0].ToString();
pd.Product_didian = row[1].ToString();
pd.Product_bianhao2 = row[2].ToString();
pd.Product_shape = row[3].ToString();
double sumMoney = double.Parse(row[4].ToString());
string dos = sumMoney.ToString("F3");
pd.Product_Weight = dos;
pd.Product_color = row[5].ToString();
pd.Product_Clarity = row[6].ToString();
pd.Product_Cut = row[7].ToString();
pd.Product_Polishing = row[8].ToString();
pd.Product_duic = row[9].ToString();
pd.Product_yguang = row[10].ToString();
pd.Product_zhij = zhij(row[11].ToString());
pd.Product_zshu = row[12].ToString();
decimal d = decimal.Parse(row[14].ToString());
decimal b = decimal.Round(d, 0);
pd.Product_rmbk = (double)b;
decimal n1 = decimal.Parse(row[15].ToString());
decimal n2 = decimal.Round(n1, 0);
pd.Product_rmbke = (double)n2;
pd.Product_bding = 0;
pd.Product_yzhk = double.Parse(row[16].ToString());
decimal a1 = decimal.Parse(row[17].ToString());
decimal a2 = decimal.Round(a1, 0);
pd.Product_guoji = (double)a2;
pd.Product_tuid = double.Parse(row[18].ToString());
decimal b1 = decimal.Parse(row[19].ToString());
decimal b2 = decimal.Round(b1, 0);
pd.Product_rmbk1 = (double)b2;
decimal c1 = decimal.Parse(row[20].ToString());
decimal c2 = decimal.Round(c1, 0);
pd.Product_rmbke1 = (double)c2;
pd.Product_huoc = row[21].ToString();
pd.Product_guojimj = double.Parse(row[22].ToString());
ProductService.productAdd(pd);
}
}
catch (Exception ex)
{
// return ex.ToString();
return "导入Excel失败 请确保您上传的excel的编码格式是正确的!";
}
return "导入Excel成功!";