asp.net FileUpload1导入EXCL文件到数据库的问题
public DataSet ImportExcel(string fileName)
{
//判断是否安装EXCEL
Excel.Application xlApp = new Excel.ApplicationClass();
if (xlApp == null)
{
_ReturnStatus = -1;
_ReturnMessage = "无法创建Excel对象,可能您的计算机未安装Excel";
return null;
}
//判断文件是否被其他进程使用
Excel.Workbook workbook;
workbook = xlApp.Workbooks.Open(fileName, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);
//获得所有Sheet名称
int n = workbook.Worksheets.Count;
string[] SheetSet = new string[n];
System.Collections.ArrayList al = new System.Collections.ArrayList();
for (int i = 1; i <= n; i++)
{
SheetSet[i - 1] = ((Excel.Worksheet)workbook.Worksheets[i]).Name;
}
//释放Excel相关对象
workbook.Close(null, null, null);
xlApp.Quit();
if (workbook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
}
if (xlApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
GC.Collect();
//把EXCEL导入到DataSet
DataSet ds = new DataSet();
string connStr = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + fileName + ";Extended Properties=Excel 8.0";
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
OleDbDataAdapter da;
for (int i = 1; i <= n; i++)
{
string sql = "select * from [" + SheetSet[i - 1] + "$] ";
da = new OleDbDataAdapter(sql, conn);
da.Fill(ds, SheetSet[i - 1]);
da.Dispose();
}
conn.Close();
conn.Dispose();
}
return ds;
}
传进来的fileName 为excl文件名字 为什么报错 无法找到xls文件
调用的时候
DataSet ds = ex.ImportExcel(FileUpload1.PostedFile.FileName);
无法找到“11.xls”。请检查文件名的拼写,并检查文件位置是否正确。
如果您正试图从“文件”菜单上最近使用的文件列表中打开文件,请确认文件未被重命名、移动或删除。
用户代码未处理 System.Runtime.InteropServices.COMException
HelpLink="D:\\Program Files\\Microsoft Office\\OFFICE11\\2052\\xlmain11.chm"
Message="无法找到“11.xls”。请检查文件名的拼写,并检查文件位置是否正确。\n\n如果您正试图从“文件”菜单上最近使用的文件列表中打开文件,请确认文件未被重命名、移动或删除。"
Source="Microsoft Office Excel"
ErrorCode=-2146827284
StackTrace:
在 Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
在 NXKJT.ExcelIO.ImportExcel(String fileName) 位置 e:\Visual Studio 2008\Projects\NXKJT\NXKJT\ExcelIO.cs:行号 67
在 NXKJT.InputUsers.Button2_Click(Object sender, EventArgs e) 位置 e:\Visual Studio 2008\Projects\NXKJT\NXKJT\InputUsers.aspx.cs:行号 49
在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: