数据导出之winfrom导出word(二)
程序员文章站
2022-04-04 16:29:03
本篇文章介绍了根据word模板导出word文档的方法。 一、获取模板地址 二、通过模板创建新文档 三、模板书签处插入值 四、选择保存位置 五、自动保存文档 六、替换为word中选择框 七、导出时需要默认打开文件,由用户自己保存 ......
本篇文章介绍了根据word模板导出word文档的方法。
一、获取模板地址
1 WordDocFileHelper WordTem = new WordDocFileHelper(); 2 string path = @"TempleteWord\ReportRepair.dotx"; 3 string fullName = System.Windows.Forms.Application.StartupPath.Substring(0, System.Windows.Forms.Application.StartupPath.LastIndexOf("\\")); 4 fullName = fullName.Substring(0, fullName.LastIndexOf("\\")) + "\\" + path;
二、通过模板创建新文档
WordTem.CreateNewDocument1(fullName);
1 public void CreateNewDocument1(string filePath) 2 { 3 wordApp = new ApplicationClass();//创建一个word应用程序实例 4 wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; 5 wordApp.Visible = false;//设置为不可见 6 object missing = System.Reflection.Missing.Value; 7 object format = WdSaveFormat.wdFormatDocument; 8 object templateName = filePath;//模板文件地址 9 wordDoc = wordApp.Documents.Add(ref templateName, ref missing,ref missing, ref missing); 10 }
三、模板书签处插入值
WordTem.InsertValue("XMMC", ID);
1 public bool InsertValue(string bookmark, string value) 2 { 3 object bkObj = bookmark; 4 if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark)) 5 { 6 wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select(); 7 wordApp.Selection.TypeText(value); 8 return true; 9 } 10 return false; 11 }
四、选择保存位置
1 FolderBrowserDialog path = new FolderBrowserDialog(); 2 path.Description = "选择需要保存的文件夹"; 3 path.ShowDialog(); 4 SaveFilePath = path.SelectedPath;
五、自动保存文档
WordTem.SaveDocument(SaveFilePath + "\\" + name + ".doc");
1 public void SaveDocument(string Filename) 2 { 3 object fileName = Filename; 4 object format = WdSaveFormat.wdFormatDocument;//保存格式 5 object miss = System.Reflection.Missing.Value; 6 wordDoc.SaveAs(ref fileName, ref format, ref miss, 7 ref miss, ref miss, ref miss, ref miss, 8 ref miss, ref miss, ref miss, ref miss, 9 ref miss, ref miss, ref miss, ref miss, 10 ref miss); 11 //关闭wordDoc,wordApp对象 12 object SaveChanges = WdSaveOptions.wdSaveChanges; 13 object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; 14 object RouteDocument = false; 15 wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); 16 wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); 17 }
六、替换为word中选择框
1 public string ReplaceCheckCell(string text) 2 { 3 wordApp.Selection.Find.Replacement.Font.Name = "Wingdings 2"; 4 //这里设置字体类型.Wingdings 2字体下的\u0052代表打钩的选择框,\u00A3代表未打钩的选择框 5 string Normal = "\u0052正常\u00A3不正常"; 6 string UnNormal = "\u00A3正常\u0052不正常"; 7 if (text == "正常") 8 { 9 wordApp.Selection.Find.Replacement.Text = Normal; 10 return Normal; 11 } 12 else if (text == "不正常") 13 { 14 wordApp.Selection.Find.Replacement.Text = UnNormal; 15 return UnNormal; 16 } 17 else { return ""; } 18 }
七、导出时需要默认打开文件,由用户自己保存
1 public void CreateNewDocument0(string filePath, string filename) 2 { 3 //killWinWordProcess(); 4 wordApp = new ApplicationClass();//创建一个word应用程序实例 5 wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; 6 wordApp.Visible = false;//设置为不可见 7 object missing = System.Reflection.Missing.Value; 8 object format = WdSaveFormat.wdFormatDocument; 9 object fileName = filename; 10 object templateName = filePath;//模板文件地址 11 wordDoc = wordApp.Documents.Open(ref templateName, ref missing, 12 ref missing, ref missing, ref missing, ref missing, ref missing, 13 ref missing, ref missing, ref missing, ref missing, ref missing, 14 ref missing, ref missing, ref missing, ref missing); 15 16 //保存文档 17 wordDoc.SaveAs(ref fileName, ref format, ref missing, 18 ref missing, ref missing, ref missing, ref missing, 19 ref missing, ref missing, ref missing, ref missing, 20 ref missing, ref missing, ref missing, ref missing, 21 ref missing); 22 }
1 public void OpenDocument(string Filename) 2 { 3 object fileName = Filename; 4 object Missing = System.Reflection.Missing.Value; 5 object readOnly = true; 6 wordDoc = wordApp.Documents.Open(ref fileName, ref Missing, ref readOnly, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing); 7 ////wordApp.Visible = true; 8 ////wordDoc.PrintPreview();//打印预览 9 }
推荐阅读
-
C# 大数据导出word的假死报错的处理方法
-
C#实现将数据导出到word或者Excel中的方法
-
asp.net实现导出DataTable数据到Word或者Excel的方法
-
C# 大数据导出word的假死报错的处理方法
-
MariaDB/MySQL备份和恢复(二):数据导入、导出
-
oracle数据库导出和oracle导入数据的二种方法(oracle导入导出数据)
-
asp.net实现导出DataTable数据到Word或者Excel的方法
-
C#实现将数据导出到word或者Excel中的方法
-
winfrom数据导出
-
C#_.NetCore_WebAPI项目_EXCEL数据导出(ExcelHelper_第二版_优化逻辑)