利用word模板输出word文件并将文档显示在wpf窗口上
一、通过模板输出文档
添加对Microsoft.Office.Interop.Word库的引用,需要安装office软件
通过文本域对需要替换的数据进行代换
public static bool FillData(string MuBanFileName, string JSSFileName, Hashtable data) { try { //Document doc = copyWordDoc(MuBanFileName); File.Copy(MuBanFileName, JSSFileName); ReplaceWordDocAndSave( JSSFileName, data); } catch (Exception) { return false; } return true; } protected static void ReplaceWordDocAndSave( object savePath, Hashtable table) { object format = WdSaveFormat.wdFormatDocument; object readOnly = false; object isVisible = false; Object Nothing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass(); //Microsoft.Office.Interop.Word.Document oDoc = wordApp.Documents.Open(ref obj, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing); Microsoft.Office.Interop.Word.Document oDoc=null; try { oDoc = wordApp.Documents.Open(ref savePath, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object FindText, ReplaceWith, Replace; object MissingValue = Type.Missing; foreach (string str in table.Keys) { oDoc.Content.Find.Text = str; //要查找的文本 FindText = str; //替换文本 ReplaceWith = Convert.ToString(table[str]); Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; //移除Find的搜索文本和段落格式设置 oDoc.Content.Find.ClearFormatting(); oDoc.Content.Find.Execute(ref FindText, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref ReplaceWith, ref Replace, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue); } oDoc.SaveAs(ref savePath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); } catch (Exception) { } finally { oDoc.Close(ref Nothing, ref Nothing, ref Nothing); //关闭wordApp组件对象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); } }
二、通过documentview控件在wpf窗口中加载出来
实现方式是通过将word文档修改为xps文档,然后在documentView控件中加载
private void ConvertWordToXPS(string wordDocName) { FileInfo fi = new FileInfo(wordDocName); XpsDocument result = null; string xpsDocName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), fi.Name); xpsDocName = xpsDocName.Replace(".docx", ".xps").Replace(".doc", ".xps"); Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application(); try { wordApplication.Documents.Add(wordDocName); Document doc = wordApplication.ActiveDocument; doc.ExportAsFixedFormat(xpsDocName, WdExportFormat.wdExportFormatXPS, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateHeadingBookmarks, true, true, false, Type.Missing); result = new XpsDocument(xpsDocName, FileAccess.Read); } catch (Exception ex) { string error = ex.Message; wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges); MessageBox.Show(ex.Message); } wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges); documentViewer.Document = result.GetFixedDocumentSequence(); documentViewer.FitToWidth(); result.Close(); //File.Delete(xpsDocName); File.Delete(wordDocName); MessageBox.Show("计算完成"); //return result; }

浙公网安备 33010602011771号