栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

使用aspose.word 第三方的插件实现导出word

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用aspose.word 第三方的插件实现导出word

使用aspose.word 第三方的插件实现服务器端无需安装office组件导出word的功能

#region 生成的excel文件命名和确保文件的唯一性
 Random rd = new Random();
 DateTime dtime = DateTime.Now;
 string Filename = "培训需求表" + dtime.Year.ToString() + dtime.Month.ToString() + dtime.Day.ToString() + dtime.Hour.ToString() + dtime.Minute.ToString() + dtime.Second.ToString() + Convert.ToString(rd.Next(99) * 97 + 100) + ".doc";
 #endregion
 
 #region 定义变量
 //建立document物件,调用模块对word字体和table格式设置
 string serverpath = Constants.GetAppSettingValue("LocalLogicPath") + Constants.GetAppSettingValue("LocalTemplate") + "px_xqjh_tx.doc";
 document doc = new document(serverpath);
 //建立documentBuilder物件 
 documentBuilder builder = new documentBuilder(doc);
 #endregion
 
 #region 页面设置,设置页面为横向布局,设置纸张类型为A4纸或通过页面的宽度设置
 //设置纸张布局
 builder.PageSetup.PaperSize = PaperSize.A4;
 //builder.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
 #endregion
 
 #region 设置word全局的字体样式和字体大小
 builder.RowFormat.Borders.LineStyle = LineStyle.Thick;
 builder.RowFormat.HeightRule = HeightRule.Auto;
 builder.RowFormat.Alignment = RowAlignment.Center;
 //builder.Font.Name = "仿宋-GB2312";
 
 builder.Font.Name = "宋体";
 builder.Font.Size = 10.5; //五号
 #endregion
 
 List widthList = new List();
 double remarkWidth = 0;
 for (int k = 0; k < 18; k++)
 {
   builder.MoveToCell(0, 1, k, 0); //移动单元格
   double width = builder.CellFormat.Width;//获取单元格宽度
   widthList.Add(width);
   remarkWidth += width;
 
   //Universal.ExceptionLog(k.ToString(), width.ToString());
 }
 
 DataTable dtable = GetQuestionList();
 if (dtable != null && dtable.Rows.Count > 0)
 {
   #region 绘制表格以及设置--------- 开头
   doc.Range.Bookmarks["tbdw"].Text = this.lblcom.Text;
 
   builder.MoveToBookmark("pxtx");    //开始添加值
   Aspose.Words.Tables.Table table = builder.StartTable();
   builder.RowFormat.HeadingFormat = true;
   builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
   #endregion
 
   #region 数据集
   for (int i = 0; i < dtable.Rows.Count; i++)
   {
     for (int j = 0; j < dtable.Columns.Count - 3; j++)
     {
#region 列
builder.InsertCell();// 添加一个单元格   
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(255, 255, 255);
builder.CellFormat.Width = widthList[j];
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Top;//垂直居中对齐
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;//水平居中对齐
builder.Write(dtable.Rows[i][j].ToString().Trim());
 
//Universal.ExceptionLog((i + 1).ToString() + "*" + (j + 1).ToString(), dtable.Rows[i][j].ToString().Trim());
#endregion
     }
 
     builder.EndRow();
 
   }
   #endregion
 
   #region 备注列
   builder.InsertCell();// 添加一个单元格   
   builder.CellFormat.Borders.LineStyle = LineStyle.Single;
   builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
   builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(255, 255, 255);
   builder.CellFormat.Width = remarkWidth;
   builder.RowFormat.Height = 50;
   builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
   builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Top;//垂直居中对齐
   builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;//水平居中对齐
   builder.Write("备注:");
   builder.EndRow();
   #endregion
 
   #region 绘制表格以及设置--------- 结尾
   builder.EndTable();
   doc.Range.Bookmarks["pxtx"].Text = "";  // 清掉标示 
   #endregion
 
   #region 计划编制和审批人
   doc.Range.Bookmarks["writername"].Text = dtable.Rows[0]["reg_staff_name"].ToString();
   doc.Range.Bookmarks["checkname"].Text = dtable.Rows[0]["check_man_name"].ToString();
   #endregion
 }
 dtable.Dispose();
  
 #region 保存数据.
 doc.Save(Filename, SaveFormat.Doc, SaveType.OpenInWord, Response); //保存为doc,并打开
 #endregion

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/125183.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号