C# 上传Excel工作表时出现问题。工作表1不是有效的名称。
当上传excel时,它给出了错误,起初它的工作很好,但今天它给出了错误,我在另一个项目中实现了save,并且它工作正常,我尝试按照文章中的建议将确切的名称命名为sheet name,但没有任何效果C# 上传Excel工作表时出现问题。工作表1不是有效的名称。,c#,oledb,oledbexception,C#,Oledb,Oledbexception,当上传excel时,它给出了错误,起初它的工作很好,但今天它给出了错误,我在另一个项目中实现了save,并且它工作正常,我尝试按照文章中的建议将确切的名称命名为sheet name,但没有任何效果 HttpFileCollection uploads = HttpContext.Current.Request.Files; for (int i = 0; i < uploads.Count; i++) { HttpPostedFile
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);
}
}
HttpFileCollection uploads=HttpContext.Current.Request.Files;
对于(int i=0;i0)
{
如果(fileCustSite.PostedFile.ContentLength>0)
{
字符串扩展名=Path.GetExtension(fileCustSite.PostedFile.FileName);
字符串inFileName=Path.GetFileName(fileCustSite.PostedFile.FileName);
字符串pathDataSource=Server.MapPath(“~/Files\\”)+inFileName;
字符串conStr=“”;
if(扩展名==“.xls”| |扩展名==“.xlsx”)
{
交换机(分机)
{
case“.xls”://Excel1997-2003 Provider=Microsoft.Jet.OLEDB.4.0;
conStr=“Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;“+”数据源=“+”路径数据源.ToString()+”;“+”扩展属性=Excel 8.0;”;
打破
case“.xlsx”://Excel2007 Provider=Microsoft.ACE.OLEDB.12.0;
conStr=“Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;“+”数据源=“+”路径数据源.ToString()+”;“+”扩展属性=Excel 8.0;”;
打破
违约:
打破
}
尝试
{
OleDbConnection connExcel=新的OleDbConnection(conStr.ToString());
OleDbCommand cmdExcel=新的OleDbCommand();
OleDbDataAdapter oda=新的OleDbDataAdapter();
Open();
数据集ds=新数据集();
OleDbDataAdapter da=新的OleDbDataAdapter(“从[“+”弹出窗口\上传\模板“+”$]”中选择*,conStr.ToString());
da.填充(ds);
gdvUpExcel.DataSource=ds;
gdvUpExcel.DataBind();
会话[“表1”]=ds.表[0];
如果(gdvUpExcel.Columns.Count>0)
{
}
connExcel.Close();
if(File.Exists(pathDataSource))
{
File.Delete(pathDataSource);
}
}
您在代码中的何处遇到错误?此代码的确切用途是什么?什么是
fileCustSite
?顺便说一句,您代码中的excel 1997-2003提供程序字符串与2007中的相同。并且调用.ToString()没有多大用处在该字符串上…@Nyerguds fileCustSite是我的fileupload控件的id。我在此行中遇到错误:-da.Fill(ds);