C#中word導(dǎo)出功能的騷操作詳解
前言
馬上過(guò)牛年了,先祝大家新年好,身體好,心情好!??!
年前最后寫(xiě)一篇之前項(xiàng)目開(kāi)發(fā)的一個(gè)功能,自己根據(jù)系統(tǒng)業(yè)務(wù),想到的一個(gè)解決辦法,效率還是不錯(cuò)的,廢話不多說(shuō),開(kāi)整?。。?/p>
需求:
企業(yè)填報(bào)自己的企業(yè)信息到系統(tǒng)中,最后需要將數(shù)據(jù)以給定word模板形式導(dǎo)出,功能簡(jiǎn)單,就是要開(kāi)發(fā)快,趕及
分析:主要費(fèi)時(shí)間的工作是設(shè)計(jì)企業(yè)填報(bào)表單設(shè)計(jì)實(shí)現(xiàn),以及根據(jù)提供的word模板導(dǎo)出數(shù)據(jù)這塊兒,因?yàn)樯婕暗降谋韱伪容^多,一個(gè)表單最少也有差不多150多個(gè)字段,一個(gè)一個(gè)對(duì),頭發(fā)也得一把一把的掉
想到的解決法案:在導(dǎo)出word這個(gè)功能模塊兒,寫(xiě)一些通用的方法,減少一些工作量。
word數(shù)據(jù)導(dǎo)出功能,思路就是在word模板中每一個(gè)需要填數(shù)據(jù)的地方命名一個(gè)標(biāo)簽,代碼中找到對(duì)應(yīng)命名的標(biāo)簽,插入數(shù)值
傳統(tǒng)做法,第一步:在word模板中填寫(xiě)標(biāo)簽 第二步:程序中每個(gè)插入字段數(shù)據(jù)和word模板標(biāo)簽對(duì)應(yīng),最后插值,這樣做有一個(gè)問(wèn)題就是比較耗時(shí)間,而且很容易出錯(cuò)
我的做法,第一步:給數(shù)據(jù)字段一個(gè)自定義特性,在自定定義特性中寫(xiě)上對(duì)應(yīng)的標(biāo)簽地址,應(yīng)用反射的方法將數(shù)據(jù)最終插入到word模板中。這樣就省去了第一步在word中寫(xiě)標(biāo)簽這樣繁雜的操作。這樣做的問(wèn)題就是性能比較差,但是可以忽略不計(jì)
大體思路就這樣,我就單獨(dú)寫(xiě)一個(gè)demo供大家參考,之后能用就用,重在思路和想法的分享和討論
開(kāi)寫(xiě):
新建一個(gè)項(xiàng)目:ExportWordModel


最終項(xiàng)目簡(jiǎn)易結(jié)構(gòu):

將沒(méi)用的東西全部去掉,修改Index.cshtml頁(yè)面成這樣:
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron" style="text-align: center">
@*<h1>ASP.NET</h1>*@
<input type="button" value="導(dǎo)出" onclick="location.href = '@Url.Action("GetExport","Home")'" />
</div>
在 HomeController 中創(chuàng)建:GetExport
創(chuàng)建一個(gè)類(lèi)ExportFileOperator(所有的word操作),此類(lèi)需要繼承Controller,因?yàn)橛蟹祷谾ile操作方法

1、 在GetExport中首先命名一個(gè)導(dǎo)出word標(biāo)題就叫:測(cè)試導(dǎo)出Word文件
string title = "測(cè)試導(dǎo)出Word文件";
創(chuàng)建doc:
var doc = ExportFileOperator.CreateBuilder("GroupForm.doc");
2、CreateBuilder方法實(shí)現(xiàn)為(此處操作需要Aspose.Word組件,是操作word的,這個(gè)需要大家自己去找一下,或者網(wǎng)上找個(gè)破解的):
private static string _tempPath = AppDomain.CurrentDomain.BaseDirectory;
public static (Document doc, DocumentBuilder builder) CreateBuilder(string tempFileName)
{
string tempPath = $"{_tempPath}{tempFileName}";
Document doc = new Document(tempPath);
return (doc, new DocumentBuilder(doc));
}
3、插入標(biāo)題(需要在word中寫(xiě)一個(gè)標(biāo)簽,作為標(biāo)題插入的地址):

