Office OpenXML-Excel(一)
原文 http://www.cnblogs.com/changminglong/articles/2840004.html
适用于
2007 Microsoft Office 套件,Microsoft Office Excel 2007,Microsoft Office PowerPoint 2007,Microsoft Office Word 2007
OpenXML优势
用户可以在Office应用程序和企业系统之间使用XML和ZIP技术来交换数据。文档是全局可以访问的。并且,您还可以减少文件损坏的风险。
Office XML 格式的结构
基于简单的分部分的压缩的ZIP文件格式。在新的Office Open XML格式的核心使用一些XML的引用架构和一个ZIP容器。每个文件都是由一些部件的集合组成的;这个集合定义了文档。
文档部件是存储在容器文件当中,或者存储在基于工业标准的ZIP格式的包中。许多部件都是用来描述应用程序数据,元数据,以及自定义数据的XML数据,它们都是存储在容器文件当中的。
新建一个Excel文件把后缀名该为为ZIP格式,然后解压我们会看到如下结构;

_rels 目录
这个目录中包含一个.rels文件,它定义了包中的根关系。它是在解析整个包时首先要浏览的第一个文件;.rels 文件包含了基于起始部件(虚拟的起始部件)的关系.
使用OpenXML 导出Excel
首先在http://www.microsoft.com/en-us/download/details.aspx?id=5124 下载OpenXMLSDKv2和OpenXMLSDKTool;
然后在建立一个Excel表格用OpenXMLSDKTool 打开,如下图所示

建立一个新项目,建立一个新类把左侧的代码拷贝到这个新类中;然后我们把想放入Excel中的数据传递到这个类中。
在这个新类中定义一个新的静态方法用来生成表的行代码如下:

private static void GenerateTableRow(SheetData sheetData1, Persion person, uint row)
{
Row row2 = new Row() { RowIndex = (UInt32Value)row, Spans = new ListValue<StringValue>() { InnerText = "1:4" } };
Cell cell5 = new Cell() { CellReference = "A" + row, StyleIndex = (UInt32Value)1U, DataType = CellValues.String, CellValue = new CellValue(person.Name) };
Cell cell6 = new Cell() { CellReference = "B" + row, StyleIndex = (UInt32Value)1U, DataType = CellValues.Number, CellValue = new CellValue(person.Age.ToString()) };
Cell cell7 = new Cell() { CellReference = "C" + row, StyleIndex = (UInt32Value)1U, DataType = CellValues.String, CellValue = new CellValue(person.Address) };
Cell cell8 = new Cell() { CellReference = "D" + row, StyleIndex = (UInt32Value)1U, DataType = CellValues.String, CellValue = new CellValue(person.Company) }; row2.Append(cell5);
row2.Append(cell6);
row2.Append(cell7);
row2.Append(cell8); sheetData1.Append(row2);
} private static void GenerateTableHeader(SheetData sheetData1)
{
Row row1 = new Row() { RowIndex = (UInt32Value)1U, Spans = new ListValue<StringValue>() { InnerText = "1:4" } }; Cell cell1 = new Cell() { CellReference = "A1", StyleIndex = (UInt32Value)2U, DataType = CellValues.SharedString };
CellValue cellValue1 = new CellValue();
cellValue1.Text = ""; cell1.Append(cellValue1); Cell cell2 = new Cell() { CellReference = "B1", StyleIndex = (UInt32Value)2U, DataType = CellValues.SharedString };
CellValue cellValue2 = new CellValue();
cellValue2.Text = ""; cell2.Append(cellValue2); Cell cell3 = new Cell() { CellReference = "C1", StyleIndex = (UInt32Value)2U, DataType = CellValues.SharedString };
CellValue cellValue3 = new CellValue();
cellValue3.Text = ""; cell3.Append(cellValue3); Cell cell4 = new Cell() { CellReference = "D1", StyleIndex = (UInt32Value)2U, DataType = CellValues.SharedString };
CellValue cellValue4 = new CellValue();
cellValue4.Text = ""; cell4.Append(cellValue4); row1.Append(cell1);
row1.Append(cell2);
row1.Append(cell3);
row1.Append(cell4); sheetData1.Append(row1);
}

