惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
Help Net Security
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LINUX DO - 最新话题
PCI Perspectives
PCI Perspectives
博客园 - 三生石上(FineUI控件)
V2EX - 技术
V2EX - 技术
Spread Privacy
Spread Privacy
T
Tor Project blog
量子位
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
小众软件
小众软件
博客园 - 叶小钗
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
Y
Y Combinator Blog
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Tenable Blog
Scott Helme
Scott Helme
G
GRAHAM CLULEY
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
博客园 - 司徒正美
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Last Watchdog
The Last Watchdog
L
LangChain Blog
C
Check Point Blog
Google Online Security Blog
Google Online Security Blog
V
Visual Studio Blog
Latest news
Latest news

博客园 - 蒜头

Mailbox unavailable. The server response was: 5.7.1 Unable to relay for (email address). 再谈Images到xps,pdf的转换 SQL Server 2005中xml类型和函数的简单应用 部分Office 2007文件格式转换为xps和pdf代码整理 Image.FromFile gives "Out of Memory" Exception for icon - 蒜头 简单介绍PDF,XPS,Images,Office 2007之间的转换方法 - 蒜头 - 博客园 SQL 2005 全文检索(续) 简单应用ReportViewer控件 配置SQL Server Session方法 采用负载均衡,部署了两个ASP.NET 2.0的站点服务器碰到的问题 初试VSTS 2008(TFS安装) Windows Server 2003分区修改方法[转载] 制作VSTO 2005 SE开发的Office 2007 AddIn的安装包 一个简单的document library event handler VS 2005 SP1 安装错误 [续] 一个简单的Checkbox Custom Field Type ASP.NET 2.0 SQL Cache 配置方法 解读Document Library关于权限的对象模型 SharePoint应用AJAX.NET和AJAX Control Toolkit
简单实现C#生成Excel 2007文件并下载
蒜头 · 2008-01-28 · via 博客园 - 蒜头


添加引用Microsoft Excel 12.0 Object Library,我这里使用的是Office 2007。

有必要的话,需要修改web.config:

<identity impersonate="true" userName="administrator" password="password"/>

目的是使用这个账号来运行Excel程序,曾经使用Word 2007在代码中生成XPS文件,因为没有如此修改web.config,初始化Word程序时,得到无权限的异常或者弹出Windows登陆框。

代码如下:

            Application app = null;
            Workbook wb 
= null;
            Worksheet sheet 
= null;

            
try
            
{
                app 
= new Application();
                app.Visible 
= false;

                wb 
= (Workbook)app.Workbooks.Add(Missing.Value);
                sheet 
= (Worksheet)wb.ActiveSheet;

                
for (int i = 0; i < contents.Count; i++)
                
{
                    String[] item 
= contents[i];

                    
for (int j = 0; j < item.Length; j++)
                        sheet.Cells[i
+1, j+1= item[j];
                }

                wb.Saved 
= true;

                
string fileName = String.Format("{0}{1}.xlsx", Path.GetTempPath(), DateTime.Now.Ticks);
                app.ActiveWorkbook.SaveCopyAs(fileName);

                
return fileName;
            }

            
finally
            
{
                wb.Close(
nullnullnull);
                app.Workbooks.Close();
                app.Quit();
                Marshal.ReleaseComObject((
object)app);
                Marshal.ReleaseComObject((
object)wb);
                Marshal.ReleaseComObject((
object)sheet);
                GC.Collect();
            }

需要注意的是:

sheet.Cells[i+1, j+1] = item[j];

cell要从一行一列开始填值,不能从零行零列。
另外是finally中的代码,目的关闭Excel.exe进程。

在一个button事件或者其他postback事件中,使用上面的代码生成文件后,附加下面代码,就可以实现下载:

                    Response.AppendHeader("Content-Disposition""attachment;filename=MassPay.xlsx"); 
                    Response.ContentType 
= "application/ms-excel";
                    Response.WriteFile(fileName);

postback完成后,客户端会弹出保存窗口.