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 直接安裝,在做相應操作