最終可以顯示結(jié)果為這樣:

方法:
ExportFileOperator.InsertTitle(ref doc.Item2, title);//插入標(biāo)題
public static void InsertTitle(ref DocumentBuilder builder, string fileName, string tempBookMarkName = "title")
{
builder.MoveToBookmark(tempBookMarkName);
builder.Write(fileName);
}
4、根據(jù)業(yè)務(wù)實(shí)體,將實(shí)體數(shù)據(jù)寫(xiě)入到word中,也是核心所在
首先命名一個(gè)數(shù)據(jù)類(lèi):
public class EnterpriseEntity
{
#region 實(shí)體成員
/// <summary>
/// id
/// </summary>
public string id { get; set; }
/// <summary>
/// 團(tuán)隊(duì)名
/// </summary>
[Description("企業(yè)名稱(chēng)")]
public string v1 { get; set; }
/// <summary>
/// 統(tǒng)一社會(huì)信用代碼
/// </summary>
[Description("統(tǒng)一社會(huì)信用代碼")]
public string v3 { get; set; }
/// <summary>
/// 成立日期
/// </summary>
[Description("成立日期")]
public string v4 { get; set; }
/// <summary>
/// 參賽行業(yè)領(lǐng)域
/// </summary>
[Description("參賽行業(yè)領(lǐng)域")]
public string v5 { get; set; }
/// <summary>
/// 行政區(qū)域
/// </summary>
[Description("行政區(qū)域")]
public string v6 { get; set; }
/// <summary>
/// 是否屬于國(guó)家高新區(qū)內(nèi)的企業(yè)
/// </summary>
[Description("屬于國(guó)家高新區(qū)內(nèi)的企業(yè)")]
public string v7 { get; set; }
/// <summary>
/// 是否屬于國(guó)家級(jí)經(jīng)濟(jì)開(kāi)發(fā)區(qū)內(nèi)的企業(yè)
/// </summary>
[Description("屬于國(guó)家級(jí)經(jīng)濟(jì)開(kāi)發(fā)區(qū)內(nèi)的企業(yè)")]
public string v9 { get; set; }
/// <summary>
/// 是否屬于國(guó)家級(jí)科技企業(yè)孵化器內(nèi)的企業(yè)
/// </summary>
[Description("屬于國(guó)家級(jí)科技企業(yè)孵化器內(nèi)的企業(yè)")]
public string v11 { get; set; }
/// <summary>
/// 是否屬于國(guó)家大學(xué)科技園內(nèi)的企業(yè)
/// </summary>
[Description("屬于國(guó)家大學(xué)")]
public string v13 { get; set; }
/// <summary>
/// 是否國(guó)家備案的眾創(chuàng)空間內(nèi)的企業(yè)
/// </summary>
[Description("國(guó)家備案的眾創(chuàng)空間內(nèi)的企業(yè)")]
public string v20 { get; set; }
/// <summary>
/// 企業(yè)注冊(cè)類(lèi)型
/// </summary>
[Description("企業(yè)注冊(cè)類(lèi)型")]
public string v22 { get; set; }
/// <summary>
/// 注冊(cè)資本
/// </summary>
[Description("注冊(cè)資本")]
public string v24 { get; set; }
/// <summary>
/// 實(shí)收資本(萬(wàn)元人民幣)
/// </summary>
[Description("實(shí)收資本")]
public string v25 { get; set; }
/// <summary>
/// 企業(yè)注冊(cè)地址
/// </summary>
[Description("企業(yè)注冊(cè)地址")]
public string v26 { get; set; }
/// <summary>
/// 郵政編碼
/// </summary>
[Description("注冊(cè)地郵政編碼")]
public string v27 { get; set; }
/// <summary>
/// 通信地址
/// </summary>
[Description("通信地址")]
public string v28 { get; set; }
/// <summary>
/// 郵政編碼
/// </summary>
[Description("通訊地郵政編碼")]
public string v29 { get; set; }
/// <summary>
/// 企業(yè)網(wǎng)址
/// </summary>
[Description("企業(yè)網(wǎng)址")]
public string v30 { get; set; }
/// <summary>
/// 企業(yè)官方微信
/// </summary>
[Description("企業(yè)官方微信")]
public string v31 { get; set; }
/// <summary>
/// 職工總數(shù)
/// </summary>
[Description("職工總數(shù)")]
public string v32 { get; set; }
/// <summary>
/// 博 士人數(shù)
/// </summary>
[Description("博 士")]
public string v33 { get; set; }
/// <summary>
/// 碩 士人數(shù)
/// </summary>
[Description("碩 士")]
public string v34 { get; set; }
/// <summary>
/// 本 科人數(shù)
/// </summary>
[Description("本 科")]
public string v35 { get; set; }
/// <summary>
/// 大專(zhuān)及以下人數(shù)
/// </summary>
[Description("大專(zhuān)及以下")]
public string v36 { get; set; }
/// <summary>
/// 高級(jí)職稱(chēng)人數(shù)
/// </summary>
[Description("高級(jí)職稱(chēng)")]
public string v37 { get; set; }
/// <summary>
/// 中級(jí)職稱(chēng)人數(shù)
/// </summary>
[Description("中級(jí)職稱(chēng)")]
public string v38 { get; set; }
/// <summary>
/// 初級(jí)職稱(chēng)人數(shù)
/// </summary>
[Description("初級(jí)職稱(chēng)")]
public string v39 { get; set; }
/// <summary>
/// 高級(jí)技工人數(shù)
/// </summary>
[Description("高級(jí)技工")]
public string v40 { get; set; }
/// <summary>
/// 上市公司控股企業(yè)是否
/// </summary>
[Description("上市公司控股企業(yè)")]
public string v41 { get; set; }
/// <summary>
/// 新三板企業(yè)是否
/// </summary>
[Description("新三板企業(yè)")]
public string v42 { get; set; }
/// <summary>
/// 高新技術(shù)企業(yè)是否
/// </summary>
[Description("高新技術(shù)企業(yè)")]
public string v43 { get; set; }
/// <summary>
/// 獲得時(shí)間
/// </summary>
[Description("獲得時(shí)間")]
public string v44 { get; set; }
/// <summary>
/// 登記入庫(kù)的科技型中小企業(yè)是否
/// </summary>
[Description("登記入庫(kù)的科技型中小企業(yè)")]
public string v45 { get; set; }
/// <summary>
/// 企業(yè)概要(不超1000字)
/// </summary>
[Description("企業(yè)概要")]
public string v46 { get; set; }
/// <summary>
/// 關(guān) 鍵 詞
/// </summary>
[Description("關(guān) 鍵 詞")]
public string v47 { get; set; }
/// <summary>
/// 現(xiàn)融資階段
/// </summary>
[Description("現(xiàn)融資階段")]
public string v48 { get; set; }
/// <summary>
/// 參賽項(xiàng)目產(chǎn)品圖片
/// </summary>
public string v49 { get; set; }
/// <summary>
/// 參賽項(xiàng)目收入占去年企業(yè)營(yíng)業(yè)收入比例
/// </summary>
[Description("參賽項(xiàng)目收入占去年企業(yè)營(yíng)業(yè)收入比例")]
public string v50 { get; set; }
/// <summary>
/// 參賽項(xiàng)目介紹(1000字以?xún)?nèi))
/// </summary>
[Description("參賽項(xiàng)目介紹(1000字以?xún)?nèi))")]
public string v51 { get; set; }
/// <summary>
/// 當(dāng)前五大客戶(hù)
/// </summary>
[Description("當(dāng)前五大客戶(hù)")]
public string v52 { get; set; }
/// <summary>
/// 當(dāng)前五大供應(yīng)商
/// </summary>
[Description("當(dāng)前五大供應(yīng)商")]
public string v53 { get; set; }
/// <summary>
/// 國(guó)內(nèi)市場(chǎng)地位排名
/// </summary>
[Description("國(guó)內(nèi)市場(chǎng)地位排名")]
public string v54 { get; set; }
/// <summary>
/// 商業(yè)模式及業(yè)務(wù)拓展計(jì)劃
/// </summary>
[Description("商業(yè)模式及業(yè)務(wù)拓展計(jì)劃")]
public string v56 { get; set; }
/// <summary>
/// 經(jīng)營(yíng)風(fēng)險(xiǎn)與對(duì)策
/// </summary>
[Description("經(jīng)營(yíng)風(fēng)險(xiǎn)與對(duì)策")]
public string v57 { get; set; }
/// <summary>
/// 企業(yè)管理模式
/// </summary>
[Description("企業(yè)管理模式")]
public string v58 { get; set; }
/// <summary>
/// 公司對(duì)管理層及關(guān)鍵人員是否已采取激勵(lì)措施是否
/// </summary>
[Description("公司對(duì)管理層及關(guān)鍵人員是否已采取激勵(lì)措施")]
public string v59 { get; set; }
/// <summary>
/// 公司是否考慮員工持股問(wèn)題?是否
/// </summary>
[Description("公司是否考慮員工持股問(wèn)題")]
public string v60 { get; set; }
/// <summary>
/// 企業(yè)其他技術(shù)、產(chǎn)品及服務(wù)(1000字以?xún)?nèi))
/// </summary>
[Description("企業(yè)其他技術(shù)、產(chǎn)品及服務(wù)(1000字以?xún)?nèi))")]
public string v61 { get; set; }
/// <summary>
/// 參賽目的
/// </summary>
[Description("參賽目的")]
public string v62 { get; set; }
/// <summary>
/// 并購(gòu)需求
/// </summary>
[Description("并購(gòu)需求")]
public string v63 { get; set; }
/// <summary>
/// 申請(qǐng)大賽組織的大企業(yè)對(duì)接活動(dòng)是否
/// </summary>
[Description("申請(qǐng)大賽組織的大企業(yè)對(duì)接活動(dòng)")]
public string v64 { get; set; }
/// <summary>
/// 資金使用計(jì)劃
/// </summary>
[Description("債權(quán)融資資金使用計(jì)劃")]
public string v65 { get; set; }
/// <summary>
/// 股權(quán)融資需求是否
/// </summary>
[Description("直接從事研發(fā)科技人員數(shù)")]
public string v66 { get; set; }
/// <summary>
/// 融資金額(萬(wàn)元¥)
/// </summary>
[Description("上年度吸納高校應(yīng)屆畢業(yè)生人數(shù)")]
public string v67 { get; set; }
/// <summary>
/// 擬出讓股權(quán)比例
/// </summary>
[Description("參賽項(xiàng)目名稱(chēng)")]
public string v68 { get; set; }
/// <summary>
/// 融資時(shí)間
/// </summary>
[Description("產(chǎn)品市場(chǎng)分析及競(jìng)爭(zhēng)優(yōu)勢(shì)")]
public string v69 { get; set; }
/// <summary>
/// 資金使用計(jì)劃
/// </summary>
[Description("股權(quán)融資資金使用計(jì)劃")]
public string v70 { get; set; }
/// <summary>
/// 申請(qǐng)大賽推薦投資機(jī)構(gòu)是否 (修改 申請(qǐng)大賽推薦信貸機(jī)構(gòu))
/// </summary>
[Description("申請(qǐng)大賽推薦信貸機(jī)構(gòu)")]
public string v71 { get; set; }
/// <summary>
/// 申請(qǐng)大賽組織的融資路演是否 (修改 申請(qǐng)大賽推薦投資機(jī)構(gòu))
/// </summary>
[Description("申請(qǐng)大賽推薦投資機(jī)構(gòu)")]
public string v72 { get; set; }
/// <summary>
/// 申請(qǐng)國(guó)家科技成果轉(zhuǎn)化引導(dǎo)基金設(shè)立的子基金推薦 (修改 申請(qǐng)大賽組織的融資路演)
/// </summary>
[Description("申請(qǐng)大賽組織的融資路演")]
public string v73 { get; set; }
#endregion
public List<string> GetThisDescriptionName()
{
var result = new List<string>();
GetType().GetProperties().ToList().ForEach(f =>
{
var descriptionObj = (DescriptionAttribute[])f.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (descriptionObj.Length > 0 && !string.IsNullOrWhiteSpace(descriptionObj[0].Description))
{
result.Add(descriptionObj[0].Description);
}
});
return result;
}
}
其中重要的地方是:需要給每個(gè)字段一個(gè)Description,這里面的值對(duì)應(yīng)的就是word模板中的名稱(chēng),如下:

