c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
2015-05-08 15:08
791 查看
由于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; } } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- 用C#操作office组件库实现word文档的创建及文字 表格 图片的插入
- C# 操作Word书签(二)——插入图片、表格到书签;读取、替换书签
- c#操作word 在指定书签插入文字或者图片
- c#操作word 在指定书签插入文字或者图片(转)
- c#操作word 在指定书签插入文字或者图片
- Aspose.Word 操作word复杂表格 拆分单元格 复制行 插入行 文字颜色
- c#之Word基本操作----创建,打开,绘制表格,插入图片等
- c#操作word模板插入文字、图片及表格
- delphi操作word基类,插入表格,分页符,日期,页码,替换,图片
- C# 操作word 指定书签位置插入图片并设置图片大小和位置
- c#操作在word指定书签插入文字
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- C#操作word模板插入文字、图片及表格详细步骤
- delphi操作word基类,插入表格,分页符,日期,页码,替换,图片
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- C# :Word 标签替代文字或图片 (图片浮于文字之上 插入后回车或换行符替换的解决方法)
- Aspose.Word 操作word复杂表格 拆分单元格 复制行 插入行