MVC5中根据word模板填充数据生成word
来源:互联网 发布:linux命令存储 编辑:程序博客网 时间:2024/06/05 07:51
HomeController.cs
using Aspose.Words;using Aspose.Words.Tables;using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;using System.Web.Mvc;namespace Web.Controllers{ public class HomeController : Controller { // GET: Home public ActionResult Index() { FormatReportWord(); ReportWord(); return View(); } private void FormatReportWord() { try { string templateFile = Server.MapPath("~/Temp/FormartWordTemplate.doc"); string saveDocFile = Server.MapPath("~/Word/FormartWord" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc"); Aspose.Words.Document doc = new Aspose.Words.Document(templateFile); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc); builder.MoveToBookmark("模块名称"); //builder.InsertFootnote(0,"这是一个模块"); builder.Write("这是一个模块"); builder.MoveToBookmark("操作动作"); //builder.InsertFootnote(0,"这是一个模块"); builder.Write("添加一个门店"); doc.Save(saveDocFile); System.Diagnostics.Process.Start(saveDocFile); } catch (Exception ex) { throw ex; } } private void ReportWord() { try { string templateFile = Server.MapPath("~/Temp/WordTemplate.doc"); string saveDocFile = Server.MapPath("~/Word/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc"); Aspose.Words.Document doc = new Aspose.Words.Document(templateFile); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc); DataTable nameList = new DataTable(); nameList.Columns.Add("编号", typeof(string)); nameList.Columns.Add("姓名", typeof(string)); nameList.Columns.Add("时间", typeof(string)); DataRow row = null; for (int i = 0; i < 50; i++) { row = nameList.NewRow(); row["编号"] = i.ToString().PadLeft(4, '0'); row["姓名"] = "伍华聪 " + i.ToString(); row["时间"] = DateTime.Now.ToString(); nameList.Rows.Add(row); } List<double> widthList = new List<double>(); for (int i = 0; i < nameList.Columns.Count; i++) { builder.MoveToCell(0, 0, i, 0); //移动单元格 double width = builder.CellFormat.Width;//获取单元格宽度 widthList.Add(width); } builder.MoveToBookmark("table"); //开始添加值 for (var i = 0; i < nameList.Rows.Count; i++) { for (var j = 0; j < nameList.Columns.Count; j++) { builder.InsertCell();// 添加一个单元格 builder.CellFormat.Borders.LineStyle = LineStyle.Single; builder.CellFormat.Borders.Color = System.Drawing.Color.Black; builder.CellFormat.Width = widthList[j]; builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中对齐 builder.Write(nameList.Rows[i][j].ToString()); } builder.EndRow(); } doc.Save(saveDocFile); doc.Range.Bookmarks["table"].Text = ""; // 清掉标示 //doc.Save(saveDocFile); System.Diagnostics.Process.Start(saveDocFile); } catch (Exception ex) { throw ex; } } }}
模板
运行结果如图:
阅读全文
0 0
- MVC5中根据word模板填充数据生成word
- 填充数据到word模板中
- 根据模板生成word
- java word 模板 数据填充
- 根据模板生成word文件
- aspose.word根据word模板填充对应信息并导出
- 把Excel数据填充word模板生成多份word文档
- Java中根据模板导出数据到word的解决方案
- .net根据模板生成Word文件
- 根据Word模板生成动态网页
- jacob根据word模板生成文
- java根据模板生成word文件
- freemarker根据模板生成word文档,换行
- freemarker根据模板生成word步骤优化
- java根据ftl模板生成word文档
- 根据模板生成word文档下载
- Java根据xml模板生成word
- freemarker根据word模板生成word文档中遇到的问题
- HDU-2025(查找最大元素)
- hibernate
- c和lua进行的一些基础交互和处理,这里留用lua5.1
- jQuery的几大难点
- java 对图片的矩阵运算示例
- MVC5中根据word模板填充数据生成word
- 全部用jquery写的无缝左右滑动轮播图
- 多线程的应用
- 火车购票系统
- 创建字体信息设置界面
- setTimeout()和setInterval()
- 实验四 Linux系统管理
- 博弈汇总
- 基于opencv+Dlib的面部合成(Face Morph)