C#使用Aspose.Words操作word文档

2022-10-22,,,,

最近接到个需求,由于客服这边要导出大量有一定规则的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文档的相关教程结束。

《C#使用Aspose.Words操作word文档.doc》

下载本文的Word格式文档,以方便收藏与打印。