public static void Export(string wordTemplatePath, string newFileName, Dictionary<string, string> wordLableList)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
string TemplateFile = wordTemplatePath;
File.Copy(TemplateFile, newFileName);
_Document doc = new Microsoft.Office.Interop.Word.Document();
object obj_NewFileName = newFileName;
object obj_Visible = false;
object obj_ReadOnly = false;
object obj_missing = System.Reflection.Missing.Value;
doc = app.Documents.Open(ref obj_NewFileName, ref obj_missing, ref obj_ReadOnly, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_Visible,
ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing);
doc.Activate();
if (wordLableList.Count > 0)
{
object what = WdGoToItem.wdGoToBookmark;
foreach (var item in wordLableList)
{
object lableName = item.Key;
if (doc.Bookmarks.Exists(item.Key))
{
doc.ActiveWindow.Selection.GoTo(ref what, ref obj_missing, ref obj_missing, ref lableName);//光标移动书签的位置
doc.ActiveWindow.Selection.TypeText(item.Value);//在书签处插入的内容
doc.ActiveWindow.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置插入内容的Alignment
}
}
}
foreach (Bookmark bk in doc.Bookmarks)
{
if (bk.Name == "创伤图片")
{
bk.Select();
Selection sel = app.Selection;
string picture = string.Format("{0}Images\\创伤分析图片.png", Path.GetFullPath(Path.Combine(RunningPath, @"..\..\")));
sel.InlineShapes.AddPicture(picture);
}
}
object obj_IsSave = true;
doc.Close(ref obj_IsSave, ref obj_missing, ref obj_missing);
}
if (File.Exists(wordTemplatePath))
{
Dictionary<string, string> wordLableList = new Dictionary<string, string>();
wordLableList.Add("姓名", "钟飞");
wordLableList.Add("性别", "男");
wordLableList.Add("生日", "1997年4月1日");
wordLableList.Add("病人编号", "1009");
wordLableList.Add("放射编号", "8669");
wordLableList.Add("检查时间", "2023/4/14 10:06:09");
wordLableList.Add("报告时间", "2023/4/14 10:09:11");
wordLableList.Add("检查部位", "左腕 前后位");
wordLableList.Add("检查所见", "左手手腕骨质不连续,断端移位不明显,周围软组织肿胀;余骨质未见明确骨折现象。");
wordLableList.Add("诊断意见", "左手手腕骨折。");
Export(wordTemplatePath, newFileName, wordLableList);
MessageBox.Show("导出成功!");
}
后面添加转pdf操作
public static void Export(string wordTemplatePath, string newFileName, Dictionary<string, string> wordLableList,string pdfpath)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
string TemplateFile = wordTemplatePath;
File.Copy(TemplateFile, newFileName);
_Document doc = new Microsoft.Office.Interop.Word.Document();
object obj_NewFileName = newFileName;
object obj_Visible = false;
object obj_ReadOnly = false;
object obj_missing = System.Reflection.Missing.Value;
doc = app.Documents.Open(ref obj_NewFileName, ref obj_missing, ref obj_ReadOnly, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_Visible,
ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing);
doc.Activate();
if (wordLableList.Count > 0)
{
object what = WdGoToItem.wdGoToBookmark;
foreach (var item in wordLableList)
{
object lableName = item.Key;
if (doc.Bookmarks.Exists(item.Key))
{
doc.ActiveWindow.Selection.GoTo(ref what, ref obj_missing, ref obj_missing, ref lableName);//光标移动书签的位置
doc.ActiveWindow.Selection.TypeText(item.Value);//在书签处插入的内容
doc.ActiveWindow.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置插入内容的Alignment
}
}
}
foreach (Microsoft.Office.Interop.Word.Bookmark bk in doc.Bookmarks)
{
if (bk.Name == "创伤图片")
{
bk.Select();
Selection sel = app.Selection;
string picture = string.Format("{0}Images\\创伤分析图片.png", Path.GetFullPath(Path.Combine(RunningPath, @"..\..\")));
sel.InlineShapes.AddPicture(picture);
}
}
object obj_IsSave = true;
doc.Close(ref obj_IsSave, ref obj_missing, ref obj_missing);
doc = app.Documents.Open(ref obj_NewFileName, ref obj_missing, ref obj_ReadOnly, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing, ref obj_missing, ref obj_missing, ref obj_Visible,
ref obj_missing, ref obj_missing, ref obj_missing,
ref obj_missing);
doc.Activate();
doc.ExportAsFixedFormat(pdfpath, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportFromTo);
obj_IsSave = true;
doc.Close(ref obj_IsSave, ref obj_missing, ref obj_missing);
}