码迷,mamicode.com
首页 > Web开发 > 详细

Aspose.Words导出图片 表格

时间:2017-09-29 19:38:24      阅读:815      评论:0      收藏:0      [点我收藏+]

标签:jpg   nes   pre   添加   alignment   shape   val   test   cal   

先定义一个WORD 模板, 然后替换文本、域 ,定位开始表格

单元格

技术分享
        /// <summary>
        /// 添加单元格
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="data"></param>
        /// <param name="width"></param>
        public static void InsertCell(DocumentBuilder builder, string data, double width)
        {
            builder.InsertCell();
            builder.RowFormat.Height = 22;
            builder.CellFormat.Width = width;
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
            builder.CellFormat.VerticalMerge = CellMerge.None;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中
            builder.Font.Size = 8;
            //builder.Font.Bold = true;
            builder.Write(data);
        }
View Code

图片单元格

技术分享
        /// <summary>
        /// 添加带图片的单元格
        /// </summary>
        /// <param name="doc">word文档</param>
        /// <param name="builder"></param>
        /// <param name="strUrl">图片路径</param>
        /// <param name="width">单元格宽度</param>
        /// <param name="height">单元格高度</param>
        /// <param name="tableIndex">表索引</param>
        /// <param name="rowIndex">行索引</param>
        /// <param name="columnIndex">列索引</param>
        private static void InsertCellWithImage(Document doc, DocumentBuilder builder, string strUrl, double width, double height, int rowIndex, int columnIndex)
        {
            builder.InsertCell();
            builder.RowFormat.Height = height;
            builder.CellFormat.Width = width;
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;    // 垂直居中
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;  // 水平居中
            if (File.Exists(HttpContext.Current.Server.MapPath(strUrl)))
            {
                // 向此单元格中插入图片
                Shape shape = new Shape(doc, ShapeType.Image);
                shape.ImageData.SetImage(HttpContext.Current.Server.MapPath(strUrl));
                shape.Width = width - 5;
                shape.Height = height-5;
                shape.HorizontalAlignment = HorizontalAlignment.Center;
                shape.VerticalAlignment = VerticalAlignment.Center;
                builder.InsertNode(shape);
            }
        }
View Code

表格--书签位置

技术分享
            DocumentBuilder builder = new DocumentBuilder(doc);            
            builder.MoveToBookmark("table"); //移动到书签
            builder.StartTable();
            builder.CellFormat.Shading.BackgroundPatternColor = Color.Yellow;

            InsertCell(builder, "测试", 235);
            builder.EndRow();
            builder.CellFormat.Shading.BackgroundPatternColor = Color.White;
            builder.EndTable();            
View Code

替换:  插入-文本部件-域

技术分享
            string[] fieldNames = {  "Test01""Test02"};
            object[] fieldValues = {  "Test01""Test02"};
            fieldValues[0]="测试01";
            fieldValues[1]="测试02";            
View Code

 

技术分享
            doc.MailMerge.Execute(fieldNames, fieldValues);
            doc.MailMerge.DeleteFields();  


            string strPhysicsPath = HttpContext.Current.Server.MapPath(DocPath);
            if (!Directory.Exists(strPhysicsPath))
            {
                Directory.CreateDirectory(strPhysicsPath);
            }
            string tempUrl = "/" + strProjName + filename + ".doc";
            strPhysicsPath = strPhysicsPath.TrimEnd(\\) + tempUrl;
            if (Directory.Exists(strPhysicsPath))
            {
                File.Delete(strPhysicsPath);
            }

            doc.Save(strPhysicsPath, SaveFormat.Docx);
            return  tempUrl;              
View Code

 用Aspose.Words把word转成图片

技术分享
Document doc = new Document("f:\\333.doc");
ImageSaveOptions iso = new          ImageSaveOptions(SaveFormat.Jpeg);
iso.Resolution = 128;
iso.PrettyFormat = true;
iso.UseAntiAliasing = true;
for (int i = 0; i < doc.PageCount; i++)
{
        iso.PageIndex = i;
         doc.Save("D:/test/test" + i + ".jpg", iso);
}
View Code

 

    

Aspose.Words导出图片 表格

标签:jpg   nes   pre   添加   alignment   shape   val   test   cal   

原文地址:http://www.cnblogs.com/love201314/p/7612100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!