這里因?yàn)閿?shù)據(jù)是保密的,我就將一些字段刪除了,包括word模板中的一些也刪除了,就拿出一部分。
和數(shù)據(jù)庫(kù)交互的部分我也沒(méi)寫(xiě),就將查出來(lái)的數(shù)據(jù)先命名一個(gè)_enterpriseStr,最后用Newtonsoft轉(zhuǎn)換成實(shí)體這樣操作了哈:
EnterpriseEntity enterprise = JsonConvert.DeserializeObject<EnterpriseEntity>(_enterpriseStr);

5、將查出來(lái)的數(shù)據(jù),插入到word中,完成最終的導(dǎo)出:
ExportFileOperator.InsertFormData(enterprise, ref doc.Item1);//實(shí)體數(shù)據(jù)插入 return new ExportFileOperator().FileResult(title, doc.Item1);
其中最重要的方法就是InsertFormData這個(gè),他的實(shí)現(xiàn)如下:
public static void InsertFormData<T>(T objFormData, ref Document document)
{
NodeCollection allTables = document.GetChildNodes(NodeType.Table, true);
List<string> headDescribeNameList = GetObjectHeadDescription<T>();//獲取實(shí)體中每個(gè)Description中的值
foreach (Table tableFirst in allTables)
{
for (int headIndex = 0; headIndex < headDescribeNameList.Count; headIndex++)//循環(huán)實(shí)體中的每個(gè)DescribeName
{
for (int rowIndex = 0; rowIndex < tableFirst.Rows.Count; rowIndex++)//遍歷word模板中所有的table
{
for (int cellIndex = 0; cellIndex < tableFirst.Rows[rowIndex].Cells.Count; cellIndex++)//遍歷模板中所有的table每行的列數(shù)
{
if (tableFirst.Rows[rowIndex].Cells[cellIndex].GetText() != null && tableFirst.Rows[rowIndex].Cells[cellIndex].GetText().Contains(headDescribeNameList[headIndex]) &&
((tableFirst.Rows[rowIndex].Cells.Count > cellIndex && tableFirst.Rows[rowIndex].Cells[cellIndex + 1] != null && tableFirst.Rows[rowIndex].Cells[cellIndex + 1].GetText().Equals("\a")) || (tableFirst.Rows.Count > rowIndex && tableFirst.Rows[rowIndex + 1] != null && tableFirst.Rows[rowIndex + 1].Cells[cellIndex] != null && tableFirst.Rows[rowIndex + 1].Cells[cellIndex].GetText().Equals("\a"))))//如果遍歷的cell不為空、其中的值能和DescribeName匹配上,并且這個(gè)單元的右邊的cell或者下邊cell有占位,而且是空,就在此處插入值
{
var objValue = GetObjectValueByPropName(objFormData, headDescribeNameList[headIndex]);//根據(jù)DescribeName獲取對(duì)應(yīng)的值
if (tableFirst.Rows[rowIndex].Cells.Count > cellIndex && tableFirst.Rows[rowIndex].Cells[cellIndex + 1] != null && tableFirst.Rows[rowIndex].Cells[cellIndex + 1].GetText().Equals("\a"))
{
InsertCell(objValue, document, tableFirst.Rows[rowIndex].Cells[cellIndex + 1]);//優(yōu)先在右變空位插入值
break;
}
InsertCell(objValue, document, tableFirst.Rows[rowIndex + 1].Cells[cellIndex]);//右側(cè)如果沒(méi)有就在下邊空位插入值
break;
}
}
}
}
}
}
public static List<string> GetObjectHeadDescription<T>()
{
var obj = Activator.CreateInstance<T>();
MethodInfo method = obj.GetType().GetMethod("GetThisDescriptionName", new Type[] { });//每個(gè)實(shí)體需要有GetThisDescriptionName這個(gè)方法
return (List<string>)(method?.Invoke(obj, null));
}
其中GetThisDescriptionName方法需求在每個(gè)實(shí)體類(lèi)中有實(shí)現(xiàn):

