原文地址为: 基于Aspose.Words 生成Word文件

using Aspose.Words;

string tmppath = Server.MapPath("~/TestWord.doc");        Document doc = new Document(tmppath); //载入模板        if (doc.Range.Bookmarks["Rhythmk"] != null)        {            Bookmark mark = doc.Range.Bookmarks["Rhythmk"];            mark.Text = "张三公司";        }        doc.Save("demo.doc", SaveFormat.Doc, SaveType.OpenInWord, Response);  //保存为doc,并打开

附件下载:http://files.cnblogs.com/rhythmK/OperateWord.rar

Aspose.Words初学者,请指教!

1.先在你的项目中引入Aspose.Words.dll

2.导出带图片人员信息到Word中代码如下:

protected void btn_DownDoc_Click(objectsender, EventArgs e)
       {

//实例化一个新的Word Document

//也可以在Aspose.Words.Document doc = newAspose.Words.Document(path)中加path参数,

//此path指向你设计好的Word模板路径
           Aspose.Words.Document doc = new Aspose.Words.Document();
           DocumentBuilder builder = new DocumentBuilder(doc);
            //设置单元格内容对齐方式
           builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;

//清除设置

builder.PageSetup.ClearFormatting();
           string sql = "select xyxz.fid,xyxz.name as xz,xyxz.ord_no,xy.* "+
                      "from jw_xyxx xy " +
                      "inner join g_infos g on g.id=xy.info_id andg.deleted<>-1 " +
                      "inner join sub_file_relation sub on sub.info_id=xy.info_id "+
                      "inner join jw_bcgl bc on bc.info_id=sub.finfo_id " +
                      "inner join g_infos g2 on g2.id=bc.info_id andg2.deleted<>-1 " +
                      "left join ( " +
                      "select wo.name,rel1.fid,rel1.ord_no,rel1.rel_id fromjw_classframework_rel rel1 " +
                      "inner join jw_classframework_rel rel2 on rel1.fid=rel2.id andrel1.bcinfo_id=rel2.bcinfo_id " +
                      "inner join jw_classframework wo on rel2.rel_id=wo.id andrel2.bcinfo_id=wo.bcinfo_id " +
                      "where rel1.bcinfo_id='"+myuser.ClassList[0].ClassID+"' " +
                      ") xyxz on xyxz.rel_id=xy.info_id " +
                      "where bc.info_id='"+myuser.ClassList[0].ClassID+"' and sub.xxzt=0" +
                      "order by xyxz.fid asc,xyxz.ord_no asc ";
           DBInfo();
           DataTable tbl = m_db.GetDataTable(sql, m_cn);
           List<string> list = newList<string>();
           
           if (tbl != null && tbl.Rows.Count> 0)
           {
               //加载小组
               for (int i = 0; i < tbl.Rows.Count; i++)
               {
                   if (!list.Contains(tbl.Rows[i]["XZ"].ToString()))
                   {
                       list.Add(tbl.Rows[i]["XZ"].ToString());
                   }
               }

double imgcellwidth = 85;
               double imgcellheight = 120;
               double cellwidth = 165;
               double cellheight = 18.5;

//匹配小组中的学员
               builder.StartTable();//开始画Table
               builder.RowFormat.Alignment =Aspose.Words.Tables.RowAlignment.Center;
               string xz = string.Empty;
               int count = 0;
               int rowcount = 0;
               for (int n = 0; n < list.Count;n++ )
               {
                   xz = list[n];
                   builder.RowFormat.Height = 20;
                   //插入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 = 11;

//是否加粗
                   builder.Bold = true;

//向此单元格中添加内容
                   builder.Write(xz);

//Table行结束
                   builder.EndRow();
                   builder.Bold = false;
                   DataRow[] rows = tbl.Select("xz='" + xz + "'");
                   for (int i = 0; i < rows.Length; i = i + 2)
                   {
                       count++;
                       rowcount = (count-1)*6+1+n;
                       //第一行
                       builder.InsertCell();
                       builder.RowFormat.Height = imgcellheight;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;

//合并行单元格
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.First;

builder.CellFormat.VerticalAlignment =Aspose.Words.Tables.CellVerticalAlignment.Center;
                       builder.CellFormat.TopPadding = 2;
                       builder.CellFormat.WrapText = false;
                       builder.CellFormat.Width = imgcellwidth;

if (!string.IsNullOrEmpty(rows[i]["ZPPATH"].ToString()))
                       {

//向此单元格中插入图片
                           Shape shape = new Shape(doc, ShapeType.Image);
                           string url =System.Configuration.ConfigurationManager.AppSettings["UserPhotosSitePath"]+ "\\" + rows[i]["ZPPATH"].ToString();
                           shape.ImageData.SetImage(url);
                           shape.Width = imgcellwidth-2;
                           shape.Height = imgcellheight;
                           shape.HorizontalAlignment = HorizontalAlignment.Center;
                           CompositeNode node = shape.ParentNode;

//把此图片移动到那个单元格中
                           builder.MoveToCell(0, rowcount, 0, 0);
                           builder.InsertNode(shape);
                       }

builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.Width = cellwidth;
                       builder.Write(rows[i]["XM"].ToString());

builder.InsertCell();
                       builder.RowFormat.Height = imgcellheight;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;

//合并行单元格
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.First;
                       builder.CellFormat.VerticalAlignment =Aspose.Words.Tables.CellVerticalAlignment.Center;
                       builder.CellFormat.Width = imgcellwidth;
                       if (rows.Length > i + 1)
                       {
                           if (!string.IsNullOrEmpty(rows[i + 1]["ZPPATH"].ToString()))
                           {
                               Shape shape = new Shape(doc, ShapeType.Image);
                               string url =System.Configuration.ConfigurationManager.AppSettings["UserPhotosSitePath"]+ "\\" + rows[i + 1]["ZPPATH"].ToString();
                               shape.ImageData.SetImage(url);
                               shape.Width = imgcellwidth-2;
                               shape.Height = imgcellheight;
                               shape.HorizontalAlignment = HorizontalAlignment.Center;
                               CompositeNode node = shape.ParentNode;
                               builder.MoveToCell(0, rowcount, 2, 0);
                               builder.InsertNode(shape);
                           }
                       }
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.Width = cellwidth;
                       if (rows.Length > i + 1)
                       {
                           builder.Write(rows[i + 1]["XM"].ToString());
                       }
                       builder.EndRow();

//第二行
                       builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = cellwidth;
                       builder.Write("单位及职务:"+rows[i]["SZDW"].ToString());

builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.Width = cellwidth;
                       if (rows.Length > i + 1)
                       {
                           builder.Write("单位及职务:"+rows[i+1]["SZDW"].ToString());
                       }
                       builder.EndRow();

//第三行
                       builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = cellwidth;
                       builder.Write("单位电话:"+rows[i]["DWDH"].ToString());

builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.Width = cellwidth;
                       if (rows.Length > i + 1)
                       {
                           builder.Write("单位电话:"+rows[i+1]["DWDH"].ToString());
                       }
                       builder.EndRow();

//第四行
                       builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = cellwidth;
                       builder.Write("手机:"+rows[i]["SJHM"].ToString());

builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.Width = cellwidth;
                       if (rows.Length > i + 1)
                       {
                           builder.Write("手机:"+rows[i+1]["SJHM"].ToString());
                       }
                       builder.EndRow();

//第五行
                       builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = cellwidth;
                       builder.Write("邮编:"+rows[i]["DWYB"].ToString());

builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.Width = cellwidth;
                       if (rows.Length > i + 1)
                       {
                           builder.Write("邮编:"+rows[i+1]["DWYB"].ToString());
                       }
                       builder.EndRow();

//第六行
                       builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = cellwidth;
                       builder.Write("Email:"+rows[i]["DZYJ"].ToString());

builder.InsertCell();

//此单元格与上一行单元格合并
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.Previous;
                       builder.CellFormat.HorizontalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Width = imgcellwidth;
                       builder.InsertCell();
                       builder.RowFormat.Height = cellheight;
                       builder.CellFormat.VerticalMerge =Aspose.Words.Tables.CellMerge.None;
                       builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                       builder.CellFormat.Width = cellwidth;
                       if (rows.Length > i + 1)
                       {
                           builder.Write("Email:"+rows[i+1]["DZYJ"].ToString());
                       }
                       builder.EndRow();
                   }
               }
               builder.EndTable();
           }
           string name = "学员通讯录.doc";

//以下载Word的形式打开Wrod

//如图所示:
           doc.Save(name, Aspose.Words.SaveFormat.Doc,Aspose.Words.SaveType.OpenInWord, Response);

}

