基于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

你可能感兴趣的:(word)