一、操作Word
首先援用这个DLL,Microsoft.Office.Interop.Word,官方提供的。
能够操作word文字,表格,图片等。
文字经过交换关键字的方式完成
document.Paragraphs[i].Range.Text=temptext.Replace(“{$village}”,”HELLOWORLD”);
表格能够本人获取模板中已有的表格
Microsoft.Office.Interop.Word.Tabletable1=document.Tables[1];
table1.Cell(1,1).Range.Text=”TEST1″;
也能够本人创立表格,能够设计表头,单元格等。
inttableRow=6;
inttableColumn=6;
//定义一个Word中的表格对象
Microsoft.Office.Interop.Word.Tabletable=document.Tables.Add(document.Paragraphs[i].Range,
tableRow,tableColumn,refNothing,refNothing);
//默许创立的表格没有边框,这里修正其属性,使得创立的表格带有边框
table.Borders.Enable=1;
table.Cell(1,1).Merge(table.Cell(2,1));//纵向兼并
table.Cell(1,1).Range.Text=”牌号/代码”;
table.Cell(1,2).Merge(table.Cell(2,2));//纵向兼并
table.Cell(1,2).Range.Text=”规范编号”;

有一篇文章写的很细致能够参考下:https://www.cnblogs.com/xh6300/p/5915717.html
复制代码
publicboolCreateWord(DataTabledttmp)
{
boolresult=false;
ObjectNothing=Missing.Value;
Microsoft.Office.Interop.Word.Applicationapplication=newMicrosoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Documentdocument=null;
stringpath=@”C:UsersAdministratorDesktopBB合同模版.doc”;
objectFileName=@”C:UsersAdministratorDesktopBB”+DateTime.Now.ToString(“yyyyMMddHHmmssffffff”)+”.doc”;
application.Visible=false;
document=application.Documents.Open(path);
introwNum=dttmp.Rows.Count;
for(inti=1;i<=document.Paragraphs.Count;i++)
{
stringtemptext=document.Paragraphs[i].Range.Text;
//以下为交换文档模版中的关键字
if(temptext.Contains(“{$village}”))
document.Paragraphs[i].Range.Text=temptext.Replace(“{$village}”,”HELLOWORLD”);
Microsoft.Office.Interop.Word.Tabletable1=document.Tables[1];
table1.Cell(1,1).Range.Text=”TEST1″;
if(temptext.Contains(“{$Table1}”))
{
//设置表格的行数和列数
inttableRow=6+rowNum;
inttableColumn=13;
//定义一个Word中的表格对象
Microsoft.Office.Interop.Word.Tabletable=document.Tables.Add(document.Paragraphs[i].Range,
tableRow,tableColumn,refNothing,refNothing);
//默许创立的表格没有边框,这里修正其属性,使得创立的表格带有边框
table.Borders.Enable=1;//这个值能够设置得很大
table.Cell(1,1).Merge(table.Cell(2,1));//纵向兼并
table.Cell(1,1).Range.Text=”牌号/代码”;
table.Cell(1,2).Merge(table.Cell(2,2));//纵向兼并
table.Cell(1,2).Range.Text=”规范编号”;
table.Cell(1,6).Merge(table.Cell(2,6));
table.Cell(1,6).Range.Text=”单重n(MT)”;
table.Cell(1,7).Merge(table.Cell(2,7));
table.Cell(1,7).Range.Text=”张数”;
table.Cell(1,8).Merge(table.Cell(2,8));
table.Cell(1,8).Range.Text=”重量n(MT))”;
table.Cell(1,9).Merge(table.Cell(2,9));
table.Cell(1,9).Range.Text=”单价n(元/吨)”;
table.Cell(1,10).Merge(table.Cell(2,10));
table.Cell(1,10).Range.Text=”金额(钱)”;
table.Cell(1,13).Merge(table.Cell(2,13));
table.Cell(1,13).Range.Text=”交货期”;
table.Cell(1,3).Merge(table.Cell(1,5));//横向兼并
table.Cell(1,3).Range.Text=”规格(mm)”;
table.Cell(2,3).Range.Text=”厚度”;
table.Cell(2,4).Range.Text=”宽度”;
table.Cell(2,5).Range.Text=”宽度”;
table.Cell(1,9).Merge(table.Cell(1,10));
table.Cell(1,10).Range.Text=”外表加工”;
table.Cell(2,11).Range.Text=”边缘”;
table.Cell(2,11).Range.Text=”精度”;
}
}
objectformat=document.SaveFormat;
document.Save();
application.Quit(refNothing,refNothing,refNothing);
returnresult;
}
复制代码
二、Word导出PDF
复制代码
publicboolWordToPDF(stringsourcePath)
{
boolresult=false;
Microsoft.Office.Interop.Word.Applicationapplication=newMicrosoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Documentdocument=null;
try
{
application.Visible=false;
document=application.Documents.Open(sourcePath);
stringPDFPath=sourcePath.Replace(“.doc”,”.pdf”);//pdf寄存位置
if(!File.Exists(@PDFPath))//存在PDF,不需求继续转换
{
document.ExportAsFixedFormat(PDFPath,Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
}
result=true;
}
catch(Exceptione)
{
Console.WriteLine(e.Message);
result=false;
}
finally
{
//document.Close();
}
returnresult;
}
复制代码

1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com