然后在主函数中通过调用这个新类把数据传过去就能生成Excel。
具体代码请下载参考:http://files.cnblogs.com/changminglong/OfficeOpenXMLTest.rar
Office OpenXML-Excel(一)的更多相关文章
- office openxml学习(一)
以前用过,aspose.dll处理word ,excel,之后发现 npoi,使用了一段时间,总觉得是第三方,不明白底层的实现,直到最近发现了office openxml ,其实这个技术,很久以前就有 ...
- Office(Excel、Word)二次开发——VSTO
Office(Excel.Word)二次开发——VSTO Office(Excel.Word)二次开发——VSTO Office二次开发模式: 1) VBA(visual studio for app ...
- Microsoft.Office.Interop.Excel的用法以及利用Microsoft.Office.Interop.Excel将web页面转成PDF
1.常见用法 using Microsoft.Office.Interop.Excel; 1)新建一个Excel ApplicationClass ExcelApp = New A ...
- 引用Microsoft.Office.Interop.Excel出现的问题
引用Microsoft.Office.Interop.Excel出现的问题 转自:http://www.hccar.com/Content,2008,6,11,75.aspx,作者:方继祥 操作背 ...
- Microsoft.Office.Interop.Excel操作Excel文件时出现的问题及解决方案
问题描述: Microsoft.Office.Interop.Excel.Worksheet 打不开文件 Microsoft Office Excel 不能访问文件"a.xls". ...
- Microsoft.Office.Interop.Excel 程序集引用 ,Microsoft.Office.Interop.Excel.ApplicationClass 无法嵌入互操作类型
using Microsoft.Office.Interop.Excel 添加程序集引用 方法:在引用--程序集--扩展中,添加引用Microsoft.Office.Interop.Excel,此 ...
- 利用Microsoft.Office.Interop.Excel 将web页面转成PDF
网上有很多将Web页面转成PDF的方法,还有许多收费的第三方插件.其实利用Office 自带的将EXCEL发布成PDF的功能就可以实现,如果你的需求没有多复杂,可以采用笔者的方法. 首先将web页面h ...
- Microsoft.Office.Interop.Excel, Version=12.0.0.0版本高于引用的程序集(已解决)
Microsoft.Office.Interop.Excel, Version=12.0.0.0版本高于引用的程序集(已解决) 论坛里的帮助:http://bbs.csdn.net/topics/39 ...
- 无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口
解决 把Microsoft.Office.Interop.Excel.DLL的嵌入互操作类型改为ture就可以了
- Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结
Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word excel pdf 的web预览要求 ...
随机推荐
- 子shell的$$
http://blog.csdn.net/firefoxbug/article/details/7426109
- ORA-14400: inserted partition key does not map to any partition
ORA-14400: inserted partition key does not map to any partition 数据库表已经分区,如果插入数据时出现错误提示: ORA-14400: 插 ...
- hash_map和map的区别
hash_map和map的区别 分类: STL2008-10-15 21:24 5444人阅读 评论(0) 收藏 举报 class数据结构编译器存储平台tree 这里列几个常见问题,应该对你理解和使用 ...
- UESTC_邱老师玩游戏 2015 UESTC Training for Dynamic Programming<Problem G>
G - 邱老师玩游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- HTML5 Canvas Arc Tutorial
HTML5 Canvas Arc Tutorial HTML5 Canvas Arc Tutorial
- [Oracle] Data Guard 系列(5) - 创建逻辑备库
在创建逻辑备库之前,必须得先创建物理备库,关于如何创建物理备库,请参考<Data Guard 系列(4) - 在不停主库的情况下创建物理备库>. 1. 在物理备库上停止日志应用服务 SYS ...
- 所闻所获6:meditashayne项目总结
项目源码下载地址: https://github.com/ShayneYeorg/Meditashayne 1.首先一开始设计这个App的时候,我就希望它能比系统自带的备忘录更方便:比如备忘录需要手动 ...
- cocos2d-x-2.2.5项目创建--命令行创建
Gavin:downloads DavidLik$ cd cocos2d-x-2.2.5/ Gavin:cocos2d-x-2.2.5 DavidLik$ cd tools/ Gavin:tools ...
- Elasticlunr.js 简单介绍
Elasticlunr.js 项目地址:http://elasticlunr.com/ 代码地址:https://github.com/weixsong/elasticlunr.js 文档地址:htt ...
- 使用isql连接Sybase ASE数据库的常见错误及处理方式
使用isql连接Sybase ASE数据库 Sybase ASE客户端工具中有一个比较实用的命令行工具isql.利用isql可以对ASE数据库服务器进行几乎所有的管理维护工作. 下面用isql工具连接 ...