Aspose.Words生成报告

1. 引入Aspose.Words.dll


2. 

a.创建一个word模版,在需要动态读取数据的地方插入书签,书签默认不显示,可在word设置中设置为显示;

Aspose.Words生成报告_第1张图片


b.单行文本可设置域做替换,案例引用:http://my.oschina.net/dancefires/blog/217858?fromerr=LCF2VyNJ

Aspose.Words生成报告_第2张图片


3. C#代码插入文本

string tmppath = Server.MapPath("~/test.docx");//读取模版word
string appImg = Server.MapPath("~/Admin/Project/Ajax/image/" + DatasetTableName + "/Appchart.png");//读取要插入的图片
DataTable AppDateTable = GetAnalysisData(DatasetTableName, "AppDate", 15, "15", "国别", "line");//获取分析结果数据
Document doc = new Document(tmppath); //载入模板
DocumentBuilder builder = new DocumentBuilder(doc);
printTablefmat(builder, AppDateTable , appImg , "IPCBu", doc, 100);//调用插入表格和图片的方法
String[] fieldNames = new String[] { "UserName", "CreateTime" };//单行文本录入,此处对应的是模版中设置的域<span style="color: rgb(51, 51, 51); font-family: 'Microsoft YaHei', Verdana, sans-serif, 宋体; font-size: 13px; letter-spacing: 0.5px; line-height: 22.5px;">MergeField</span>
Object[] fieldValues = new Object[] { User.Identity.Name, DateTime.Now.ToShortDateString() };//定义录入的值
doc.MailMerge.Execute(fieldNames, fieldValues); //执行录入
doc.Save(DatasetTableName+"报告.doc", SaveFormat.Doc, SaveType.OpenInWord, Response);//保存并下载

        /// <summary>
        /// 打印数据到word表格
        /// </summary>
        /// <param name="builder">DocumentBuilder builder = new DocumentBuilder(doc);</param>
        /// <param name="dt">数据表</param>
        /// <param name="img">插入的图片</param>
        /// <param name="mark">word中设置的标签</param>
        /// <param name="doc">Document doc = new Document(tmppath); //载入的模板</param>
        /// <param name="width">单元格宽度</param>
        public void printTablefmat(DocumentBuilder builder, DataTable dt, string img, string mark, Document doc,int width)
        {
            builder.MoveToBookmark(mark); //开始添加值

            builder.StartTable();

            builder.RowFormat.Alignment = RowAlignment.Center;
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.CellFormat.Borders.Color = Color.Black;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.Bold = true;
            for (var i = 0; i < dt.Columns.Count; i++)
            {
                builder.InsertCell();
                builder.CellFormat.Width = width;
                builder.Write(dt.Columns[i].ColumnName.ToString());
                builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            }
            builder.EndRow();

            //开始添加值
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                for (var j = 0; j < dt.Columns.Count; j++)
                {
                    builder.InsertCell();// 添加一个单元格                    
                    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                    builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
                    builder.CellFormat.Width = width;
                    builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
                    builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
                    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中对齐
                    builder.Write(dt.Rows[i][j].ToString());
                }
                builder.EndRow();
            }
            builder.Writeln(" ");
            builder.Writeln(" ");
            builder.Writeln(" ");
            builder.InsertImage(img, 450, 250);
            doc.Range.Bookmarks[mark].Text = ""; // 清掉标示 
        }


你可能感兴趣的:(C#,aspose.words)