有些事情,你想记得的就会记得。有些事情,你想忘记的就会忘记,如果忘记不了,那就不要忘记了,因为忘记是不需要努力的。


Model_Car.cs代码

  public class Model_Car{public string PartyA { get; set; }public string PartyB { get; set; }public string Aidentity { get; set; }public string Bidentity { get; set; }public string Asinger { get; set; }public string Bsinger { get; set; }}

ExcelHelper.cs代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Aspose.Cells;namespace ReplaceTest
{public class ExcelHelper{#region 数据导入public static DataTable ExeclToDataTable(string Path){try{DataTable dt = new DataTable();Aspose.Cells.Workbook workbook = new Workbook();workbook.Open(Path);Worksheets wsts = workbook.Worksheets;for (int i = 0; i < wsts.Count; i++){Worksheet wst = wsts[i];int MaxR = wst.Cells.MaxRow;int MaxC = wst.Cells.MaxColumn;if (MaxR > 0 && MaxC > 0){dt = wst.Cells.ExportDataTableAsString(0, 0, MaxR + 1, MaxC + 1, true);}}return dt;}catch (Exception ex){throw new Exception(ex.Message);}}public static DataSet ExeclToDataSet(string Path){try{DataTable dt = new DataTable();Aspose.Cells.Workbook workbook = new Workbook();workbook.Open(Path);Worksheets wsts = workbook.Worksheets;for (int i = 0; i < wsts.Count; i++){Worksheet wst = wsts[i];int MaxR = wst.Cells.MaxRow;int MaxC = wst.Cells.MaxColumn;if (MaxR > 0 && MaxC > 0){dt = wst.Cells.ExportDataTableAsString(0, 0, MaxR + 1, MaxC + 1, true);}}//SqlDataAdapter adapter = null;DataSet ds = new DataSet();ds.Tables.Add(dt);//adapter.Fill(dt);return ds;}catch (Exception ex){throw new Exception(ex.Message);}}#endregion}
}

WordHelper.cs代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Aspose.Cells;
using Aspose.Words;namespace ReplaceTest
{public class WordHelper{#region 导出Wordpublic static string ModelToWord(Model_Car car,string path,int num){try{Document doc = new Document(path);DocumentBuilder builder = new DocumentBuilder(doc);foreach (System.Reflection.PropertyInfo p in car.GetType().GetProperties()){builder.MoveToBookmark(p.Name);builder.Write(p.GetValue(car,null).ToString());}doc.Save(System.AppDomain.CurrentDomain.BaseDirectory + string.Format("OutFile/car{0}协议书By书签.doc", num));return "OK";}catch (Exception ex){throw new Exception(ex.Message);}}#endregion}
}

Index.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="ReplaceTest.Index" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title>
</head>
<body><form id="form1" runat="server"><div><asp:Button ID="btnConvert" runat="server" Text="转换" OnClick="btnConvert_Click" /></div></form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace ReplaceTest
{public partial class Index : System.Web.UI.Page{private const string pathDirectory = "~/DataFile/";private string excelfileName = "Buyer.xlsx";private string docfileName = "car协议书.doc";private string docfileName1 = "carByProperty协议书.doc";List<Model_Car>  list=new List<Model_Car>();protected void Page_Load(object sender, EventArgs e){}protected void btnConvert_Click(object sender, EventArgs e){try{if (!Directory.Exists(Server.MapPath(pathDirectory))){//若文件目录不存在 则创建Directory.CreateDirectory(Server.MapPath(pathDirectory));}string fullname = pathDirectory + excelfileName;string fullpath = Server.MapPath(fullname);DataTable inputdt = ExcelHelper.ExeclToDataTable(fullpath);for (int i = 0; i < inputdt.Rows.Count; i++){Model_Car mCar = new Model_Car();mCar.PartyA = inputdt.Rows[i][0].ToString().Replace("\"", "").Trim();mCar.PartyB = inputdt.Rows[i][1].ToString().Trim();mCar.Aidentity = inputdt.Rows[i][2].ToString().Trim();mCar.Bidentity = inputdt.Rows[i][3].ToString().Trim();mCar.Asinger = inputdt.Rows[i][4].ToString().Trim();mCar.Bsinger = inputdt.Rows[i][5].ToString().Trim();list.Add(mCar);}string docfullpath = Server.MapPath(pathDirectory + docfileName1);for (int i = 0; i < list.Count; i++){WordHelper.ModelToWord(list[i], docfullpath, i);}}catch (Exception ex){}}}
}

方法二生成多份word文档:

 public partial class Index : System.Web.UI.Page{private const string pathDirectory = "~/DataFile/";private string excelfileName = "Buyer.xlsx";private string docfileName = "car协议书.doc";List<Model_Car>  list=new List<Model_Car>();protected void Page_Load(object sender, EventArgs e){}protected void btnConvert_Click(object sender, EventArgs e){try{if (!Directory.Exists(Server.MapPath(pathDirectory))){//若文件目录不存在 则创建Directory.CreateDirectory(Server.MapPath(pathDirectory));}string fullname = pathDirectory + excelfileName;string fullpath = Server.MapPath(fullname);DataTable inputdt = ExcelHelper.ExeclToDataTable(fullpath);string docfullpath = Server.MapPath(pathDirectory + docfileName);WordHelper.DtToWord(inputdt, docfullpath);}catch (Exception ex){}}}
  public static void DtToWord(DataTable inputdt, string docfullpath){for (int i = 0; i < inputdt.Rows.Count; i++){Document doc = new Document(docfullpath);DocumentBuilder builder = new DocumentBuilder(doc);for (int j = 0; j <  inputdt.Columns.Count; j++){string Cellvalue = inputdt.Rows[i][j].ToString().Trim();builder.MoveToBookmark(inputdt.Columns[j].ColumnName);builder.Write(Cellvalue);}doc.Save(System.AppDomain.CurrentDomain.BaseDirectory + string.Format("OutFile/car{0}协议书By书签.doc", i));}}/// <summary>/// 根据datatable获得列名/// </summary>/// <param name="dt">表对象</param>/// <returns>返回结果的数据列数组</returns>public static string[] GetColumnsByDataTable(DataTable dt){string[] strColumns = null;if (dt.Columns.Count > 0){int columnNum = 0;columnNum = dt.Columns.Count;strColumns = new string[columnNum];for (int i = 0; i < dt.Columns.Count; i++){strColumns[i] = dt.Columns[i].ColumnName;}}return strColumns;}

项目及文档结构:




运行结果

把Excel数据填充word模板生成多份word文档相关推荐

  1. 利用Freemarker模板生成doc或者docx文档(转载整理)

    可以直接看主要代码实现 doc作为模板文件生成指定格式的doc文件 实现逻辑 1.把作为模板的doc文件另存为xml文件 2.凡是需要填充的数据用${xxxx}替代 3.利用Template类将数据填 ...

  2. Python通过word模板生成新的word文件

    功能自定义好的word文档,生成新的word文件 模块地址:https://docxtpl.readthedocs.io/en/latest/ 使用模块 docxtpl 安装方式 在线安装 pip i ...

  3. java springboot easypoi 根据word模板生成对应的word并下载

    生成word如图 访问接口地址即可下载 具体操作如下 第一步:创建word模板 word模板如下,需要和java代码map值一一对应. 第二步:引入maven依赖 <dependency> ...

  4. Android poi 根据已有模板生成新的doc文档

    最新要做个根据已有doc模板生成新的doc文件项目,查了相关资料,比较好用的是poi和jword,但是jword需要付费,免费30天:但是poi也有弊端,最新版的说是支持java,不支持Android ...

  5. Vue根据word模板导出页面所需文档

    今天看到前端页面感觉还挺神奇,决定学习一下这个功能是怎么写的. 这是报价单生成页面,当该填的内容填完之后,点击立即创建.然后就会生成一个报价单.是不是挺有意思的,一瞬间发现了前端的乐趣. 先说一下里面 ...

  6. 如何使用freeMarker生成doc、docx文档

    如何使用freeMarker生成doc.docx.pdf文件 freeMarker是什么 doc和docx有什么区别 如何生成doc文档(带图片) 如何生成docx文档,以及将其生成pdf文档 fre ...

  7. python 批量打印文档_使用python将Excel数据填充Word模板并生成Word

    [项目需求] Excel中有一万多条学生学平险数据,需要给每位学生打印购买回执单,回执单包括学生姓名,身份证号,学校等信息,目前只能从Excel拷贝数据到Word模板中,然后打印,效率及其低下,寻求帮 ...

  8. python excel word模板_Python将Excel数据插入Word模板生成详细内容文档

    最近在实际工作中遇到的一个情况是,每个月固定时间要报送一批文档,文档的内容相似,有固定的模板,我这么懒的人肯定要想一个一劳永逸的办法.下面把搜索发现的情况记录一下,以备以后需要. Python有个叫做 ...

  9. 使用word模板生成word文档的各类方案

    使用word模板生成word文档的各类方案 生成word的各种方案 word另存xml进行后续处理 2003版本word(.doc)的xml处理并生成word 2007版本word(.docx)的xm ...

最新文章

  1. kafka丢数据问题方案(转载+整理+汇总)
  2. yarn界面中的Minimum Allocation和Maximum Allocation与yarn-site.xml中参数的对应关系
  3. 文档预览 OfficeWebViewer:在浏览器中查看Office文档
  4. 【MVC 过滤器的应用】ASP.NET MVC 如何统计 Action 方法的执行时间
  5. 深度剖析Java数据结构之表(三)——ArrayList泛型类的实现
  6. php fpm 日志级别,Php 错误日志级别
  7. 用c语言编声光报警子程序,C语言编程的智能火灾报警监测系统
  8. linux之--install超时
  9. 此时本机的BootLoader程序坏了,也就是说grub第一阶段坏掉了,该如何修复
  10. python 初始化数组_Python里面这些点,据说90%的新手都会一脸懵逼
  11. 电商大促作战指南之全链路压测
  12. 程序员和码农有什么不同?从这三个单词就看得出来
  13. openwrt定时任务 2021-10-08
  14. 认识即时通讯开发通信协议之MQTT
  15. Cobalt Strike Malleable C2
  16. SAP 成套销售按项目销售
  17. centos7下zeppelin安装配置
  18. C语言模拟实现strlen
  19. 解读NSString之性能分析
  20. 中国图书分类号-自动化_计算机

热门文章

  1. 标准C函数库头文件、POSIX标准库头文件和Windows API函数库头文件说明
  2. java 导出复杂格式的 Excel 留着自己备用
  3. 拉格朗日乘子法(自己总结一些要点)
  4. cmd中文输入和显示问题
  5. 企业微信组织架构同步优化的思路
  6. 【python游戏开发】回合制手游做任务嫌麻烦?教你用Python简单制作回合制手游外挂
  7. vue项目初始化出现tar ENOENT: no such file or directory错误的解决办法。
  8. nyoj1328派队方案
  9. C++游戏——小胎大乱斗
  10. 说一下国内做MES的几类厂商