把Excel数据填充word模板生成多份word文档

xiaoxiao2021-02-27  387

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


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 导出Word public 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; }

项目及文档结构:




运行结果

转载请注明原文地址: https://www.6miu.com/read-8731.html

最新回复(0)