4.整体效果如图所示:

http://www.soujigu.com/blog_136010.html

转载请注明本文地址: 基于Aspose.Words 生成Word文件

基于Aspose.Words 生成Word文件相关推荐

  1. Java技术:SpringBoot集成FreeMarker生成word文件

                    今天给大家分享SpringBoot集成FreeMarker模板引擎生成word文件的用法,感兴趣的可以学一下,完整源码地址在文章末尾处,欢迎互相沟通交流! 一.什么是F ...

  2. java下freemarker2.3.28+aspose实现生成word文档生成、word转pdf转换

    一.java生成word文档 java生成word文档常用的两种方式: (1)apache下的poi类库处理office文档 (2)freemarker编辑word文件转换的xml文件 通过网上了解, ...

  3. PHP如何引用word模板生成word文件

    进入正题:本流程是基于window环境下. 一.安装composer:Download Compser 点击Composer-Setup.exe之后按照流程安装就行,安装路径自己定义. 可能会出现安装 ...

  4. JSP生成word文件

    1.jsp生成word文件,直接改动jsp格式: <%@ page contentType="application/vnd.ms-word;charset=GB2312"% ...

  5. php如何生成本地文档,php如何生成word文件

    php生成word文件的方法: 需求: 甲方爸爸说"我有一个word文件,里面是关于用户信息的表格,你帮我把系统里面所有用户数据都按照这个文件导出来给我": 我当时就不乐意了,啪的 ...

  6. Java使用iText生成word文件的完美解决方案(亲测可行)

    JAVA生成WORD文件的方法目前有以下种: 一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案 一种是pio但是他的excel处理很程序 wor ...

  7. 在linux环境下com.aspose.words将word文件转为pdf后乱码,window环境下不会

    在linux环境下com.aspose.words将word文件转为pdf后乱码,window环境下不会 乱码原因是因为在linux系统下没有中文字体,所以转换的时候乱码,需要我们手动把window系 ...

  8. Java使用iText生成word文件的解决方案

    JAVA生成WORD文件的方法目前有以下种: 一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案 一种是pio但是他的excel处理很程序 wor ...

  9. java itext word_Java使用iText生成word文件的解决方案 | 学步园

    转自[http://www.31km.cn/post/450.html] JAVA生成WORD文件的方法目前有以下种: 一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操 ...

最新文章

  1. 使用ADO.NET 的最佳实践(zz)
  2. nf_conntrack: table full, dropping packet 解决方案
  3. Jquery Mobile左右滑动效果
  4. CentOS安装rpm包时遇到Header V3 DSA signature: NOKEY时解决办法
  5. java dataset flatmap_Flink 系例 之 FlatMap
  6. Docker4Dev#7 使用 Windows Container运行ASP.NET MVC 2 + SQLExpress 应用
  7. WordPress资源站点推荐
  8. pmp 第六版 模拟卷2疑难问题
  9. 使用ESP8266/ESP8285做一个WIFI中继(WiFi信号放大器)
  10. Nacos:快速不停打印获取配置 c.a.n.client.config.impl.ClientWorker : get changedGroupKeys[${dataId}+${grou
  11. 记忆宫殿--清华一日游
  12. 手机计算机错误格式,手机内存卡提示文件格式错误怎么办【解决方法】
  13. ae渲染存在偏移_E3依旧是最好选择.千元游戏渲染主机该如何搭配
  14. EXCEL内容很少文件却很大---如何变小
  15. 香帅的北大金融学课笔记3 -- 投资银行
  16. 【Jmeter常用断言组件】
  17. 基于元学习的红外弱小点状目标跟踪算法
  18. linux pacemaker.log,Pacemaker部署
  19. redis cluster使用pipeline
  20. 1.2数据模型(实现世界的数据化过程)

热门文章

  1. Mac OS 使用Communicator 方法
  2. 【已解决】ModuleNotFoundError: No module named ‘lanms‘
  3. Py-plt:Plot如何控制坐标轴,图例和注释画出精美的图
  4. 软件工程应用与实践(8)——视频清晰度切换
  5. Linux多进程多线程编程笔记
  6. 兔大侠整理的MySQL-Python(MySQLdb)封装类 - 推酷
  7. macbookpro接口叫什么_USB-C接口是什么 苹果MacBook新型接口分析【详解】
  8. tp5 链接多个数据库 跨数据库操作
  9. 如何在word中制作流程图_在Word 2007中创建流程图
  10. 玩家数据重置问题的思考