Syncfusion Essential DocIO操作word文件实用函数

简介:
复制代码

Essential DocIO

.NET库,能够读写Microsoft Word文件。该组件是一个对象模型,同Microsoft Office COM类库相似,它不采用COM interop,以C#编写。如果系统内没有安装Microsoft Word,可以考虑该组件。

创建新的MS Word文档:支持创建包含文本、图片、图表、页面和页脚的MS Word文档。

文档格式化:支持格式化为通用的MS Word 报告。

文档生成基于模板:基于模板生成文档,可以使用MS Word GUI设计文档报告,然后使用DocIO向模板文件内动态填充数据。

文档属性:读写Word文档的属性设置。

转换:支持使用Essential PDF将MS Word文档转换为PDF。

高级特性:支持复制和合并多个MS Word文档为单个文档。

复制代码
ExpandedBlockStart.gif
复制代码
public static byte[] ConvertHtmlToDoc( string html)
{
var document = new WordDocument();
IWSection section = document.AddSection();
IWParagraph para = section.AddParagraph();

string errorMessage = "";
bool valid = section.Body.IsValidXHTML(html, XHTMLValidationType.Strict, out errorMessage);
if (!valid)
throw new InvalidCastException(errorMessage + " <hr> " + html) ;
document.XHTMLValidateOption = XHTMLValidationType.Strict;
section.Body.InsertXHTML(html);
var outMem = new MemoryStream();

document.Save(outMem, FormatType.Doc);
outMem.Seek( 0, SeekOrigin.Begin);
var content = new byte[outMem.Length];
outMem.Read(content, 0, content.Length);
outMem.Dispose();
document.Close();
return content;
}
复制代码

ExpandedBlockStart.gif
复制代码
/// <summary>
/// 生成Word的时候替换指定的文字
/// </summary>
/// <param name="templatePath"></param>
/// <param name="FileName"></param>
/// <param name="replaysDictionary"></param>
public static void ReplaceDocContent( string templateFileName, string newFileName,
Dictionary< string, string> replaysDictionary)
{
IWordDocument document = new WordDocument();
document.Open(templateFileName, FormatType.Doc);
foreach (var rd in replaysDictionary)
{
if ( string.IsNullOrEmpty(document.GetText())) continue;

document.Replace(rd.Key, rd.Value, false, false);
while (document.GetText().IndexOf(rd.Key) != - 1)
document.Replace(rd.Key, rd.Value, false, false);
}
document.Save(newFileName, FormatType.Doc);
}
复制代码

ExpandedBlockStart.gif
复制代码
public static Stream SetDocProtect( byte[] docContent, string key)
{
var mem = new MemoryStream(docContent);
mem.Seek( 0, SeekOrigin.Begin);

IWordDocument document = new WordDocument(mem, FormatType.Automatic);
document.Protect(ProtectionType.AllowOnlyFormFields, key);
var outMem = new MemoryStream();
document.Save(outMem, FormatType.Doc);
outMem.Seek( 0, SeekOrigin.Begin);
return outMem;
}
复制代码

