最近接到个需求,由于客服这边要导出大量有一定规则的word文件,里面的内容希望系统自动填充,例如

这里我使用Aspose.Words.dll这个类库,

1.首先,我们需要创建模板文件,毕竟有规则的东西才好开发。在各个位置处添加书签,如下:

2.核心方法如下,由于我这边需求最多填充四个参数:中文品名、英文描述、HAWB、件数,所以下面方法就定义这几个变量。其中,有些模板若不需要个别参数,直接传空值就行。

 /// <summary>
/// 非危保函(将指定路径的模板Path_TempleteDoc输出至Path_out路径)
/// </summary>
/// <param name="Path_TempleteDoc">模板文件路径,包含文件名</param>
/// <param name="CNName">中文品名</param>
/// <param name="ENName">英文描述</param>
/// <param name="HAWB">HAWB</param>
/// <param name="PCS">件数</param>
/// <param name="Path_out">文件输出路径,包含文件名</param>
private void HandleGuaranteeDoc(string Path_TempleteDoc,string CNName,string ENName,string HAWB,string PCS,string Path_out)
{
string tempFile = Path.GetFullPath(Path_TempleteDoc).ToString(); //获取模板路径,这个根据个人模板路径而定。
Document doc = new Document(tempFile);
DocumentBuilder builder = new DocumentBuilder(doc); //操作word
Dictionary<string, string> dic = new Dictionary<string, string>(); //创建键值对 第一个string 为书签名称 第二个string为要填充的数据
if (!string.IsNullOrEmpty(CNName))
{
dic.Add("CNName", CNName);
}
if (!string.IsNullOrEmpty(ENName))
{
dic.Add("ENName", ENName);
}
if (!string.IsNullOrEmpty(HAWB))
{
dic.Add("HAWB", HAWB);
}
if (!string.IsNullOrEmpty(PCS))
{
dic.Add("PCS", PCS);
}
foreach (var key in dic.Keys) //循环键值对
{
builder.MoveToBookmark(key); //将光标移入书签的位置
builder.Write(dic[key]); //填充值
}
doc.Save(Path_out); //保存word
}

另附上文件的复制和整个文件夹的复制

文件复制(路径都准确到文件名):

 /// <summary>
/// 大文件多次复制文件 true:复制成功 false:复制失败
/// </summary>
/// <param name="soucrePath">原始文件路径包含文件名</param>
/// <param name="targetPath">复制目标文件路径,包含文件名</param>
/// <returns></returns>
public bool CopyFile(string soucrePath, string targetPath)
{
try
{
//读取复制文件流
using (FileStream fsRead = new FileStream(soucrePath, FileMode.Open, FileAccess.Read))
{
//写入文件复制流
using (FileStream fsWrite = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = new byte[ * * ]; //每次读取2M
//可能文件比较大,要循环读取,每次读取2M
while (true)
{
//每次读取的数据 n:是每次读取到的实际数据大小
int n = fsRead.Read(buffer, , buffer.Count());
//如果n=0说明读取的数据为空,已经读取到最后了,跳出循环
if (n == )
{
break;
}
//写入每次读取的实际数据大小
fsWrite.Write(buffer, , n);
}
}
}
return true;
}
catch (System.Exception ex)
{
return false;
}
}

文件夹复制(路径都指到文件夹路径)

/// <summary>
/// Copy文件夹至
/// </summary>
/// <param name="sourceDir">原路径</param>
/// <param name="toDir">目标路径</param>
public static void CopyDirectInfo(string sourceDir, string toDir)
{
if (!Directory.Exists(sourceDir))
{
throw new ApplicationException("Source directory does not exist");
}
if (!Directory.Exists(toDir))
{
Directory.CreateDirectory(toDir);
}
DirectoryInfo directInfo = new DirectoryInfo(sourceDir);
//copy files
FileInfo[] filesInfos = directInfo.GetFiles();
foreach (FileInfo fileinfo in filesInfos)
{
string fileName = fileinfo.Name;
File.Copy(fileinfo.FullName, toDir + @"/" + fileName, true);
}
//copy directory
foreach (DirectoryInfo directoryPath in directInfo.GetDirectories())
{
string toDirPath = toDir + @"/" + directoryPath.Name;
CopyDirectInfo(directoryPath.FullName, toDirPath);
}
}

