zl程序教程

您现在的位置是:首页 >  其他

当前栏目

ABP word下载 模板替换 文件下载

文件模板下载ABP 替换 word
2023-09-11 14:22:26 时间

原文:
https://github.com/A-Grass-Code/OpenDemo/tree/main/Word模板文件的替换并生成新的Word文件

引用的包:DocX

代码


        public FileResult Download(int id)
        {
            string filename = $"DailyDiaryTemplate2-{DateTime.Now.ToString("HH_mm_ss_ffff")}.docx";
            string path1 = _hostingEnvironment.WebRootPath + "/TemplateFiles/Quality/DailyDiaryTemplate2.docx";
            string path2 = $"{_hostingEnvironment.WebRootPath}/TemplateFiles/Quality/"+ filename;

            WordTemplateReplace(path1, path2,
             new Dictionary<string, string>()
             {
                 ["ConstructionDate"] = "张三",
                 ["ConstructionTitle"] = "23",
                 ["WeatherCondition"] = "男",
             });

            System.Threading.Thread.Sleep(1000);//模拟延迟

            return  File($"/TemplateFiles/Quality/" + filename, "application/octet-stream", "施工日志.docx");// 返回文件
        }


        public bool WordTemplateReplace(string tempPath, string newWordPath, Dictionary<string, string> textDic)
        {
            try
            {
                var doc = DocX.Load(tempPath);  // 加载 Word 模板文件

                if (textDic != null && textDic.Count > 0)
                {
                    foreach (var paragraph in doc.Paragraphs)   // 遍历当前 Word 文件中的所有(段落)段
                    {
                        foreach (var texts in textDic)
                        {
                            try
                            {
                                paragraph.ReplaceText($"[={texts.Key}]", texts.Value);  // 替换段落中的文字
                            }
                            catch (Exception ex)
                            {
                                // 不处理
                                continue;
                            }
                        }
                    }

                    foreach (var table in doc.Tables)   // 遍历当前 Word 文件中的所有表格
                    {
                        foreach (var row in table.Rows) // 遍历表格中的每一行
                        {
                            foreach (var cell in row.Cells)     //遍历每一行中的每一列
                            {
                                foreach (var paragraph in cell.Paragraphs)  // 遍历当前表格里的所有(段落)段
                                {
                                    foreach (var texts in textDic)
                                    {
                                        try
                                        {
                                            paragraph.ReplaceText($"[={texts.Key}]", texts.Value);  // 替换段落中的文字
                                        }
                                        catch (Exception ex)
                                        {
                                            // 不处理
                                            continue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


                doc.SaveAs(newWordPath);

                return true;
            }
            catch (Exception ex)
            {
                // 不处理
                return false;
            }
        }



效果