在上传excel时,它给出了错误,起初它可以正常工作,但今天却给出了错误,我已经在另一个项目中实现了保存功能,并且它工作正常,我尝试给出确切的名称作为工作表名称,如下所示:在文章中建议,但没有用。
HttpFileCollection uploads = HttpContext.Current.Request.Files;
for (int i = 0; i < uploads.Count; i++)
{
HttpPostedFile upload = uploads[i];
if (upload.ContentLength == 0)
continue;
string c = System.IO.Path.GetFileName(upload.FileName);
try
{
upload.SaveAs(Server.MapPath("~/Files\\") + c);
}
catch (Exception Exp)
{
throw (Exp);
}
}
if (fileCustSite.PostedFile != null)
{
HttpPostedFile attFile = fileCustSite.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
if (fileCustSite.PostedFile.ContentLength > 0)
{
string Extension = Path.GetExtension(fileCustSite.PostedFile.FileName);
string inFileName = Path.GetFileName(fileCustSite.PostedFile.FileName);
string pathDataSource = Server.MapPath("~/Files\\") + inFileName;
string conStr = "";
if (Extension == ".xls" || Extension == ".xlsx")
{
switch (Extension)
{
case ".xls": //Excel 1997-2003 Provider=Microsoft.Jet.OLEDB.4.0;
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;" + "Data Source='" + pathDataSource.ToString() + "';" + "Extended Properties=Excel 8.0;";
break;
case ".xlsx": //Excel 2007 Provider=Microsoft.ACE.OLEDB.12.0;
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;" + "Data Source='" + pathDataSource.ToString() + "';" + "Extended Properties=Excel 8.0;";
break;
default:
break;
}
try
{
OleDbConnection connExcel = new OleDbConnection(conStr.ToString());
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
connExcel.Open();
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + "POP_Upload_Template" + "$]", conStr.ToString());
da.Fill(ds);
gdvUpExcel.DataSource = ds;
gdvUpExcel.DataBind();
Session["Table1"] = ds.Tables[0];
if (gdvUpExcel.Columns.Count > 0)
{
}
connExcel.Close();
if (File.Exists(pathDataSource))
{
File.Delete(pathDataSource);
}
}