ExpandedBlockStart.gif
复制代码
public static IWTable ReplaceTable(WordDocument document, string bookmarkName, DataTable data, string mergeColName , List<List< string>> mutilTableCaption)
{
if (document == null) throw new ArgumentNullException( " document ");
if (bookmarkName == null) throw new ArgumentNullException( " bookmarkName ");
if (data == null) throw new ArgumentNullException( " data ");
if (data.Columns.Count < 1) throw new ArgumentNullException( " data ");

int captionCount = mutilTableCaption != null && mutilTableCaption.Count > 0 ? mutilTableCaption.Count : 1;
WTable table = new WTable(document, true);

table.ResetCells(data.Rows.Count + captionCount, data.Columns.Count);

for (var colCount = 0; colCount < captionCount; colCount++)
{
for (var col = 0; col < data.Columns.Count; col++)
{
var paragraph = table.Rows[colCount].Cells[col].AddParagraph();

var caption = data.Columns[col].ColumnName;
if (mutilTableCaption != null && mutilTableCaption.Count > 0)
caption = mutilTableCaption[colCount][col];
var text = paragraph.AppendText(caption);
paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
text.CharacterFormat.FontName = " 宋体 ";
text.CharacterFormat.Bold = false;
text.CharacterFormat.FontSize = 10.5f;
}
}

for (var row = captionCount; row <= data.Rows.Count; row++)
for (var col = 0; col < data.Columns.Count; col++)
{
var paragraph = table.Rows[row].Cells[col].AddParagraph();
var text = paragraph.AppendText(data.Rows[row - captionCount][col] + "");

text.CharacterFormat.FontName = " 宋体 ";
text.CharacterFormat.FontSize = 9f;
double val = 0;
if ( double.TryParse(text.Text, out val))
{
text.Text = Math.Round(val, 2) + "";
// align right
paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
table.Rows[row].Cells[col].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
table.Rows[row].Cells[col].CellFormat.TextWrap = false;
}
}
// 合并单元格,向下合并
if (! string.IsNullOrEmpty(mergeColName))
for (var row = captionCount; row < table.Rows.Count; row++)
{
var cell = table.Rows[row].Cells[data.Columns[mergeColName].Ordinal];
cell.CellFormat.VerticalMerge = CellMerge.Start;
var text = data.Rows[row - captionCount][mergeColName] + "";
if (row > captionCount)
{
var priorCell = table.Rows[row - captionCount].Cells[data.Columns[mergeColName].Ordinal];
var findText = data.Rows[row - captionCount - 1][mergeColName] + "";
if (text.Equals(findText))
cell.CellFormat.VerticalMerge = CellMerge.Continue;
}
}

BookmarksNavigator bk = new BookmarksNavigator(document);
bk.MoveToBookmark(bookmarkName);

TextBodyPart body= bk.GetBookmarkContent();
bk.DeleteBookmarkContent( true);

bk.InsertTable(table);
return table;
}



本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2010/02/04/1663398.html,如需转载请自行联系原作者
相关文章
|
1月前
|
XML 关系型数据库 MySQL
python将word(doc或docx)的内容导入mysql数据库
用python先把doc文件转换成docx文件(这一步也可以不要后续会说明),然后读取docx的文件并另存为htm格式的文件(上一步可以直接把doc文件另存为htm),python根据bs4获取p标签里的内容,如果段落中有图片则保存图片。(图片在word文档中的位置可以很好的还原到生成的数据库内容) 我见网上有把docx压缩后解压获取图片的,然后根据在根据xml来读取图片的位置,我觉得比较繁琐。用docx模块读取段落的时候还需要是不是判断段落中有分页等,然而转成htm之后就不用判断那么多直接判断段落里的样式或者图片等就可以了。
22 1
|
3月前
Sources close to the matter 的含义和使用场合介绍
Sources close to the matter 的含义和使用场合介绍
22 0
|
芯片 Python
M1 Mac 下使用python将doc批量转换为docx
M1 Mac 的兼容原因,win32com库无法引用,故而通过其他方法实现,将doc批量转换为docx。
993 1
M1 Mac 下使用python将doc批量转换为docx
|
4月前
|
XML Go 数据格式
Go如何自动解压缩包?如何读取docx/doc文件内容?
在开发过程中,我们常常需要处理压缩包和文档文件。本文将介绍如何使用Go语言自动解压缩包和读取docx/doc文件。
|
8月前
|
Shell Linux Python
【Python】一键查询依赖生成文件 requirements.txt
【Python】一键查询依赖生成文件 requirements.txt
136 0
|
Python
使用python-docx-template操作word文档
1.python-docx-template简介 我们知道可以使用python-docx库来创建word文档,但是对于文档的修改功能,还可以使用另一个库 python-docx-template 可以完成对word的修改工作。 python-docx-template 模块主要依赖两个库, python-docx用于读取,编写和创建子文档 , jinja2用于管理插入到模板docx中的标签 。 其基本思路是利用jinja2制作Word模板,并动态向模板中插入文字、图片、表格等内容。 安装所需要的库文件🙌
582 0
使用python-docx-template操作word文档
|
Python
python中读取txt文件时split()函数的妙用
python中读取txt文件时split()函数的妙用
449 2
python中读取txt文件时split()函数的妙用
|
数据采集 安全 数据格式
python读取word详解【from docx import Document】
python读取word详解【from docx import Document】
493 0
python读取word详解【from docx import Document】
|
Apache
使用POI读写Word doc文件
使用POI读写word doc文件 目录 1     读word doc文件 1.1     通过WordExtractor读文件 1.2     通过HWPFDocument读文件 2     写word doc文件          Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的。
2222 0