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预览要求 ...
随机推荐
- BZOJ NOI十连测 第二测 T2
思路:20%可以搜索.. #include<algorithm> #include<cstdio> #include<cmath> #include<cstr ...
- jQuery 序列化表单数据 serialize() serializeArray()
1.serialize()方法 格式:var data = $("form").serialize(); 功能:将表单内容序列化成一个字符串. 这样在ajax提交表单数据时,就不用 ...
- Android 设置 横屏 竖屏
方法一:在AndroidManifest.xml中配置 如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上androi ...
- css案例学习之按钮超链接
效果 css实现 <html> <head> <title>按钮超链接</title> <style> a{ /* 统一设置所有样式 */ ...
- 2014第3周六升级win8.1
今天上班主要是沟通了一个需求,然后思考下实现方案并记录下要点,晚上没有加班就回来,把操作系统升级到了win8.1,升级的重要原因是,原来的win7时常会卡顿,并且开关机慢,还有上面装了很多无用的影响效 ...
- 使用 apache ant 轻松实现文件压缩/解压缩(转)
原文地址:http://blog.csdn.net/irvine007/article/details/6779492 maven配置ant包: <dependency> <grou ...
- Pasha and String(思维,技巧)
Pasha and String Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- APP设计规范大全
大图可保存到本地
- HTTP状态码搜集
一.1xx消息 这一类型的状态码,代表请求已经接受,需要继续处理. 这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束. 由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非 ...
- php.ini配置
PHP作为一门强大的脚本语言被越来越多的web应用程序采用,不规范的php安全配置可能会带来敏感信息泄漏.SQL注射.远程包含等问题,规范的安全配置可保障最基本的安全环境.下面我们分析几个会引发安全问 ...