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

推荐订阅源

A
Arctic Wolf
T
Tenable Blog
T
Troy Hunt's Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hacker News: Front Page
S
Secure Thoughts
AWS News Blog
AWS News Blog
L
LINUX DO - 最新话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
MIT News - Artificial intelligence
T
Tor Project blog
S
Schneier on Security
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
D
Docker
J
Java Code Geeks
GbyAI
GbyAI
H
Heimdal Security Blog
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
T
Tailwind CSS Blog
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
The Register - Security
The Register - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - 心利

DocOptimizer 0.9.0 Beta Released SharePoint GroupedItemPicker Control Drag & Drop between SharePoint Document libraries Improving SharePoint User Experience With JQuery-Client Side Form Validate SharePoint How To:定位错误:“An SPRequest object was not disposed .." SharePoint Tips and Tricks --如何用JS向PeopleEditor填充数据 SharePoint Tips and Tricks -- Ribbon,People Editor 使用T4为数据库自动生成实体类(C#) (Tips&Tricks)用客户端模板精简JavaScript代码 (Tips &Tricks)如何为Windows Mobile 创建拨号连接--C# System.Xml FAQ Part 1 Windows Ce vs Windows Mobile My Page StartKit项目概览 做一个更好的程序员 .Net类库中实现的HashTable 为Asp.net控件写单元测试(ViewState) 使用Control Adapters优化Asp.net控件 ViewState 简述一(With Example And Apply to Asp.net) 桌面背景:rss新闻阅读器(图)
Asp.net程序中生成Excel报表
心利 · 2008-04-14 · via 博客园 - 心利
 

   在后台调用excel组件,生成Excel,虽然可以Excel文件进行完全控制,可以生成任何复杂的格式,但是有个很大的缺点,这种方式会产生很多Excel进程很难完全清掉,特别是在出错的时候,可能会使整个服务器崩溃。本文为大家介绍一个C#写的开源组件,并简单说下office2003和以上版本支持的XML格式。

 操作Excel二进制格式

    OpenOffice.org发布过的俩个文档Excel File Format (BIFF8)SpecificationMicrosoft CompoundDocument (OLE2) Format SpecificationExcel的二进制格式做了一个比较详细的说明,依靠这些信息,我们可以直接操作Office二进制格式文档。

  MyXls是一个C#写的开源组件,可以用来生成具有很多表格且包含格式的Excel文件。它提供了一套基于对象的API,非常容易使用。

1,生成一个空的表格

1 XlsDocument xls = new XlsDocument(); //创建一个空的Excel文档
2        
3 xls.Send(); //将文档发送到浏览器。

2, 创建一个复杂点表格

 XlsDocument xls = new XlsDocument();
            xls.FileName 
= "Wacky.xls";

            
//添加文件属性
            xls.SummaryInformation.Author = "Tim Erickson"//作者
            xls.SummaryInformation.Subject = "A wacky display of Excel file generation";
            xls.DocumentSummaryInformation.Company 
= "in2bits.org";

            
for (int sheetNumber = 1; sheetNumber <= 5; sheetNumber++)
            
{
                
string sheetName = "Sheet " + sheetNumber;
                
int rowMin = sheetNumber;
                
int rowCount = sheetNumber + 10;
                
int colMin = sheetNumber;
                
int colCount = sheetNumber + 10;
                
//创建5个表格
                Worksheet sheet = xls.Workbook.Worksheets.AddNamed(sheetName);
                Cells cells 
= sheet.Cells;
                
for (int r = 0; r < rowCount; r++)
                
{
                    
if (r == 0)
                    
{
                        
for (int c = 0; c < colCount; c++)
                        
{
                            
//在一行内创建colCount个单元格
                            cells.Add(rowMin + r, colMin + c, "Fld" + (c + 1)).Font.Bold = true;
                        }

                    }

                    
else
                    
{
                        
for (int c = 0; c < colCount; c++)
                        
{
                            
int val = r + c;
                            Cell cell 
= cells.Add(rowMin + r, colMin + c, val);
                            
if (val % 2 != 0)
                            
{
                                cell.Font.FontName 
= "Times New Roman";
                                cell.Font.Underline 
= UnderlineTypes.Double;//给文字下方加一个双下划线
                                cell.Rotation = 45;//单元格文字旋转45度
                            }

                        }

                    }

                }

            }

效果图,一个Excel文件包含五个表格

二 XML格式office2003或以上版本才支持xml格式,这样可以直接通过一些模板将内容转化为Office可以识别的xml,限于时间,下次再与大家讨论:-)