根據(jù)descriptionName獲取實(shí)體中的值:
private static string GetObjectValueByPropName<T>(T objFormData, string descriptionName)
{
try
{
var properties = objFormData.GetType().GetProperties();
foreach (var propertyInfo in properties)
{
var descriptionAttributes = (DescriptionAttribute[])propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (descriptionAttributes.Length > 0 && !string.IsNullOrWhiteSpace(descriptionAttributes[0].Description) && descriptionAttributes[0].Description.Equals(descriptionName))
{
return propertyInfo.GetValue(objFormData) == null ? "無(wú)" : propertyInfo.GetValue(objFormData).ToString();
}
}
return "無(wú)";
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
在cell中插入值:
private static void InsertCell(string value, Document doc, Cell cell)
{
Cell insertCell = cell;
insertCell.FirstParagraph.Remove();
Paragraph p = new Paragraph(doc);
p.AppendChild(new Run(doc, (value == null ? "" : value)));
p.ParagraphFormat.Alignment = ParagraphAlignment.Center;
insertCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
insertCell.AppendChild(p);
}
最后一個(gè)方法FileResult:
public FileResult FileResult(string fileName, Document doc)
{
var filePathName = $"{fileName}.doc";
doc.Save(Path.Combine(_tempPath, "temp", filePathName), SaveFormat.Doc); //保存word
filePathName = Path.Combine(_tempPath, "temp", filePathName);
return File(filePathName, "application/doc", $"{fileName}.Doc");
}
最終效果:


最后說(shuō)一下,其中有一些細(xì)節(jié)的地方還是需要做一些處理,暫時(shí)沒(méi)時(shí)間寫(xiě),后期有時(shí)間補(bǔ),先就這樣了
大家有什么好的想法或者更好的實(shí)現(xiàn)方式,盡管提出來(lái),共同進(jìn)步
git地址:https://github.com/Binzm/ExportWorkdModel.git
總結(jié)
到此這篇關(guān)于C#中word導(dǎo)出功能騷操作的文章就介紹到這了,更多相關(guān)C# word導(dǎo)出功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文帶你快速學(xué)會(huì)C#中WinForm框架的使用詳解
WinForm是一門(mén)非常經(jīng)濟(jì)實(shí)惠的技術(shù),就是說(shuō),可以在短時(shí)間內(nèi)學(xué)會(huì),并迅速借此進(jìn)行項(xiàng)目開(kāi)發(fā)。本文就來(lái)和大家聊聊WinForm框架的使用方法,希望對(duì)大家有所幫助2023-02-02
C#多線程異步執(zhí)行和跨線程訪問(wèn)控件Helper
這篇文章介紹了C#多線程異步執(zhí)行和跨線程訪問(wèn)控件Helper,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C#簡(jiǎn)單實(shí)現(xiàn)防止多個(gè)程序運(yùn)行的方法
這篇文章主要介紹了C#簡(jiǎn)單實(shí)現(xiàn)防止多個(gè)程序運(yùn)行的方法,涉及C#進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-02-02
C#實(shí)現(xiàn)獲取電腦硬件顯卡信息的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)獲取電腦硬件顯卡信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
Qt讀取本地系統(tǒng)時(shí)間的幾種方式小結(jié)
這篇文章主要介紹了Qt讀取本地系統(tǒng)時(shí)間的幾種方式小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03

