在做excel导入功能时,已经将文件上传至服务器上,但是读取的时候时常报‘未指定的错误’,按照网上查找的解决方法试了很多种,就是不管用,代码如下:
string sPath = "C:\\Temp\\" + this.FUPatch.FileName.Replace(".xls", "") + DateTime.Now.ToString().Replace(":","").Replace(" ","").Replace("-","") + ".xls";
this.FUPatch.SaveAs(sPath);
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + sPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
DataTable dtQualityLevel = new DataTable("default");
OleDbConnection connExecel = new OleDbConnection(strcon);
try
{
string strsql = "select 申请单位,项目名称,WBS订单号,合同号,付款方式,币种,合同金额,变更金额,合同总价,本次付款金额,累计付款金额,资金计划序号,申请人,供应商编码,收款单位,开户银行,银行账户,开户银行地址,付款内容,付款条件,付款日期,附件名称,费用科目,预算号,合同类别,年度预算金额,年度预算执行比例,合同签订日期,要求到货日期,实际到货日期,备注 from [Sheet1$] where [本次付款金额] IS NOT NULL";
OleDbDataAdapter dpQualityLevel = new OleDbDataAdapter(strsql, connExecel);
connExecel.Open();
dpQualityLevel.Fill(dtQualityLevel);
this.GVList.DataSource = dtQualityLevel;
this.GVList.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.StackTrace);
connExecel.Close();
}
finally {
if (connExecel.State == ConnectionState.Open)
{
connExecel.Close();
}
}