c#呼叫Aspose.Word元件操作word 插入文字/圖片/表格 書籤替換套打
阿新 • • 發佈:2019-02-03
由於NPOI暫時沒找到書籤內容替換功能,所以換用Apose.Word元件.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Aspose.Words; using Aspose.Words.Drawing; namespace WordNPOI { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { try { Utils.ExcelDataTableConverter edc = new Utils.ExcelDataTableConverter("60歲以上人員.xlsx"); DataTable dt = edc.ExcelToDataTable("sheet1", true); int rowCount = dt.Rows.Count; int columnCount = dt.Columns.Count; Aspose.Words.Document doc = new Aspose.Words.Document("TEMPLATE.DOCX"); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc); builder.MoveToBookmark("BK001"); builder.StartTable();//開始畫Table builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; // RowAlignment.Center; string str = string.Empty; builder.RowFormat.Height = 20; //新增列頭 for (int i = 0; i < columnCount; i++) { builder.InsertCell(); //Table單元格邊框線樣式 builder.CellFormat.Borders.LineStyle = LineStyle.Single; //Table此單元格寬度 builder.CellFormat.Width = 600; //此單元格中內容垂直對齊方式 builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; //字型大小 builder.Font.Size = 10; //是否加粗 builder.Bold = true; //向此單元格中新增內容 builder.Write(dt.Columns[i].ColumnName); } builder.EndRow(); //新增每行資料 for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { str = dt.Rows[i][j].ToString(); //http://www.cnblogs.com/geovindu/p/4106418.html //http://www.cnblogs.com/wuhuacong/archive/2012/08/30/2662961.html //插入Table單元格 builder.InsertCell(); //Table單元格邊框線樣式 builder.CellFormat.Borders.LineStyle = LineStyle.Single; //Table此單元格寬度 跟隨列頭寬度 //builder.CellFormat.Width = 500; //此單元格中內容垂直對齊方式 builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; //字型大小 builder.Font.Size = 10; //是否加粗 builder.Bold = false; //向此單元格中新增內容 builder.Write(str); } //Table行結束 builder.EndRow(); } builder.EndTable(); //doc.Range.Bookmarks["BK001"].Text = ""; // 清掉標示 doc.Range.Bookmarks["BK002"].Text = "標題"; <span style="color:#ff6666;">//替換書籤內容</span> //Shape shape = new Shape(doc, ShapeType.Image); //shape.ImageData.SetImage("1.png"); //shape.Width = 600; //shape.Height = 400; //shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center; if (doc.Range.Bookmarks["BK003"] != null) { //builder.InsertNode(shape); //這種圖片會把後面的內容蓋掉 builder.MoveToBookmark("BK003"); var img = builder.<span style="color:#ff6666;">InsertImage</span>("1.png"); img.Width = 300; img.Height = 300; img.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center; } string saveDocFile = "1.DOCX"; doc.Save(saveDocFile); if (MessageBox.Show("儲存成功,是否開啟檔案?", "", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { System.Diagnostics.Process.Start(saveDocFile); } } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } } }