我开发了一个Web应用程序,从Visual Studio执行时运行良好。
应用程序的主题是获取excel文件中的内容并显示它。
我们的应用程序中有一个文件上传控件,用于获取excel文件。
从VS执行时它就像魅力一样,我可以看到所需的结果,但是通过IIS浏览时会出错。
代码是在Framework 4.0中开发的
excel文件没有上传,并在读取文件时抛出错误。
这是我的代码..
您能通过IIS浏览时检测到出现了什么问题吗?
DataSet dsRates = new Schemas.Rates();
DataTable dtExcel = new DataTable();
dtDBTable = dsRates.Tables[0];
DataTable dtColumnNameNotFound = new DataTable();
FileStream stream;
string changed = string.Empty;
string FilePath = string.Empty;
try
{
if (ValidateUserInputs())
{
DataSet dsExcel = new DataSet();
OleDbConnection con = new OleDbConnection();
try
{
if (fupExtract.HasFile == true)
{
FilePath = Server.MapPath("~/Temp/" + fupExtract.PostedFile.FileName);
fupExtract.SaveAs(FilePath);
}
else
{
fupExtract = ((FileUpload)Session["FileUploadCtrl"]);
FilePath = Server.MapPath("~/Temp/" + fupExtract.PostedFile.FileName);
fupExtract.SaveAs(FilePath);
}
//Read the Excel Data in to Datatable
string _ConnectionString = string.Empty;
string _Extension = Path.GetExtension(FilePath);
if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
{
_ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + FilePath + ";Extended Properties=" + (char)34 + "Excel 8.0;HDR=NO;IMEX=1;" + (char)34;
}
//Use ACE OleDb
else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
{
_ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=" + (char)34 + "Excel 8.0;HDR=NO;IMEX=1;" + (char)34;
}
else
{
lblMessage.Text = fupExtract.FileName + "is not a supported format, only '.xls|.xlsx' files are supported";
return;
}
int i = 0;
con = new OleDbConnection(_ConnectionString);
con.ResetState();
con.Open();
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach (DataRow Sheet in dt.Rows)
{
OleDbDataAdapter daExcel = new OleDbDataAdapter("SELECT * FROM [" + Sheet["TABLE_NAME"].ToString().Trim() + "]", con);
DataTable dtData = new DataTable();
daExcel.Fill(dtData);
var filteredRows = dtData.Rows.Cast<DataRow>().Where(row => row.ItemArray.Any(field => !(field is System.DBNull)));
if (filteredRows.Count() > 0)
{
dsExcel.Tables.Add(dtData);
}
i++;
}
con.Close();
}
catch (Exception ex)
{
lblMessage.Text = "Error occured while reading the file";
con.Close();
}
finally
{
if (File.Exists(FilePath))
{
File.Delete(FilePath);
}
}
请帮忙
谢谢,
答案 0 :(得分:0)
您可以使用Interop来阅读您的Excel文档
object misValue = System.Reflection.Missing.Value;
var xlApp = new Excel.ApplicationClass();
var xlWorkBook = xlApp.Workbooks.Open("yourFile.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Console.Write(xlWorkSheet.get_Range("A1","A1").Value2.ToString());
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
答案 1 :(得分:0)
我已经得到了我的问题的答案。
我没有安装Office 64位和Microsoft.ACE.OLEDB.12.0。我正在使用的是32位。