C#使用Aspose.Words操作word文档的更多相关文章

  1. 使用Aspose.Words将Word文档转换为Tiff格式图片文件

    用Aspose组件的优点是操作Word文档不需要安装Office就可以实现. 首先需要引用Aspose.Words.dll,链接地址:链接:https://pan.baidu.com/s/1rJvjp ...

  2. iText操作word文档总结

    操作word文档的工具有很多,除了iText之外还有POI,但是POI擅长的功能是操作excel,虽然也可以操作word,但是能力有限,而且还有很多的bug,技术并不成熟,下面就重点介绍一种操作wor ...

  3. C#操作Word文档(加密、解密、对应书签插入分页符)

    原文:C#操作Word文档(加密.解密.对应书签插入分页符) 最近做一个项目,客户要求对已经生成好的RTF文件中的内容进行分页显示,由于之前对这方面没有什么了解,后来在网上也找了相关的资料,并结合自己 ...

  4. Aspose.Words给word文档加水印

    需求:在一些重要的Word文档需要打印时,添加水印以明出处. 方案:使用Aspose组件给word文档 代码:干货如下 /// <summary> /// Inserts a waterm ...

  5. 利用Python操作Word文档【图片】

    利用Python操作Word文档

  6. WPF使用Aspose.Words导出Word文档

    一.创建Word文档模板 分析需要导出的word文档,将固定的内容和由程序生成的内容分开; 创建一个word(例如:Template.doc)文档,将固定的内容按照一定的格式写入当前文档中; 打开Te ...

  7. Java文件操作系列[3]——使用jacob操作word文档

    Java对word文档的操作需要通过第三方组件实现,例如jacob.iText.POI和java2word等.jacob组件的功能最强大,可以操作word,Excel等格式的文件.该组件调用的的是操作 ...

  8. QTP操作word文档

    QTP可以对word文档进行操作,这里最主要展示的是向word文档写入内容,并保存的功能. Option explicit Dim wordApp Set wordApp = createobject ...

  9. c#中操作word文档-四、对象模型

    转自:http://blog.csdn.net/ruby97/article/details/7406806 Word对象模型  (.Net Perspective) 本文主要针对在Visual St ...

随机推荐

  1. 利用Hadoop自带example实现wordCount

    上次虽然把环境搭好了,但是实际运行起来一堆错误,下面简述一下踩的坑. 1.hadoop fs -put上传文件失败,WARN org.apache.hadoop.hdfs.server.datanod ...

  2. HBase高可用原理与实践

    前言 前段时间有套线上HBase出了点小问题,导致该套HBase集群服务停止了2个小时,从而造成使用该套HBase作为数据存储的应用也出现了服务异常.在排查问题之余,我们不禁也在思考,以后再出现类似的 ...

  3. 洛谷P2828 Switching on the Lights(开关灯)

    P2828 Switching on the Lights(开关灯) 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.( ...

  4. PostGIS安装 pgis3.4.2 postgresql 10.1

    https://yq.aliyun.com/articles/228258http://download.osgeo.org/postgis/source/http://blog.51cto.com/ ...

  5. ubuntu命令错误集

    1.在ubuntu命令行使用rz从windows传输文件时出现乱码 解决方法:使用 rz -e    选项进行传输,一般小文件传输不用加 -e 选项,大文件传输需要.

  6. P4135 作诗

    传送门 分块 设sum[ i ] [ j ] 存从左边到第 i 块时,数字 j 的出现次数 f [ i ] [ j ] 存从第 i 块,到第 j 块的一整段的答案 那么最后答案就是一段区间中几块整段的 ...

  7. ACM-较大的数乘法取模技巧*

    比如模数是1e15这种,相乘的时候爆LL了,但是又不想用大数,咋办呢? long long ksc(long long a, long long b, long long mod){ ; while( ...

  8. 解决spark-shell输出日志信息过多

    import org.apache.log4j.Logger import org.apache.log4j.Level Logger.getLogger("org").setLe ...

  9. spring boot test MockBean

    使用spring boot , MockBean @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) p ...

  10. C# String.Format方法

    一.定义String.Format是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项. 如: (1)string p1 = "Jackie";strin ...