我必须阅读现有的Excel工作表值并使用
生成文本格式报告Microsoft.Office.Interop.Excel命名空间
当我在Windows服务器8 r2上的IIS 7.5上部署ASP.NET Web应用程序时我无法在IIS托管之后访问该事件但是当我运行本地计算机时它将正常工作...请给我解决方案这个问题?关注我的aspx.cs代码。
using System.Data;
using System.Reflection;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Excel;
public void GetTextValue()
{
string sUploadFilePath = string.Empty;
sUploadFilePath = Server.MapPath(".") + @"\Report\GAD_Model_Template_keivsan1_D2038_00.xls";
DataSet ds = new DataSet();
//Office Application Object Declaretion
Microsoft.Office.Interop.Excel.Application oXL = null;
//Office workbook object Declaretion
Microsoft.Office.Interop.Excel.Workbook oWB = null;
//Office Worksheet object Declaretion
Microsoft.Office.Interop.Excel.Worksheet oSheet = null;
//Office Range object Declaretion
Microsoft.Office.Interop.Excel.Range oRng = null;
try
{
//Create an Application object
oXL = new Microsoft.Office.Interop.Excel.Application();
//Getting a WorkBook object
oWB = oXL.Workbooks.Open(sUploadFilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//Getting a WorkSheet object
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets["OUTPUT"];
//Datatable with dtExcel Name
System.Data.DataTable dt = new System.Data.DataTable("dtExcel");
ds.Tables.Add(dt);
DataRow dr;
StringBuilder sb = new StringBuilder();
//Total Column Count
int jValue = oSheet.UsedRange.Cells.Columns.Count;
//Total Row count
int iValue = oSheet.UsedRange.Cells.Rows.Count;
for (int j = 1; j <= jValue; j++)
{
dt.Columns.Add("column" + j, System.Type.GetType("System.String"));
}
//Getting Data in Cell
for (int i = 1; i <= iValue; i++)
{
dr = ds.Tables["dtExcel"].NewRow();
for (int j = 1; j <= jValue; j++)
{
oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];
string strValue = oRng.Text.ToString();
dr["column" + j] = strValue;
}
ds.Tables["dtExcel"].Rows.Add(dr);
}
//Release the Excel objects
oWB.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
oXL.Workbooks.Close();
oXL.Quit();
oXL = null;
oWB = null;
oSheet = null;
oRng = null;
GC.GetTotalMemory(false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.GetTotalMemory(true);
//Release the Excel objects
StringBuilder commaDelimitedText = new StringBuilder();
commaDelimitedText.AppendLine("GAD Inputs");
foreach (DataRow row in ds.Tables["dtExcel"].Rows)
{
string value = string.Format("{0}", row[0]); // how you format is up to you (spaces, tabs, delimiter, etc)
commaDelimitedText.AppendLine(value);
}
File.WriteAllText("" + Server.MapPath(".") + @"\Report\" + "GADoutput" + ".txt" + "", commaDelimitedText.ToString());
string txtfilepath = Server.MapPath(".") + @"\Report\" + "GADoutput" + ".txt";
Session["filepath"] = txtfilepath;
ScriptManager.RegisterStartupScript(this, typeof(string), "SITE", "alert('Text File Write successfully!');", true);
}
catch (Exception ex)
{ }
//return ds;
}
感谢。