public static string CreateWord()
{
//**********************************************
//来自博客http://blog.csdn.net/fujie724
//**********************************************
object oMissing = System.Reflection.Missing.Value;
//创建一个Word应用程序实例
Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
//设置为不可见
oWord.Visible = false;
//模板文件地址,这里假设在X盘根目录 模板word 位置
object oTemplate = "d://采购单生成表.docx";
//以模板为基础生成文档
Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
//声明书签数组
object[] oBookMark = new object[11];
//赋值书签名 //获取模板 指定书签
oBookMark[0] = "MerchantName";
oBookMark[1] = "CustomerName";
oBookMark[2] = "SHR";
oBookMark[3] = "PZS";
oBookMark[4] = "Address";
oBookMark[5] = "CGDNO";
oBookMark[6] = "Phone";
oBookMark[7] = "NO";
oBookMark[8] = "CreateTime";
oBookMark[9] = "Table"; //赋值任意数据到书签的位置
oDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = "新华瀚品";
oDoc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = "李四";
oDoc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = "李四";
oDoc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = "5";
oDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = "贺州平山开路区33号";
oDoc.Bookmarks.get_Item(ref oBookMark[5]).Range.Text = "CGD123456789";
oDoc.Bookmarks.get_Item(ref oBookMark[6]).Range.Text = "15832665267";
oDoc.Bookmarks.get_Item(ref oBookMark[7]).Range.Text = "A00001";
oDoc.Bookmarks.get_Item(ref oBookMark[8]).Range.Text = "2018/01/01"; Word.Range range = oDoc.Bookmarks.get_Item(ref oBookMark[9]).Range;
Word.Table textTable = oDoc.Tables.Add(range, 3, 8, ref oMissing, ref oMissing); /**表格 水平居中**/
oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
/**表格 外边线**/
textTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; /**设置 表格外边线 行**/
for (int k = 1; k <= 3; k++)
{
textTable.Rows[k].Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
}
/**设置 表格外边线 列**/
for (int k = 1; k <= 8; k++)
{
textTable.Columns[k].Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
if (k == 1)
{
textTable.Columns[k].Width = 40f;
}
} /**垂直居中**/
object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
object countjz = 1;
oWord.Selection.MoveEnd(ref unit, ref countjz);
textTable.Select();
oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 /**设置单元格表头**/
textTable.Cell(1, 1).Range.Text = "序号";
textTable.Cell(1, 1).Merge(textTable.Cell(2, 1));//合并得话 是在这里操作
textTable.Cell(1, 2).Range.Text = "条形码";
textTable.Cell(1, 2).Merge(textTable.Cell(2, 2));
textTable.Cell(1, 3).Range.Text = "品名";
textTable.Cell(1, 3).Merge(textTable.Cell(2, 3));
textTable.Cell(1, 4).Range.Text = "规格";
textTable.Cell(1, 4).Merge(textTable.Cell(2, 4));
textTable.Cell(1, 5).Range.Text = "计量单位";
textTable.Cell(1, 5).Merge(textTable.Cell(2, 5));
textTable.Cell(1, 6).Range.Text = "数量";
textTable.Cell(1, 6).Merge(textTable.Cell(2, 6));
textTable.Cell(1, 7).Range.Text = "发货价";
textTable.Cell(1, 7).Merge(textTable.Cell(2, 7));
textTable.Cell(1, 8).Range.Text = "备注";
textTable.Cell(1, 8).Merge(textTable.Cell(2, 8)); textTable.Rows.Select();
/**设置单元格样式 行**/
Word.Range rngCell = null;
for (int i = 1; i <= 8; i++)
{
textTable.Cell(1, i).Height = 20f;//设置 行高
rngCell = textTable.Cell(1, i).Range;
rngCell.Font.Bold = 1;
rngCell.Font.Name = "微软雅黑";
rngCell.Font.Size = 10.5f;
} /**添加 数据行**/
for (int i = 2; i < 3; i++)
{
textTable.Cell(i, 1).Range.Text = (i-1).ToString();
textTable.Cell(i, 2).Range.Text = "1";
textTable.Cell(i, 3).Range.Text = "1";
textTable.Cell(i, 4).Range.Text = "1";
textTable.Cell(i, 5).Range.Text = "1";
textTable.Cell(i, 6).Range.Text = "1";
textTable.Cell(i, 7).Range.Text = "1";
textTable.Cell(i, 8).Range.Text = "1";
} //保存生成的Word
object filename = "d://测试.docx";// 要 绝对路径 保存的位置
oDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
//关闭word
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
return ""; }
using Word = Microsoft.Office.Interop.Word;  声明方便使用

  

  

这个 之前需要 增加一个 word  模板 放到指定得位置 并在相应节点添加 书签   结合到  增加表单操作, 需要添加 下面得dll

作者注释: 这种方法  必须在服务器上安装 office 套装 才能完成 word 操作(安装需要掏钱得), 可以用 NPOI  相关插件,

直接项目中  管理 NuGet程序包    中搜索 NPOI 直接安装,在做相应操作

最新文章

  1. spring功能总结
  2. C++嵌套多个命名空间举例
  3. App_Offline.htm 功能
  4. mongodb学习3---mongo的MapReduce
  5. YTU 2296: KMP模式匹配 二(串)
  6. SPOJ #692. Fruit Farm
  7. 【转】VC6.0附带小工具软件一览
  8. PHP中0、空、null和false的总结
  9. 在node.js中使用ejs的demo 第五篇
  10. POJ 3301 Texas Trip
  11. SVN插件
  12. Mac上使用selenium自动运行chrome
  13. Task的在主线程处理异常信息的Helper类
  14. 008_使用pyenv进行py开发环境管理
  15. python之数据类型与变量
  16. 【网络】高性能网络编程--下一个10年,是时候考虑C10M并发问题了
  17. JPA注解@SecondaryTables 实现一个实体映射多张数据库表
  18. 浅析若干Java序列化工具【转】
  19. [CodeForces850C]Arpa and a game with Mojtaba
  20. Spyder5 &amp; 显示器校准 &amp; 色彩校准

热门文章

  1. PHP中new static()与new self()的区别异同
  2. Python汉英/英汉翻译(百度API/有道API)
  3. EUI组件之DataGroup
  4. [shell]用shell脚本将本地文件夹与ftp上的文件夹同步
  5. 170529、springMVC 的工作原理和机制
  6. 表格table列宽度控制&lt;colgroup&gt;
  7. jenkins之配置git认证方式
  8. 徐州网络赛G-Trace【线段树】
  9. Andrew Ng机器学习公开课笔记 -- Logistic Regression
  10. 洛谷P2325王室联邦 SCOI2005 构造+树上分块