使用aspose.word 第三方的插件实现导出word
程序员文章站
2022-06-09 13:47:31
使用aspose.word 第三方的插件实现服务器端无需安装office组件导出word的功能
#region 生成的excel文件命名和确保文件的唯一...
使用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<double> widthlist = new list<double>(); 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