C#使用Spire.Doc Word for .Net读写Word
以前对Excel或Word文档操作都使用微软的COM组件Microsoft Word 15.0 object library。
但是这种方式必须要求服务器上安装Office,而且会出现读写操作完成后未成功释放资源的情况。
还有使用NPOI的方式,可以在没有安装Office的情况下对Word或Excel文档进行读写操作,但是面对复杂的需求,还是显得无能为力。
于是出现了一些其他的API库,如Spire.Doc和Aspose,不但可以很好地读写操作Office文档,而且可以轻松实现PDF等文档格式的转换。
Spire.Doc支持JAVA和C#开发语言。我们可以从官网下载到。
Document document = new Document();
Section section = document.AddSection();
ShowProgressBar(); //显示进度条
for (int i = ; i < dsSpInfo.Tables[].Rows.Count; i++)
{
GenerateWord(document,section, dsSpInfo.Tables[], dsQiandi.Tables[], dsBaocun.Tables[],i); //生成Word文档
}
section.AddColumn(550f, 50f); //添加分栏,参数为分栏宽度
section.AddColumn(550f, 50f);
section.MakeColumnsSameWidth();
string s = Guid.NewGuid().ToString("N") + ".docx";
document.SaveToFile(@"temp\" + s, FileFormat.Docx);
document.Close();
System.Diagnostics.Process.Start(@"temp\" + s);
private void GenerateWord(Document document,Section section,DataTable dtSpInfo, DataTable dtQiandi, DataTable dtBaocun,int index)
{ Paragraph pFamily = section.AddParagraph(); //添加段落
AddTextRange(section, pFamily, cfamily, , true, "黑体", Spire.Doc.Documents.HorizontalAlignment.Center);
AddTextRange(section, pFamily, family, , true, "宋体", Spire.Doc.Documents.HorizontalAlignment.Center); Paragraph paragraph = section.AddParagraph();
AddTextRange(section, paragraph, cGenus, , true, "黑体", Spire.Doc.Documents.HorizontalAlignment.Left);
AddTextRange(section, paragraph, genus, , true, "Times New Roman", Spire.Doc.Documents.HorizontalAlignment.Left); DataRow[] drQiandi = dtQiandi.Select("SPID='" + spid + "'"); //获取表格数据
if (drQiandi.Length > )
{
String[] headerQiandi = { "保存地点", "种质份数", "个体数量", "引种方式", "来源地", "生长状况" }; //表头字段
string[][] arrQiandiData = new string[drQiandi.Length][];
for (int i = ; i < drQiandi.Length; i++)
{
arrQiandiData[i] = new string[] { drQiandi[i]["保存地点"].ToString(), drQiandi[i]["种质份数"].ToString(), drQiandi[i]["个体数量"].ToString(), drQiandi[i]["引种方式"].ToString(), drQiandi[i]["来源地"].ToString(), drQiandi[i]["生长状况"].ToString() };
}
Table tableQiandi = section.AddTable(); //新建表格
tableQiandi.ResetCells(arrQiandiData.Length + , headerQiandi.Length);
tableQiandi.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single; TableRow rowQiandi = tableQiandi.Rows[]; //添加行
rowQiandi.IsHeader = true; //设为表头
rowQiandi.Height = ;
rowQiandi.HeightType = TableRowHeightType.Auto;
for (int i = ; i < headerQiandi.Length; i++) //生成表头
{
rowQiandi.Cells[i].Width = ;
rowQiandi.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
rowQiandi.Height = ;
rowQiandi.HeightType = TableRowHeightType.Auto;
Paragraph p = rowQiandi.Cells[i].AddParagraph();
AddTextRange(section, p, headerQiandi[i], , true, "黑体", Spire.Doc.Documents.HorizontalAlignment.Center);
} for (int r = ; r < arrQiandiData.Length; r++) //生成表体
{
TableRow dataRow = tableQiandi.Rows[r + ];
dataRow.RowFormat.BackColor = Color.Empty;
for (int c = ; c < arrQiandiData[r].Length; c++)
{
dataRow.Cells[c].Width = ;
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
TextRange tr = dataRow.Cells[c].AddParagraph().AppendText(arrQiandiData[r][c]);
tr.CharacterFormat.FontSize = ;
}
}
}
}
private void AddTextRange(Section section, Paragraph pragraph, string word, float fontSize, bool isBold, string fontName, Spire.Doc.Documents.HorizontalAlignment alignType)
{ TextRange textRange = pragraph.AppendText(word);
textRange.CharacterFormat.FontSize = fontSize;
textRange.CharacterFormat.Bold = isBold;
textRange.CharacterFormat.FontName = fontName;
pragraph.Format.HorizontalAlignment = alignType;
}
下面是生成的效果图

需要注意的是,Spire.Doc是收费软件,如果超过了免费版生成表格的数量(好像是25个),就要收取费用。否则会在生成的文档的第一页上方中添加广告。
C#使用Spire.Doc Word for .Net读写Word的更多相关文章
- Spire.Doc组件读取与写入Word
之前写了一篇开源组件DocX读写word的文章,当时时间比较匆忙选了这个组件,使用过程中还是有些不便,不能提前定义好模版,插入Form表单域进行替换.最近无意中发现Spire.Doc组件功能很强大,目 ...
- 使用Spire.Doc组件利用模板导出Word文档
以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作, ...
- 【好文翻译】一步一步教你使用Spire.Doc转换Word文档格式
背景: 年11月,微软宣布作为ECMA国际主要合作伙伴,将其开发的基于XML的文件格式标准化,称之为"Office Open XML" .Open XML的引进使office文档结 ...
- 使用Spire.Doc来转换文本
使用Spire.Doc来转换文本 前段时间,我为不熟悉这个产品的读者们写了一篇关于我对 Spire.Doc的初识印象.Spire.Doc是一个专业的Word .NET库,它是专门为开发人员设计的用来快 ...
- Spire.Doc组件
使用Spire.Doc组件利用模板导出Word文档 以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. ...
- [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc
开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...
- 在C#中使用Spire.doc对word的操作总结
在C#中使用Spire.doc对word的操作总结 在最近的工程中我们要处理一些word文档.通过在网上的大量搜索,我发现大多数软件功能不是不完整就是有重复.极少数可以完全实现的word组件又要收费. ...
- Word转图片(使用Spire.doc)
Spire.Doc for .NET是一款由E-iceblue公司开发的专业的Word .NET类库.支持.net,WPF,Silverlight, 下载地址:http://www.e-iceblue ...
- 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc (转)
[原文地址]http://www.cnblogs.com/liqingwen/p/5898368.html 序 本打算过几天简单介绍下组件 Spire.XLS,突然发现园友率先发布了一篇,既然 x ...
随机推荐
- linux远程windows桌面
rdesktop,例子如下,-f为全屏,-a为颜色设置 rdesktop -f -a 32 192.168.88.235
- UIView和CALayer区别
(1)首先UIView可以响应用户的触摸事件,Layer不可以. (2)View中frame getter方法,bounds和center,UIView并没有做什么工作:它只是简单的各自调用它底层的C ...
- MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)
设置Keepalived VIP切换邮件告警 修改keepalived.conf配置: [root@server01 keepalived]# cat keepalived.conf ! Config ...
- PROC IMPORT 选项
GETNAMES=YES;导入源文件字段名作为SAS数据集的字段名MIXED=NO;若某一列中包含数值型和字符型变量,将数值型按照缺省值处理.若选的是YES则是将数值型转换成字符型存储,默认为NOSC ...
- for update 与where current of的问题
在刚学oracle时一直不明白for update 的作用,今天考试又遇到郁闷半天,所以加以整理. 一: 1>首先for update是对表的行进行锁定.锁定就好比我们学java Thread那 ...
- php从数据库中取二进制流文件转换为图片,图片以二进制流存入数据库实现
php从数据库中取二进制流文件转换为图片,图片以二进制流存入数据库实现 function data_uri($contents, $mime) { $base64 = base64_encode($c ...
- tornado+jsonrpc
rpc:远程过程调用(A服务调用B服务的一个方法或函数) tornado中jsonrpc的使用 import json import tornado.httpserver import tornado ...
- C语言博客作业3--函数
C语言博客作业3--函数 1.本章学习总结 1.1思维导图 请以思维导图总结本周的学习内容,如下图所示: 1.2本章学习体会及代码量学习体会 1.2.1学习体会 描述本周学习感受,也可以在这里提出你不 ...
- peewee 通俗易懂版
Peewee作为Python ORM之一 优势:简单,小巧,易学习,使用直观 不足之处:需要手动创建数据库 基本使用流程 1⃣️根据需求定义好Model(表结构类) 2⃣️通过create_table ...
- Eclipse 上传 删除 下载 分析 hdfs 上的文件
本篇讲解如何通过Eclipse 编写代码去操作分析hdfs 上的文件. 1.在eclipse 下新建Map/Reduce Project项目.如图: 项目建好后,会默认加载一系列相应的jar包. 下 ...