• C#Word上传和转成Pdf实现


    今天的功能是前面的文章《链接: Vue根据word模板导出页面所需文档》的延续。当时OA系统中为了方便,所以可以在系统中自动生成合同、报价单等等,代码和前端生成文档功能可以在前面的文章中查看。如今有客户看上了我们公司的OA系统,他们提了一点意见,所以如今又对之前写的OA老代码增添了点功能。

    系统中同步生成

    原来的功能是在前端填写完信息后点击生成会自动下载合同、报价单等Word文档,并不会在OA系统中生成,所以这次优化的地方就是当生成Word时,系统中需要添加相对应的申请,例如合同申请信息,报价单申请信息等。
    这个功能实现起来还是比较简单的,无非是个添加功能。

    [HttpPost]
            [Route("addHt")]
            public async Task<IActionResult> addHt([FromBody] Ht jsonData)
            {            
                try
                {
                    fsql.Transaction(() => {
                        LOG_HT_GD lOG_HT_GD = new LOG_HT_GD()
                        {
                            htorder = jsonData.jsfwOrder,
                            a = jsonData.a,
                            b = jsonData.b,
                            sqwhy = jsonData.sqwhy,
                            username = jsonData.username,
                            qddate = DateTime.Now,
                            qdje = jsonData.totalPrice,
                            spdate = "待审批",
                            dome1 = "1",
                            dome3 = jsonData.demo3
                        };
                        var tj = fsql.Insert(lOG_HT_GD).ExecuteAffrows();
                        if (tj <= 0) throw new Exception("合同添加失败!");
                        foreach (var item in jsonData.list)
                        {
                            LOG_ContractDetails_GD lOG_ContractDetails_GDS = new LOG_ContractDetails_GD()
                            {
                                HTID = item.id,
                                Name = item.project,
                                Szno = item.munit,
                                Quantity = item.quant,
                                Munit = item.unit,
                                UnitPrice = item.unitPrice,
                                SumPrice = item.totalPrice,
                                Demo1 = item.remark,
                                Demo2 = item.Demo2,
                                Demo3 = item.Demo3
                            };
                            var addRows = fsql.Insert(lOG_ContractDetails_GDS).ExecuteAffrows();
                            if (addRows <= 0) throw new Exception("明细表添加失败!");
                        }
                    });
                    return Ok(new { code = 0, msg = "合同添加成功!" });
                }
                catch (Exception ex)
                {
                    return Ok(new { code = 1, msg = ex.Message });
                }
    
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    文件上传功能

    当前端Word后传给后端,这是个文件上传功能。

            #region 文件上传
            / 
            / 上传文件
            / 
            / 
            / 
            //[HttpPost]
            //[Route("PostFile")]
            //public String PostFile([FromForm] IFormCollection formCollection)
            //{
            //    try
            //    {
            //        String result = "Fail";
            //        string guid = "";
            //        FormFileCollection fileCollection = (FormFileCollection)formCollection.Files;
            //        foreach (IFormFile file in fileCollection)
            //        {
            //            StreamReader reader = new StreamReader(file.OpenReadStream());
            //            String content = reader.ReadToEnd();
            //            String name = file.FileName;//文件名
            //            string str = Path.GetFileNameWithoutExtension(name);//只取文件名即合同编号
            //            var Guid = fsql.Select().Where(x => x.htorder == str).First();
            //            guid = Guid.guid;
            //            //String filename = @"D:/Test/" + name;
            //            //string filename = "D:\\OA-FilesServer\\images\\" + name;
            //            if (!Directory.Exists($@"{this.filesType.FileBaseUrl}\{guid}"))
            //            {
            //                Directory.CreateDirectory($@"{this.filesType.FileBaseUrl}\{guid}");
            //            }
            //            using (FileStream fs = System.IO.File.Create($@"{this.filesType.FileBaseUrl}\{guid}\{name}"))//注意路径里面最好不要有中文
            //            {
            //                file.CopyTo(fs);//将上传的文件文件流,复制到fs中
            //                fs.Flush();//清空文件流
    
            //                var Rows = fsql.Update().Set(x => x.dome6 == str+".pdf")
            //                    .Where(x => x.htorder == str).ExecuteAffrows();
            //                if (Rows > 0)
            //                {
            //                    result = "0";
            //                }
            //            }               
            //        }
            //        return result;
            //    }
            //    catch (Exception ex)
            //    {
            //        return ex.Message;
            //    }
            //}
            #endregion
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    这个功能因为前端不知道怎么传给后端,所以我给注释掉了。这个文件上传的功能是可以直接拿来用的,也相当于一个轮子吧,拿过之后需要改一下路径和文件名等基本信息就可以使用。

    Word转Pdf

    原来的流程是当前端生成Word文档后,会把信息传给后端,我接到数据后添加到申请表中,然后把前端传过来的Word文档放到我们设定的文件夹中。
    然后再执行把Word转成Pdf的操作,这里我推荐使用第三方包Aspose,不过使用这个包是收费的,试用版的生成的Pdf有水印和有页数限制。代码比较简单,但是难点终于怎么解决水印,解决水印的方法我查了大半夜,网上很多方法都不行,包括有一些key都是过期的,转出来的还是有水印,最终我借同事的会员下了一个Aspose破解版的Dll文件。文件我放到链接: lC#Wrod转Pdf,只需要5积分,亲测可用。
    代码的话如下:

    #region Word转成PDF
            //[HttpGet]
            //[Route("WordToPdf")]
            //public async Task WordToPdf(string guid)
            //{
            //    try
            //    {
            //        string str = Path.GetFileNameWithoutExtension(fileName);//只取文件名即合同编号
            //        var Guid = fsql.Select().Where(x => x.htorder == str).First();
            //        string guid = Guid.guid;
            //        string wordPath = $@"{this.filesType.FileBaseUrl}\{guid}\{}";
            //        string pdfPath = "D:\\OA-FilesServer\\images\\" + str + ".pdf";
            //        //打开word文件
            //        Aspose.Words.Document doc = new Aspose.Words.Document(wordPath);
            //        //验证参数
            //        if (doc == null) { throw new Exception("Word文件无效"); }
    
            //        doc.Save(pdfPath, Aspose.Words.SaveFormat.Pdf);//以pdf格式保存至指定路径
            //        var Rows = fsql.Update().Set(x => x.dome6 == str + ".pdf")
            //                       .Where(x => x.htorder == str).ExecuteAffrows();
            //        return Ok(new { code = 0, msg = "转换成功!" });
            //    }
            //    catch (Exception ex)
            //    {
            //        return Ok(new { code = 1, msg = ex.Message });
            //    }
            //}    
            #endregion
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    上面的有业务干扰,放个纯净版代码:

    public void WordToPdfS(string wordPath,string pdfPath)
            {
                //打开word文件
                Aspose.Words.Document doc = new Aspose.Words.Document(wordPath);
                //验证参数
                if (doc == null) { throw new Exception("Word文件无效"); }
                doc.Save(pdfPath, Aspose.Words.SaveFormat.Pdf);//以pdf格式保存至指定路径
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    三行代码就解决,不愧是第三方收费包,这里我用的是破解版的dll文件,我也咨询了一下使用key,可以直接联系厂家来要key,不过只能使用30天。昨天晚上找资源找到12点多,发现大多博主给的解决办法都无效,目前来看解决水印的办法就是使用破解版的包或者使用试用版的key。

  • 相关阅读:
    71.【Java.哈希表(散列表)】
    06:HAL----定时器
    三生随记——茶叶的诅咒
    事件驱动API架构的五个协议
    MySQL 教程(一)概述
    java计算机毕业设计疫情下图书馆管理系统源程序+mysql+系统+lw文档+远程调试
    删除word最后一页之后的空白页
    命令行解析器浅解
    CRM系统的客户细分有什么作用?
    疯狂Spring Boot讲义[推荐1]
  • 原文地址:https://blog.csdn.net/qq_43434929/article/details/128163304