一、操作Word 

  首先引用这个DLL,Microsoft.Office.Interop.Word,官方提供的。

  可以操作word文字,表格,图片等。

  文字通过替换关键字的方式实现

  document.Paragraphs[i].Range.Text = temptext.Replace("{$village}", "HELLO WORLD");

  表格可以自己获取模板中已有的表格

   Microsoft.Office.Interop.Word.Table table1 = document.Tables[1];

   table1.Cell(1, 1).Range.Text = "TEST1";

  也可以自己创建表格,可以设计表头,单元格等。

   int tableRow = 6 ;
int tableColumn = ;
//定义一个Word中的表格对象
Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs[i].Range,
tableRow, tableColumn, ref Nothing, ref Nothing);
   //默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
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
public bool CreateWord(DataTable dttmp)
{
bool result = false;
Object Nothing = Missing.Value;
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = null;
string path = @"C:\Users\Administrator\Desktop\BB\合同模版.doc";
object FileName = @"C:\Users\Administrator\Desktop\BB\" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".doc";
application.Visible = false;
document = application.Documents.Open(path); int rowNum = dttmp.Rows.Count;
for (int i = ; i <= document.Paragraphs.Count; i++)
{
string temptext = document.Paragraphs[i].Range.Text;
//以下为替换文档模版中的关键字
if (temptext.Contains("{$village}"))
document.Paragraphs[i].Range.Text = temptext.Replace("{$village}", "HELLO WORLD");
Microsoft.Office.Interop.Word.Table table1 = document.Tables[];
table1.Cell(, ).Range.Text = "TEST1";
if (temptext.Contains("{$Table1}"))
{
//设置表格的行数和列数
int tableRow = + rowNum;
int tableColumn = ;
//定义一个Word中的表格对象 Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs[i].Range,
tableRow, tableColumn, ref Nothing, ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable = ;//这个值可以设置得很大
table.Cell(, ).Merge(table.Cell(, ));//纵向合并
table.Cell(, ).Range.Text = "牌号/代码"; table.Cell(, ).Merge(table.Cell(, ));//纵向合并
table.Cell(, ).Range.Text = "标准编号"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "单重\n(MT)"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "张数"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "重量\n(MT))"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "单价\n(元/吨)"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "金额(人民币)"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "交货期"; table.Cell(, ).Merge(table.Cell(, ));//横向合并
table.Cell(, ).Range.Text = "规格(mm)";
table.Cell(, ).Range.Text = "厚度";
table.Cell(, ).Range.Text = "宽度";
table.Cell(, ).Range.Text = "宽度"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "表面加工";
table.Cell(, ).Range.Text = "边缘";
table.Cell(, ).Range.Text = "精度"; }
} object format = document.SaveFormat;
document.Save(); application.Quit(ref Nothing, ref Nothing, ref Nothing);
return result; }

二、Word导出PDF

  

 public bool WordToPDF(string sourcePath)
{
bool result = false;
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = null;
try
{
application.Visible = false;
document = application.Documents.Open(sourcePath);
string PDFPath = sourcePath.Replace(".doc", ".pdf");//pdf存放位置
if (!File.Exists(@PDFPath))//存在PDF,不需要继续转换
{
document.ExportAsFixedFormat(PDFPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
}
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
//document.Close();
}
return result;
}

服务器部署可以参考;https://www.cnblogs.com/5426z/articles/4865312.html

最新文章

  1. QtCreator下运行opencv出现realloc():pointer invalid
  2. java SpringUtil获取bean
  3. 161208、Java enum 枚举还可以这么用
  4. contentWindow 和contentDocument区别 及iframe访问
  5. WCF关于VS2010的配置
  6. 自适应rem布局
  7. 210. Course Schedule II
  8. Android TabHost 动态修改图标或者动态改变标题
  9. struts2启动报错com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1
  10. Keepass 2.x 之 同步与触发器
  11. 集腋成裘-13-git使用-02进阶篇
  12. php正则表达式 剔除字符串中 ,除了汉字的字符(只保留汉字) php 正则 只保留汉字,剔除所有符号
  13. 嵌套if-esle语句
  14. ThinkPHP学习笔记
  15. Scrapy爬虫框架的学习
  16. Unity之获取资源包的路径
  17. 【原创】贡献一个JS的弹出框代码...
  18. 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第二部分)
  19. DB安装
  20. C++11 并发之std::thread std::mutex

热门文章

  1. DEDECMS安全设置篇
  2. 【转】Pandas学习笔记(一)基本介绍
  3. nginx 缓存服务
  4. 关于Socket踩过的一些坑
  5. learning shell check requires root privileges
  6. pyhton2 and python3 生成随机数字、字母、符号字典(用于撞库测试/验证码等)
  7. json 字符串 反序列化
  8. PATA1055 The World&#39;s Richest (25 分)
  9. 深入js系列-类型(null)
  10. ES6解构赋值常见用法