网站建设 | 建站知识 | 网站设计 | 房产财经 | 七彩商务 | 医疗保健 | 短信大全 | 动画欣赏 | 设计课堂 | 设计教程
建网站 | 站长学院 | 建站教程 | 网页教程 | 网站教程 | 推广教程 | 平面教程 | 动画教程 | 营销策划 | 广告设计
联系我们 | 服务宗旨 | 企业建站 | 网站策划 | 网站制作 | 网站宣传 | 宣传推广 | 财经频道 | 网站地图
服务中心 建立网站 建站方案 网页设计 网站建设 网站改版 网站推广 案例展示
七彩课堂
广告设计教程
程序开发教程
电脑维护教程
网页标准教程
平面设计教程
闪客动画教程
网站建设教程
网站推广教程
网页设计教程
网页制作教程
网站设计教程
dreamweaver
课堂通道
网页设计: 01 02 03 04
平面设计: 01 02 03 04
网站推广: 01 02 03 04
网站建设: 01 02 03 04
网页制作: 01 02 03 04
动画教程: 01 02 03 04
建站知识: 01 02 03 04
>>>> 查看更多
合作支持
英国留学 | 工艺品 | 商标注册 | 水疗设备 | 卓迈国际 | 打包带 | 杭州鲜花 | 搞笑短信 | 情人节鲜花 | Nike Shoes
七彩课堂[网页设计ASP.NET教程系列]
   
Create Dynamic ASP.NET Excel Workbooks
Create Dynamic ASP.NET Excel Workbooks In C#
By Peter A. Bromberg, Ph.D.
Printer - Friendly Version
There is also, in the downloadable solution, a utility class "SPGen" that handles running stored
Generating native Excel spreadsheets from your web server is not that difficult with ASP.NET. What can be difficult is making instances of Excel.exe go away so you don''t open up TaskMgr and see 123 instances of EXCEL.EXE still sitting in memory. I provide here a solution that has two methods, "CreateExcelWorkbook", which runs a stored proceduire that returns a DataReader and assembles a native Excel Workbook from it, saves it to the filesystem, and creates a "Download" link so the user can either load the report into Excel in their browser, or download the XLS file. The second method, GenerateCSVReport, does essentially the same thing but creates a CSV file that will, of course, also load into Excel. The CSV code correctly handles a common developer problem in that if you have a column that has leading zeroes, they are preserved.
procedures and returning DataReaders, and a RemoveFiles utility method that cleans up any XLS or CSV file older than the specified number of minutes. The key method presented below is the CreateExcelWorkbook method.
NOTE: You should be aware that you will probably need to run this page under an account that has administrative privileges as it needs write permissions to store the generated Excel or CSV files on the webserver''s file system. Probably the easiest way to handle this is to have the page in its own folder with its own web.config, and insert an <identity impersonate ="true" ... elment. You may also need to enable ACL permissions on the physical folder as well so that the identity the page runs under has write permissions. Finally, you''ll need to set a COM reference to the Excel 9.0 or Excel 10 Typelibrary and let VS.NET generate the Interop assemblies for you. I believe MS also has a link on their Office site where you can download the Office primary Interop Assemblies.

<identity impersonate="true" userName="adminuser" password="adminpass" />
Note especially the code block that does the "cleanup" of the Excel objects:
// Need all following code to clean up and extingush all references!!!
oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
oSheet=null;
oWB=null;
oXL = null;
GC.Collect(); // force final cleanup!
This is necessary because all those littlle objects "oSheet", "oWb" , ''oRng", etc. are all COM instances and we need to use the InteropServices ReleaseComObject method of the Marshal class to get rid of them in .NET.
private void CreateExcelWorkbook(string spName, SqlParameter[] parms)
{
string strCurrentDir = Server.MapPath(".") + "\\";
RemoveFiles(strCurrentDir); // utility method to clean up old files
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
try
{
GC.Collect();// clean up any other excel guys hangin'' around...
oXL = new Excel.Application();
oXL.Visible = false;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//get our Data
string strConnect = System.Configuration.ConfigurationSettings.AppSettings["connectString"];
SPGen sg = new SPGen(strConnect,spName,parms);
SqlDataReader myReader = sg.RunReader();
// Create Header and sheet...
int iRow =2;
for(int j=0;j<myReader.FieldCount;j++)
{
oSheet.Cells[1, j+1] = myReader.GetName(j).ToString();
}
// build the sheet contents
while (myReader.Read())
{
for(int k=0;k < myReader.FieldCount;k++)
{
oSheet.Cells[iRow,k+1]= myReader.GetValue(k).ToString();
}
iRow++;
}// end while
myReader.Close();
myReader=null;
//Format A1:Z1 as bold, vertical alignment = center.
oSheet.get_Range("A1", "Z1").Font.Bold = true;
oSheet.get_Range("A1", "Z1").VerticalAlignment =Excel.XlVAlign.xlVAlignCenter;
//AutoFit columns A:Z.
oRng = oSheet.get_Range("A1", "Z1");
oRng.EntireColumn.AutoFit();
oXL.Visible = false;
oXL.UserControl = false;
string strFile ="report" + System.DateTime.Now.Ticks.ToString() +".xls";
oWB.SaveAs( strCurrentDir + strFile,Excel.XlFileFormat.xlWorkbookNormal,
null,null,false,false,Excel.XlSaveAsAccessMode.xlShared,false,false,null,null,null);
// Need all following code to clean up and extingush all references!!!
oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
oSheet=null;
oWB=null;
oXL = null;
GC.Collect(); // force final cleanup!
string strMachineName = Request.ServerVariables["SERVER_NAME"];
errLabel.Text="<A href=http://" + strMachineName +"/ExcelGen/" +strFile + ">Download Report</a>";
}
catch( Exception theException )
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat( errorMessage, theException.Message );
errorMessage = String.Concat( errorMessage, " Line: " );
errorMessage = String.Concat( errorMessage, theException.Source );
errLabel.Text= errorMessage ;
}

}

七彩课堂推荐教程
网页设计|网站维护|七彩社区|祝福语|毛尖茶|招聘|留言|百科|游戏|搜索|短信|家园|热点|博客|鲜花|留学
传统节日|中华民俗|生日短信|问候短信|情人短信|新年短信|新春祝福|春节短信|流行动画|MTV动画
育儿知识|家庭医生|中医治疗|加盟合作|买卖求购|供应信息|网站赚钱|赚钱技巧|买房置业|投资理财
2005 -2008© Copyright QicaiSpace.com All rights receand. [七彩空间专业建网站做网站].
热线/MOB: 13396526989  电话/TEL: 0571 -- 63136250    E-mail: art01@163.com