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预览要求 ...
随机推荐
- SSRS 请求并显示SharePoint人员和组字段
场景: 使用Reporting Service请求SharePoint List,该list中包含人员和组字段.要求:只显示人员或组的display name.示例如下: 项目 参与人员 期望显示 项 ...
- perl 创建文本框
my $mw = MainWindow->new(-title => "Mem monitor"); $frm_name1 = $mw -> Frame()-&g ...
- cf479E Riding in a Lift
E. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- AngularJS中文介绍
简介 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小 ...
- linux下java调用.so文件的方法1: JNI
摘自http://blog.163.com/squall_smile/blog/static/6034984020129296931793/ https://my.oschina.net/simabe ...
- hdu 1599 find the mincost route(flyod求最小环)
Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1, ...
- for、foreach和MoveNext循环效率粗比较
今天没事对for循环.foreach循环.MoveNext循环,执行效率进行了对比:粗略测试代码如下: static void Main(string[] args) { #region 三种方式循环 ...
- lua select
可以用这样的方法产生类似foreach的功能: function printargs(...) local num_args = select("#", ...) for i = ...
- Android应用程序组件Content Provider的启动过程源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6963418 通过前面的学习,我们知道在Andr ...
- Extjs 6 MVC开发模式(一)
1.Extjs就绪函数 1)导入Extjs的CSS <link rel="stylesheet" type="text/css" href="r ...