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

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
B
Blog
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
IT之家
IT之家
D
Docker
Google DeepMind News
Google DeepMind News
罗磊的独立博客
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
博客园 - 司徒正美
Engineering at Meta
Engineering at Meta

博客园 - zhouruixi0728

通过数据库生成CodeFirst实体类(带字段中文名称) 防止页面重复提交,插入重复数据的解决办法 提高编码效率的一些经验 刷访问量程序 解决Ext Tab 从ifram1页刷新ifram2页 ext控件宽度变小的bug Repeater没有数据时显示空行 - zhouruixi0728 - 博客园 水晶报表添加字段 asp.net项目注意事项 - zhouruixi0728 - 博客园 清空sql2005数据库日志 还原卡破解(联想机房管家) asp.net ModalPopupExtender实现指定位置弹出 C# 多线程辅助类实现多任务 sql2005导出Excel错误解决方法 C#word转pdf失败解决方法 如何判断IIS版本 判断IIS中.NET2.0是否注册 一个委托的小例子 C# XceedFtp下载卡死解决方案 SQL 两表之间拷贝复制数据 C# tcp发送消息和传输文件
ASP.NET Simple OOXML导出Excel,Word
zhouruixi0728 · 2012-07-05 · via 博客园 - zhouruixi0728

1)简介

  Simple OOXML(http://simpleooxml.codeplex.com/)是一个基于Open Office SDK v 2.0,同时支持office2007 Excel和Word导出的开源类库。支持.net 3.5及以上版本。对于Excel支持多种数据格式包括字符串、数字、日期等,支持删除工作簿,支持DataTable的粘贴,支持合并单元格和设置单元格样式等等。对于Word支持通过书签定位来输出字段的功能。

  下载地址:本地下载

2)导出Excel

        protected void Button1_Click(object sender, EventArgs e)
        {
            MemoryStream stream = SpreadsheetReader.Create();
            SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, true);
            WorksheetPart worksheetPart = SpreadsheetReader.GetWorksheetPartByName(doc, "Sheet1");
            WorksheetWriter writer = new WorksheetWriter(doc, worksheetPart);

            writer.PasteText("B2", "Hello World");
            

            //Save to the memory stream
            SpreadsheetWriter.Save(doc);
            
            //Write to response stream
            Response.Clear();
            Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", "example1.xlsx"));
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            stream.WriteTo(Response.OutputStream);
            Response.End();
        }

  如果是现有Excel模版的导出则可以把

MemoryStream stream = SpreadsheetReader.Create();

  修改为

MemoryStream stream = SpreadsheetReader.Copy(“columnstemplate.xlsx");

3)导出Word

        public void DocumentPasteTest()
        {
            MemoryStream stream = DocumentReader.Copy(string.Format("{0}\\template.docx", TestContext.TestDeploymentDir));
            WordprocessingDocument doc = WordprocessingDocument.Open(stream, true);
            
            MainDocumentPart mainPart = doc.MainDocumentPart;

            DocumentWriter writer = new DocumentWriter(mainPart);
            
            Text name = writer.PasteText("Koos van der Merwe", "NAME");
            Text age = writer.PasteText("53", "AGE");
            //Save to the memory stream, and then to a file
            writer.Save();

            DocumentWriter.StreamToFile(string.Format("{0}\\templatetest.docx", GetOutputFolder()), stream);

        }

  通过Word的书签功能实现粘贴文本的定位。

4)结语

  Simple OOXML是一个功能非常全面好用的导出Excel和Word的解决方案,唯一美工不足是不支持office 2003的导出,希望对于.NET导出数据的朋友有所帮助。