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

推荐订阅源

C
Cybersecurity and Infrastructure Security Agency CISA
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
小众软件
小众软件
博客园 - 【当耐特】
爱范儿
爱范儿
美团技术团队
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
月光博客
月光博客
宝玉的分享
宝玉的分享
V
V2EX
Blog — PlanetScale
Blog — PlanetScale
Spread Privacy
Spread Privacy
博客园 - 叶小钗
量子位
Security Archives - TechRepublic
Security Archives - TechRepublic
Google DeepMind News
Google DeepMind News
L
LINUX DO - 热门话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
P
Privacy International News Feed
Y
Y Combinator Blog
P
Proofpoint News Feed
NISL@THU
NISL@THU
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
G
Google Developers Blog
Hacker News: Ask HN
Hacker News: Ask HN
Help Net Security
Help Net Security
I
InfoQ
Cisco Talos Blog
Cisco Talos Blog
Google Online Security Blog
Google Online Security Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
Vercel News
Vercel News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Vulnerabilities – Threatpost
B
Blog
Stack Overflow Blog
Stack Overflow Blog
Microsoft Azure Blog
Microsoft Azure Blog
N
News | PayPal Newsroom
J
Java Code Geeks
Latest news
Latest news
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - skyfei

Xcode 文档注释方法 查看iOS模拟器应用的沙箱文件 Show in Finder OC代码 C# x86应用x64系统上读取x64位应用的注册表 Make webclient support upload the large file which are larger than 1G Shortcut key for WPF get Android information with adb, the build version Decodes a QuotedPrintable encoded string C# USB Detection - winform and WPF [转] 线程同步 C# 常见面试题(2) 转:C# Interview Questions Openxml: 导出excel 设置 cell的格式 - skyfei OpenXML: excel 插入BarChart图表 OpenXML: Asp.net利用OpenXML 导出Excel. TRIGGERS :Cannot use text, ntext, or image columns in the 'inserted' and ' deleted' tables. 'String or binary data would be truncated' error message 利用.Net Framework2.0 zip压缩、解压 string 数据 C#中文和UNICODE字符转换方法 - skyfei - 博客园
.net Create Excel 2007 file with open xml
skyfei · 2008-10-22 · via 博客园 - skyfei

Open XML 是从office 2007 开始被支持的, 用Open xml可以不用Office dcom创建标准office 文档, Office Dcom进程释放不掉, 实在太烦人了, 而且资源消耗大. 用Open xml可以直接生成office 文档, 不过现在文档资料比较少, 且方法不是很可取, 包括msdn上的一些文档, 创建office 文档都是用拼字符串的方法来做的, 个人感觉不符合C#编码习惯, 不过也情有可原, 这文档大多是在open xml sdk1.0 年代写的, 先Open xml SDK2.0出来了, 经过一阵专研, 终于写出了一个excel 文件,  不过可惜, boss 考虑到风险问题, 让我继续要dcom创建, 因此仅仅研究到创建excel 文件, 并往cell里写text. 不过这种方法对于asp.net 动态生成 excel 下载, 还是很有用途的, boss 要求的下载的excel 要求带chart的, 就是根据数据生成chart在下载的excel文件中, 因此那种直接response  girdview的方法不能满足要求.

闲话少叙, 看代码.

  • 用open xml 需要首先安装 open xml sdk,  并且在.net 3.0环境, 因为它需要用LINQ.
  • 添加DocumentFormat.OpenXml 引用
  • 使用名称空间:
  • using System.IO.Packaging;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Spreadsheet;

// 创建流, 如果用在asp.net下载, 可以用memorystream

FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

//创建spreadsheetDocument

using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(fs, SpreadsheetDocumentType.Workbook))
{

              WorkbookPart workbookPart = spreadSheet.AddWorkbookPart();
              workbookPart.Workbook = new Workbook();
              workbookPart.Workbook.AppendChild<Sheets>(new Sheets());

              //ShareString 不知道是做什么用, 没有找到相关文档介绍
               SharedStringTablePart sharestringTablePart = workbookPart.AddNewPart<SharedStringTablePart>();
               sharestringTablePart.SharedStringTable = new SharedStringTable();
               int i = 0;
               foreach (SharedStringItem item in sharestringTablePart.SharedStringTable.Elements<SharedStringItem>())
               {
                  string str =  item.InnerText;
                   i++;
               }
               sharestringTablePart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text("teasfsdfasdfxt")));
              sharestringTablePart.SharedStringTable.Save();

              //SharedStringTable好像不起什么作用, 没有好像还不行

              //下边的代码是创建 sheet

               WorksheetPart newWorksheetPart = workbookPart.AddNewPart<WorksheetPart>();
               newWorksheetPart.Worksheet = new Worksheet(new SheetData());
               newWorksheetPart.Worksheet.Save();
               Sheets sheets = workbookPart.Workbook.GetFirstChild<Sheets>();
               string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart);

               // Get a unique ID for the new sheet.
               uint sheetId = 1;

               string sheetName = "Sheet" + sheetId;

               // Append the new worksheet and associate it with the workbook.
               Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
               sheets.Append(sheet);

              //下边的代码是往sheet里写东西

               SheetData sheetData = newWorksheetPart.Worksheet.GetFirstChild<SheetData>();
               Row row = new Row(){RowIndex = 2};
               sheetData.Append(row);
               Cell newCell = new Cell() { CellReference = "B2" };
               newCell.DataType = new EnumValue<CellValues>(CellValues.String);
               row.InsertAt<Cell>(newCell, 0);
               newCell.CellValue = new CellValue("1");

               newCell.CellValue.Text = "testt";
               //这个保存很重要
               newWorksheetPart.Worksheet.Save();
               workbookPart.Workbook.Save();
              spreadSheet.Close();
              fs.Close();
          }

在创建的过程中一点错误就会导致创建的excel不能被打开. 另外有个外国公司已经封装了这个open xml, 很好用,很easy, 不过要钱. 网址: http://www.spreadsheetgear.com/ 

顺便发上我写用 exce Dcom 生成 excel

Code