当前位置: 首页 > news >正文

wpf .netcore 导出docx文件


下载nuget包  DocX

static int ColShowCount = 12;using var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK){var selectedPath = folderBrowserDialog.SelectedPath;string FileName = Path.Combine(selectedPath, $"{Protocol.Name}.docx");var fi = new FileInfo(FileName);if (fi.Directory != null && !fi.Directory.Exists){fi.Directory.Create();}if (File.Exists(FileName)){bool has = false;System.Windows.Application.Current.Dispatcher.Invoke(() =>{if (new IsDeleteWin("HasFile").ShowDialog() == false){has = true;}});if (has) { return; }}// 使用 WordprocessingDocument.Create 创建 Word 文档using WordprocessingDocument doc = WordprocessingDocument.Create(FileName, WordprocessingDocumentType.Document);// 添加主文档部分MainDocumentPart mainPart = doc.AddMainDocumentPart();// 创建一个空的文档主体mainPart.Document = new Document();Body body = mainPart.Document.AppendChild(new Body());// Add a Table into the document and sets its values.var prolist = new List<List<string>> { new List<string>() { "Name", xxx}, new List<string>() { "ime", xxxx} };DocxHelper.AddTableWithTitle(body, "", prolist);body.AppendChild(new Paragraph(new Run(new Text("")))); // 添加一个空行var doclist = new List<List<string>>();doclist.Add(new List<string>() { "1111", $"{111}" });doclist.Add(new List<string>() { "222", $"{222}" });doclist.Add(new List<string>() { "333", $"{333}" });DocxHelper.AddTableWithTitle(body, "Parameter", doclist);var list = Global.DataHelper.QueryList();if (list != null && list .Any()){// 添加段落分隔body.AppendChild(new Paragraph(new Run(new Text(""))));var conclist = new List<List<string>>();foreach (var c in list ){conclist.Add(new List<string>() { $"{c.xxx}", $"{c.xxxx}" });}if (conclist?.Count > 0)DocxHelper.AddTableWithTitle(body, "title.", conclist);}mainPart.Document.Save();}

DocxHelper.cs

internal class DocxHelper{public static void AddTableWithTitle(Body body, string title, List<List<string>> data){try{// 添加表格标题var titleParagraph = new Paragraph();var titleRun = new Run();// 设置标题样式var titleRunProp = new RunProperties();titleRunProp.AppendChild(new Bold());titleRunProp.AppendChild(new FontSize() { Val = "24" }); // 12pt字体titleRun.AppendChild(titleRunProp);titleRun.AppendChild(new Text(title));titleParagraph.AppendChild(titleRun);// 设置段落居中var titleParaProp = new ParagraphProperties();titleParaProp.AppendChild(new Justification() { Val = JustificationValues.Left });titleParagraph.AppendChild(titleParaProp);body.AppendChild(titleParagraph);//body.AppendChild(new Paragraph(new Run(new Text("")))); // 添加一个空行// 创建表格Table table = CreateTable(data);if (table != null){body.AppendChild(table);}}catch (System.Exception){throw;}}private static Table CreateTable(List<List<string>> data){var table = new Table();try{// 设置表格属性var tblProp = new TableProperties(new TableWidth() { Width = "9000", Type = TableWidthUnitValues.Dxa },new TableJustification() { Val = TableRowAlignmentValues.Left });// 设置表格边框为实线var tblBorders = new TableBorders(new TopBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size = 12,Color = "000000"},new BottomBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size = 12,Color = "000000"},new LeftBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size = 12,Color = "000000"},new RightBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size = 12,Color = "000000"},new InsideHorizontalBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size = 12,Color = "000000"},new InsideVerticalBorder(){Val = new EnumValue<BorderValues>(BorderValues.Single),Size = 12,Color = "000000"});tblProp.AppendChild(tblBorders);table.AppendChild(tblProp);// 创建表格行for (int i = 0; i < data.Count; i++){var tr = new TableRow();for (int j = 0; j < data[i].Count; j++){var tc = new TableCell();// 设置单元格属性var tcProp = new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa });// 如果是表头行,设置背景色//if (i == 0)//{//    tcProp.AppendChild(new Shading()//    {//        Val = ShadingPatternValues.Clear,//        Color = "auto",//        Fill = "D9D9D9"//    });//}tc.AppendChild(tcProp);// 创建段落和文本var para = new Paragraph();var run = new Run();// 如果是表头,设置粗体//if (i == 0)//{//    var runProp = new RunProperties();//    runProp.AppendChild(new Bold());//    run.AppendChild(runProp);//}run.AppendChild(new Text(data[i][j]));para.AppendChild(run);tc.AppendChild(para);tr.AppendChild(tc);}table.AppendChild(tr);}}catch (System.Exception){return null;throw;}return table;}public static void CreateTitile(Body body, string title){try{// 添加表格标题var titleParagraph = new Paragraph();var titleRun = new Run();// 设置标题样式var titleRunProp = new RunProperties();titleRunProp.AppendChild(new Bold());titleRunProp.AppendChild(new FontSize() { Val = "24" }); // 12pt字体titleRun.AppendChild(titleRunProp);titleRun.AppendChild(new Text(title));titleParagraph.AppendChild(titleRun);// 设置段落居中var titleParaProp = new ParagraphProperties();titleParaProp.AppendChild(new Justification() { Val = JustificationValues.Left });titleParagraph.AppendChild(titleParaProp);body.AppendChild(titleParagraph);}catch (System.Exception ex){LogHelper.GetSingleObj().WriteLog(ex);}}}


文章转载自:

http://6aIh7Jcm.jrdbq.cn
http://8DiP5WaI.jrdbq.cn
http://eyqjmnCg.jrdbq.cn
http://wwYpnYOj.jrdbq.cn
http://JsEfk0rU.jrdbq.cn
http://BJyLWQQF.jrdbq.cn
http://t30QDpS6.jrdbq.cn
http://0lt7CcV2.jrdbq.cn
http://nPy2k3UW.jrdbq.cn
http://S9wBhbgQ.jrdbq.cn
http://X47U1586.jrdbq.cn
http://d9KRSpML.jrdbq.cn
http://MH4qrLCX.jrdbq.cn
http://qBlunYqr.jrdbq.cn
http://vF8NIkES.jrdbq.cn
http://n16Dn0Xy.jrdbq.cn
http://7sYOXrVF.jrdbq.cn
http://4NqsYyY4.jrdbq.cn
http://9leMonA5.jrdbq.cn
http://aTBWDFQJ.jrdbq.cn
http://lk1gtjAQ.jrdbq.cn
http://cjfTHWK4.jrdbq.cn
http://YTtcpKd0.jrdbq.cn
http://mVuV4JpO.jrdbq.cn
http://uIIXUYfS.jrdbq.cn
http://2miwBoKP.jrdbq.cn
http://mrhtkE5N.jrdbq.cn
http://4jeEQVZG.jrdbq.cn
http://ehtfUTBx.jrdbq.cn
http://cexalFMQ.jrdbq.cn
http://www.dtcms.com/a/372954.html

相关文章:

  • 基于开源AI智能名片链动2+1模式S2B2C商城小程序的移动互联网人气氛围营造机制研究
  • 六级第一关——下楼梯
  • Bug排查日记的技术文章大纲-AI生成
  • CentOS/Ubuntu安装显卡驱动与GPU压力测试
  • wpf .netcore 导出pdf文件
  • 6个步骤实现Postman接口压力测试
  • Linux-expect脚本编程
  • Dart 聊天后端开发(MongoDB + WebSocket)
  • Linux初始——自动化构建
  • Linux之GDB调试
  • 通俗理解 LSTM 的三门机制:从剧情记忆到科学原理
  • MyBatis-Plus中 IService 与 ServiceImpl等内容的深入思考理解
  • Android使用ReactiveNetwork监听网络连通性
  • 大学信息查询平台:一个现代化的React教育项目
  • 基于 GitHub Actions 的零成本自动化部署:把 Vite/Vue3 项目一键发布到 GitHub Pages 的完整实战
  • 制造企业如何实现ERP/OA/CRM/WMS等多系统贯通
  • 2025年5月架构设计师案例分析真题回顾,附参考答案、解析及所涉知识点(五)
  • 【python面向对象编程】迭代器与生成器
  • 查验接口:筑牢游戏防沉迷系统的 “数字防线”
  • 从目标到优化设计:由 Stochos 和 GenAI 提供支持的 Web 应用程序生成
  • Easy ES技术详解
  • 【C++】C++11的包装器:function与bind简介
  • C++微基础备战蓝桥杯之旅
  • 解构服务于构建
  • 天津大学智算2026预推免机试第二批题目及代码c++
  • 杰理烧录ERROR: Data error after erasing, address = 0x430000
  • Spring Cloud Alibaba快速入门02-Nacos(下)
  • DA-WSOL
  • FlutterActivity vs FlutterFragmentActivity:全面对比与最佳实践
  • 算法高频题