博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#使用Aspose.Words操作word文档(利用模板2)
阅读量:4302 次
发布时间:2019-05-27

本文共 3995 字,大约阅读时间需要 13 分钟。

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

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

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

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

复制代码

///         /// 非危保函(将指定路径的模板Path_TempleteDoc输出至Path_out路径)        ///         /// 模板文件路径,包含文件名        /// 中文品名        /// 英文描述        /// HAWB        /// 件数        /// 文件输出路径,包含文件名        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
dic = new Dictionary
(); //创建键值对 第一个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 }

复制代码

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

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

复制代码

///         /// 大文件多次复制文件  true:复制成功   false:复制失败        ///         /// 原始文件路径包含文件名        /// 复制目标文件路径,包含文件名        /// 
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[1024 * 1024 * 2]; //每次读取2M //可能文件比较大,要循环读取,每次读取2M while (true) { //每次读取的数据 n:是每次读取到的实际数据大小 int n = fsRead.Read(buffer, 0, buffer.Count()); //如果n=0说明读取的数据为空,已经读取到最后了,跳出循环 if (n == 0) { break; } //写入每次读取的实际数据大小 fsWrite.Write(buffer, 0, n); } } } return true; } catch (System.Exception ex) { return false; } }

复制代码

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

复制代码

///         /// Copy文件夹至        ///         /// 原路径        /// 目标路径        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);            }        }

复制代码

 

转载地址:http://jwlws.baihongyu.com/

你可能感兴趣的文章
Mysql出现Table 'performance_schema.session_status' doesn't exist
查看>>
MySQL innert join、left join、right join等理解
查看>>
vivado模块封装ip/edf
查看>>
sdc时序约束
查看>>
Xilinx Jtag Access/svf文件/BSCANE2
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(二) 基础命令
查看>>
Docker(三) 构建镜像
查看>>
Spring 全家桶注解一览
查看>>
JDK1.8-Stream API使用
查看>>
cant connect to local MySQL server through socket /tmp/mysql.sock (2)
查看>>
vue中的状态管理 vuex store
查看>>
Maven之阿里云镜像仓库配置
查看>>