电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> .NET
在Asp.net用C#建立动态Excel(外文翻译)-.NET教程,Asp.Net开发
作者:网友供稿 点击:12
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
 

在asp.net中建立本地的excel表,并由服务器向外传播是容易实现的,而删除掉嵌入的excel.exe进程是困难的。所以 你不要打开任务管理器 ,看excel.exe进程相关的东西是否还在内存里面。我在这里提供一个解决方案 ,里面提供了两个方法 :
"createexcelworkbook"(说明 建立excel工作簿) 这个方法 运行一个存储过程 ,返回一个datareader 并根据datareader 来生成一个excel工作簿 ,并保存到文件系统中,创建一个“download”连接,这样 用户就可以将excel表导入到浏览器中也可以直接下载到机器上。

第二个方法:generatecsvreport 本质上是做同样的一件事情,仅仅是保存的文件的csv格式 。仍然 导入到excel中,csv代码能解决一个开发中的普片的问题:你有一列 里面倒入了多个零,csv代码能保证零不变空 。(说明: 就是在excel表中多个零的值 不能保存的问题)

 

   在可以下载的解决方案中,包含一个有效的类 ” spgen” 能运行存储过程并返回datareader ,一个移除文件的方法 能删除早先于一个特定的时间值。下面出现的主要的方法就是createexcelworkbook

 

   注意:你必须知道 在运行这个页面的时候,你可能需要能在websever 服务器的文件系统中写 excel,csv文件的管理员的权限。处理这个问题的最简单的方法就是运行这个页面在自己的文件夹里面并包括自己的配置文件。并在配置文件中添加下面的元素<identity impersonate ="true" ... 。你仍然需要物理文件夹的访问控制列表(acl)的写的权限,只有这样运行的页面的身份有写的权限,最后,你需要设置一个com连接到excel 9.0 or excel 10 类型库 ,vs.net 将为你生成一个装配件。我相信 微软在他们office网站上有一个连接,可以下载到微软的初始的装配件 。(可能不准,我的理解是面向.net的装配件)

 

<identity impersonate="true" username="adminuser" password="adminpass" />

 

特别注意 下面的代码块的作用是清除excel的对象。

 

// 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!

 

 

这是必须的 ,因为osheet", "owb" , orng", 等等 对象也是com的实例,我们需要

marshal类的releasecomobject的方法把它们从.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 ;
}
}

 

 

 

 

   下面是原文章

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 dont 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 webservers 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, youll 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 ;
}
}
                   
-翻译匆忙 ,有误请谅解 ,欢迎指点,探讨 ---小徐

http://macroxu-1982.cnblogs.com/archive/2006/05/31/413879.html


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·经典收藏之 - C++内存管理详解-.NET教程,C#语言
·Master Page 初探-.NET教程,评论及其它
·GDI+编程10个基本技巧-.NET教程,评论及其它
·VB.NET中让Textbox只能输入数字(二)-.NET教程,VB.Net语言
·stl应用小问题-.NET教程,评论及其它
·WIN32中颜色值(COLORREF)与.NET中颜色值(Color)的转换-ASP教程,系统相关
·打造自己的专业图像工具-Visual C++ 2005图像编程系列【三】-.NET教程,C#语言
·.Net中常见问题及解决方法归类-.NET教程,.NET Framework
·Lex和Yacc从入门到精通(3)--一个极其简单的lex和yacc程序-.NET教程,评论及其它
·VB下几个非常有用的函数-.NET教程,VB.Net语言

最新文章
·VC#初学入门:第一个Windows程序
·ASP.NET 2.0-选用DataSet或DataReader
·用.net 处理xmlHttp发送异步请求
·asp.net创建文件夹的IO类的问题
·asp.net 2.0 中加密web.config 文件中的配置节
·关于ASP.NET调用JavaScript的实现
·如何实现ASP.NET网站个性化
·Acegi安全系统的配置-.NET教程,评论及其它
·Spring安全系统:Acegi Security Acegi简介-.NET教程,评论及其它
·Biztalk 开发之 架构和实例的验证-.NET教程,评论及其它


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号