在ASP .NET中生成Excel工作表 [英] Generating excel sheet in asp .net
问题描述
我正在尝试在Excel工作表中生成报告..
我的代码在本地主机上运行正常,但是当我将其放在服务器上时却无法运行.代码没有显示任何错误,只是刷新页面.
我在网上搜索,它建议我更改Web配置文件中的某些信任级别,而我完全不知道这件事....
这是我正在使用的代码....
请有人帮我
I am trying to generate a report in excel sheet..
My code is working perfectly on local host but when i put it on server it is not working. The code is not showing me any error, it is just refreshing the page.
I searched on net , it suggested me to change some trust level in the web config file and i am completely unaware of this thing....
here is the code which i am using....
Kindly some one help me on this
private void createDataInExcel(DataSet ds)
{
Application oXL;
_Workbook oWB;
_Worksheet oSheet;
Range oRng;
string strCurrentDir = Server.MapPath(".") + "\\reports\\";
try
{
oXL = new Application();
oXL.Visible = true;
oWB = (_Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (_Worksheet)oWB.ActiveSheet;
int iRow = 2;
if (ds.Tables[0].Rows.Count > 0)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
oSheet.Cells[1, j + 1] = ds.Tables[0].Columns[j].ColumnName.ToString().ToUpper();
}
iRow++;
for (int rowNo = 0; rowNo < ds.Tables[0].Rows.Count; rowNo++)
{
for (int colNo = 0; colNo < ds.Tables[0].Columns.Count; colNo++)
{
oSheet.Cells[iRow, colNo + 1] = ds.Tables[0].Rows[rowNo][colNo].ToString();
}
iRow++;
}
}
oRng = oSheet.get_Range("A1", "IV1");
oRng.EntireColumn.AutoFit();
oXL.Visible = true;
oXL.UserControl = true;
string strFile = "report" + DateTime.Now.Ticks.ToString() + ".xls";
// oWB.SaveAs(strCurrentDir + strFile, XlFileFormat.xlWorkbookNormal, null, null, true, true, XlSaveAsAccessMode.xlShared, true, true, null, null, null);
//response.Clear();
//response.Charset = "";
////set the response mime type for excel
//response.ContentType = "application/vnd.ms-excel";
//// response.AddHeader("Content-Disposition", "attachment;filename=\"" + "reports/" + strFile + "\"");
//response.AddHeader("content-disposition", "attachment;filename=" + strFile);
//response.End();
// oWB.Close(null, null, null);
// oXL.Workbooks.Close();
// oXL.Quit();
Marshal.ReleaseComObject(oRng);
Marshal.ReleaseComObject(oXL);
Marshal.ReleaseComObject(oSheet);
Marshal.ReleaseComObject(oWB);
// // Response.Redirect("reports/" + strFile);
//// string strMachineName = Request.ServerVariables["SERVER_NAME"];
// // Response.Redirect("http://" + strMachineName + "/" + "reports/" + strFile);
// response.Clear();
// response.Charset = "";
// //set the response mime type for excel
// response.ContentType = "application/vnd.ms-excel";
// // response.AddHeader("Content-Disposition", "attachment;filename=\"" + "reports/" + strFile + "\"");
// response.AddHeader("content-disposition", "attachment;filename=" + strFile + ".xls");
// response.End();
}
catch (Exception theException)
{
// Response.Write(theException.Message);
response.Write("<script>alert("+ theException.ToString() +")</script>");
}
}
问候
索拉·斯里瓦斯塔瓦
高级软件开发人员
HDL软件解决方案
印度
[edit]已删除呼喊声-OriginalGriff [/edit]
Regards
Saurabh Srivastava
Senior Software Developer
HDL SOFTWARE SOLUTIONS
INDIA
[edit]SHOUTING removed - OriginalGriff[/edit]
推荐答案
请检查以下链接http://msdn.microsoft.com/zh-CN/library/wyts434y.aspx [
please check the following link http://msdn.microsoft.com/en-us/library/wyts434y.aspx[^]
这篇关于在ASP .NET中生成Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!