先看一下导出的整体效果(如下图),其中标注的区域都是通过后台动态生成的:
一、先在Word中建立好表格模板
1.1、参数创建方法(Word和WPS)
1.1.1、Office中Word域的创建
1.1.1.1、选中指定的单元格 -> 点击头部工具栏中的”插入“ -> 选择 ”文档部件“ -> 选择 ”域“;
1.1.1.2、在左侧的 “选择域” 中找到 “Mergefield” -> 在域名中填写变量名(注:填写的域名需要和后台中定义的字段名数组中的字段一样,这样才会填充对应的数据);
自此,Word表格模板已创建完成!
1.1.2、Office中WPS域的创建
1.1.2.1、选中指定的单元格 -> 点击头部工具栏中的”插入“ -> 选择 ”文档部件“ -> 选择 ”域“;
1.1.2.2、在左侧的 “选择域” 中找到 “邮件合并” -> 在域代码填写变量名:例如:(MERGEFIELD Textdwmc)(注:填写的域名需要和后台中定义的字段名数组中的字段一样,这样才会填充对应的数据);
自此,WPS中Word表格模板已创建完成!
二、添加相关的DLL文件
这里的前提,你需要引用Aspose.Words.dll文件:using Aspose.Words;
2.1、添加相关的DLL文件
2.1.1、右键项目->管理NuGet程序包
2.1.2、在 管理NuGet程序包 中的浏览搜索:Aspose.Words(注:一定选择21.8.0版本,这个版本能去水印,其余高版本我暂没试过( ̄▽ ̄)~*)点击安装即可!
自此,Aspose.Words就安装成功!或者直接从网上下载指定的版本即可
三、编写代码(后台.NET 代码以VS 2019为例)
3.1、定义公共的字段(注:定义的数组字段要和Word模板中的域名相同)
public static String[] fieldNames = new String[] { "Textdwmc", "Textdwdz", "Textfrdb", "Texthyxm", "Textmz", "Radioxb", "Textsfz", "Textdp", "Textlxdh", "Textwhcd", "Textgryx", "Texthyxz", "Textqyrs", "Textjxdz", "droprhxz", "Textrhsq", "Radiostate" };
public static fileName="";
3.2、Word模板操作核心方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.IO;
using Aspose.Words;
using System.Data;
namespace AsposeWord
{
public class WordModelEvent
{
public static string ExpertWordToModel(string modelDoc, string exptDoc, String[] fieldNames, Object[] fieldValues, DataTable dt)
{
try
{
removeWatermark();
string tempPath = "../" + modelDoc + ".docx";
const string saveFold = "../word/";
string outputPath = "" + saveFold;
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
string fileName = exptDoc + "信息.docx";
outputPath += fileName;
Document doc = new Document(tempPath);
doc.MailMerge.Execute(fieldNames, fieldValues);
if (dt.Rows.Count > 0)
{
doc.MailMerge.ExecuteWithRegions(dt);
}
String StrVisitURL = saveFold + fileName;
doc.MailMerge.Execute(new[] { "PageCount" }, new object[] { doc.PageCount });
doc.Save(outputPath);
return StrVisitURL;
}
catch (Exception er)
{
return er.Message;
}
}
public static void removeWatermark()
{
new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String("PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1pdGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+CiAgICA8T3JkZXJJRD4yMDA2MDIwMTI2MzM8L09yZGVySUQ+CiAgICA8VXNlcklEPjEzNDk3NjAwNjwvVXNlcklEPgogICAgPE9FTT5UaGlzIGlzIGEgcmVkaXN0cmlidXRhYmxlIGxpY2Vuc2U8L09FTT4KICAgIDxQcm9kdWN0cz4KICAgICAgPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl0aW9uVHlwZT4KICAgIDxTZXJpYWxOdW1iZXI+OTM2ZTVmZDEtODY2Mi00YWJmLTk1YmQtYzhkYzBmNTNhZmE2PC9TZXJpYWxOdW1iZXI+CiAgICA8U3Vic2NyaXB0aW9uRXhwaXJ5PjIwMjEwODI3PC9TdWJzY3JpcHRpb25FeHBpcnk+CiAgICA8TGljZW5zZVZlcnNpb24+My4wPC9MaWNlbnNlVmVyc2lvbj4KICAgIDxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuYXNwb3NlLmNvbS9wb2xpY2llcy91c2UtbGljZW5zZTwvTGljZW5zZUluc3RydWN0aW9ucz4KICA8L0RhdGE+CiAgPFNpZ25hdHVyZT5wSkpjQndRdnYxV1NxZ1kyOHFJYUFKSysvTFFVWWRrQ2x5THE2RUNLU0xDQ3dMNkEwMkJFTnh5L3JzQ1V3UExXbjV2bTl0TDRQRXE1aFAzY2s0WnhEejFiK1JIWTBuQkh1SEhBY01TL1BSeEJES0NGbWg1QVFZRTlrT0FxSzM5NVBSWmJRSGowOUNGTElVUzBMdnRmVkp5cUhjblJvU3dPQnVqT1oyeDc4WFE9PC9TaWduYXR1cmU+CjwvTGljZW5zZT4=")));
}
}
}
3.3、通过Word模板生成新文件
public static string GetWordEvent(string id)
{
try
{
memberUnit member = new memberUnit();
member.id = "202204291157458529";
member.Textdwmc = "长沙杰术有限公司";
member.Textdwdz = "湖南省长";
member.Textfrdb =null;
member.Texthyxm = "gows";
member.Textmz = "汉";
member.Radioxb = "男";
member.Textsfz = "4302685556951366";
member.Textdp = "党员";
member.Textlxdh = "175125956569";
member.Textwhcd = "高中";
member.Textgryx = "1051485780@qq.com";
member.Texthyxz = "wu";
member.Textqyrs = "1444";
member.Textjxdz = "湖南啊";
member.droprhxz = "副会长";
member.Textrhsq = "呃呃呃呃呃呃呃呃呃呃";
member.Radiostate = "已处理";
Object[] fieldValues = new object[fieldNames.Length];
fieldValues[0] = member.Textdwmc;
fieldValues[1] = member.Textdwdz;
fieldValues[2] = member.Textfrdb;
fieldValues[3] = member.Texthyxm;
fieldValues[4] = member.Textmz;
fieldValues[5] = member.Radioxb;
fieldValues[6] = member.Textsfz;
fieldValues[7] = member.Textdp;
fieldValues[8] = member.Textlxdh;
fieldValues[9] = member.Textwhcd;
fieldValues[10] = member.Textgryx;
fieldValues[11] = member.Texthyxz;
fieldValues[12] = member.Textqyrs;
fieldValues[13] = member.Textjxdz;
fieldValues[14] = member.droprhxz;
fieldValues[15] = member.Textrhsq;
fieldValues[16] = member.Radiostate;
DataTable dt = new DataTable();
if (!string.IsNullOrEmpty(id))
{
string sql = " SELECT * FROM TABLE WHERE id='' ORDER BY id DESC ";
dt = getDataTable(sql);
dt.TableName = "UserList";
}
string StrVisitURL = WordModelEvent.ExpertWordToModel("template", member.Textdwmc, fieldNames, fieldValues, dt);
dt.Dispose();
return StrVisitURL;
}
catch (Exception er)
{
return er.Message;
}
}
private static DataTable getDataTable(string sql, string ConnectionString = "")
{
SqlConnection cn = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
DataTable ds = new DataTable();
da.Fill(ds);
cn.Dispose();
return ds;
}
3.4、调用方法
string url= GetWordEvent("");
3.5、.NET 文件下载
3.5.1、ASP.NET 文件下载
public void DownloadFile(string fileName, string fPath)
{
string filePath = Server.MapPath(fPath);
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
if (!string.IsNullOrEmpty(filePath)&& filePath!=".")
{
System.IO.File.Delete(filePath+"/"+fileName);
}
Response.End();
}
3.5.2、ASP.NET MVC 的下载方法
public static HttpResponseMessage Download(string path, string fileName)
{
var stream = File.OpenRead(path);
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
try
{
httpResponseMessage.Content = new StreamContent(stream);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
if (!string.IsNullOrEmpty(fileName))
{
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8),
};
}
if (!string.IsNullOrEmpty(filePath)&& filePath!=".")
{
System.IO.File.Delete(filePath+"/"+fileName);
}
return httpResponseMessage;
}
catch
{
stream.Dispose();
httpResponseMessage.Dispose();
throw;
}
}
|