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


最終項目簡易結(jié)構(gòu):

將沒用的東西全部去掉,修改Index.cshtml頁面成這樣:
@{
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)建一個類ExportFileOperator(所有的word操作),此類需要繼承Controller,因為有返回File操作方法

1、 在GetExport中首先命名一個導(dǎo)出word標題就叫:測試導(dǎo)出Word文件
string title = "測試導(dǎo)出Word文件";
創(chuàng)建doc:
var doc = ExportFileOperator.CreateBuilder("GroupForm.doc");
2、CreateBuilder方法實現(xiàn)為(此處操作需要Aspose.Word組件,是操作word的,這個需要大家自己去找一下,或者網(wǎng)上找個破解的):
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、插入標題(需要在word中寫一個標簽,作為標題插入的地址):

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

方法:
ExportFileOperator.InsertTitle(ref doc.Item2, title);//插入標題
public static void InsertTitle(ref DocumentBuilder builder, string fileName, string tempBookMarkName = "title")
{
builder.MoveToBookmark(tempBookMarkName);
builder.Write(fileName);
}
4、根據(jù)業(yè)務(wù)實體,將實體數(shù)據(jù)寫入到word中,也是核心所在
首先命名一個數(shù)據(jù)類:
public class EnterpriseEntity
{
#region 實體成員
/// <summary>
/// id
/// </summary>
public string id { get; set; }
/// <summary>
/// 團隊名
/// </summary>
[Description("企業(yè)名稱")]
public string v1 { get; set; }
/// <summary>
/// 統(tǒng)一社會信用代碼
/// </summary>
[Description("統(tǒng)一社會信用代碼")]
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>
/// 是否屬于國家高新區(qū)內(nèi)的企業(yè)
/// </summary>
[Description("屬于國家高新區(qū)內(nèi)的企業(yè)")]
public string v7 { get; set; }
/// <summary>
/// 是否屬于國家級經(jīng)濟開發(fā)區(qū)內(nèi)的企業(yè)
/// </summary>
[Description("屬于國家級經(jīng)濟開發(fā)區(qū)內(nèi)的企業(yè)")]
public string v9 { get; set; }
/// <summary>
/// 是否屬于國家級科技企業(yè)孵化器內(nèi)的企業(yè)
/// </summary>
[Description("屬于國家級科技企業(yè)孵化器內(nèi)的企業(yè)")]
public string v11 { get; set; }
/// <summary>
/// 是否屬于國家大學(xué)科技園內(nèi)的企業(yè)
/// </summary>
[Description("屬于國家大學(xué)")]
public string v13 { get; set; }
/// <summary>
/// 是否國家備案的眾創(chuàng)空間內(nèi)的企業(yè)
/// </summary>
[Description("國家備案的眾創(chuàng)空間內(nèi)的企業(yè)")]
public string v20 { get; set; }
/// <summary>
/// 企業(yè)注冊類型
/// </summary>
[Description("企業(yè)注冊類型")]
public string v22 { get; set; }
/// <summary>
/// 注冊資本
/// </summary>
[Description("注冊資本")]
public string v24 { get; set; }
/// <summary>
/// 實收資本(萬元人民幣)
/// </summary>
[Description("實收資本")]
public string v25 { get; set; }
/// <summary>
/// 企業(yè)注冊地址
/// </summary>
[Description("企業(yè)注冊地址")]
public string v26 { get; set; }
/// <summary>
/// 郵政編碼
/// </summary>
[Description("注冊地郵政編碼")]
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>
/// 大專及以下人數(shù)
/// </summary>
[Description("大專及以下")]
public string v36 { get; set; }
/// <summary>
/// 高級職稱人數(shù)
/// </summary>
[Description("高級職稱")]
public string v37 { get; set; }
/// <summary>
/// 中級職稱人數(shù)
/// </summary>
[Description("中級職稱")]
public string v38 { get; set; }
/// <summary>
/// 初級職稱人數(shù)
/// </summary>
[Description("初級職稱")]
public string v39 { get; set; }
/// <summary>
/// 高級技工人數(shù)
/// </summary>
[Description("高級技工")]
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>
/// 獲得時間
/// </summary>
[Description("獲得時間")]
public string v44 { get; set; }
/// <summary>
/// 登記入庫的科技型中小企業(yè)是否
/// </summary>
[Description("登記入庫的科技型中小企業(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>
/// 參賽項目產(chǎn)品圖片
/// </summary>
public string v49 { get; set; }
/// <summary>
/// 參賽項目收入占去年企業(yè)營業(yè)收入比例
/// </summary>
[Description("參賽項目收入占去年企業(yè)營業(yè)收入比例")]
public string v50 { get; set; }
/// <summary>
/// 參賽項目介紹(1000字以內(nèi))
/// </summary>
[Description("參賽項目介紹(1000字以內(nèi))")]
public string v51 { get; set; }
/// <summary>
/// 當前五大客戶
/// </summary>
[Description("當前五大客戶")]
public string v52 { get; set; }
/// <summary>
/// 當前五大供應(yīng)商
/// </summary>
[Description("當前五大供應(yīng)商")]
public string v53 { get; set; }
/// <summary>
/// 國內(nèi)市場地位排名
/// </summary>
[Description("國內(nèi)市場地位排名")]
public string v54 { get; set; }
/// <summary>
/// 商業(yè)模式及業(yè)務(wù)拓展計劃
/// </summary>
[Description("商業(yè)模式及業(yè)務(wù)拓展計劃")]
public string v56 { get; set; }
/// <summary>
/// 經(jīng)營風險與對策
/// </summary>
[Description("經(jīng)營風險與對策")]
public string v57 { get; set; }
/// <summary>
/// 企業(yè)管理模式
/// </summary>
[Description("企業(yè)管理模式")]
public string v58 { get; set; }
/// <summary>
/// 公司對管理層及關(guān)鍵人員是否已采取激勵措施是否
/// </summary>
[Description("公司對管理層及關(guān)鍵人員是否已采取激勵措施")]
public string v59 { get; set; }
/// <summary>
/// 公司是否考慮員工持股問題?是否
/// </summary>
[Description("公司是否考慮員工持股問題")]
public string v60 { get; set; }
/// <summary>
/// 企業(yè)其他技術(shù)、產(chǎn)品及服務(wù)(1000字以內(nèi))
/// </summary>
[Description("企業(yè)其他技術(shù)、產(chǎn)品及服務(wù)(1000字以內(nèi))")]
public string v61 { get; set; }
/// <summary>
/// 參賽目的
/// </summary>
[Description("參賽目的")]
public string v62 { get; set; }
/// <summary>
/// 并購需求
/// </summary>
[Description("并購需求")]
public string v63 { get; set; }
/// <summary>
/// 申請大賽組織的大企業(yè)對接活動是否
/// </summary>
[Description("申請大賽組織的大企業(yè)對接活動")]
public string v64 { get; set; }
/// <summary>
/// 資金使用計劃
/// </summary>
[Description("債權(quán)融資資金使用計劃")]
public string v65 { get; set; }
/// <summary>
/// 股權(quán)融資需求是否
/// </summary>
[Description("直接從事研發(fā)科技人員數(shù)")]
public string v66 { get; set; }
/// <summary>
/// 融資金額(萬元¥)
/// </summary>
[Description("上年度吸納高校應(yīng)屆畢業(yè)生人數(shù)")]
public string v67 { get; set; }
/// <summary>
/// 擬出讓股權(quán)比例
/// </summary>
[Description("參賽項目名稱")]
public string v68 { get; set; }
/// <summary>
/// 融資時間
/// </summary>
[Description("產(chǎn)品市場分析及競爭優(yōu)勢")]
public string v69 { get; set; }
/// <summary>
/// 資金使用計劃
/// </summary>
[Description("股權(quán)融資資金使用計劃")]
public string v70 { get; set; }
/// <summary>
/// 申請大賽推薦投資機構(gòu)是否 (修改 申請大賽推薦信貸機構(gòu))
/// </summary>
[Description("申請大賽推薦信貸機構(gòu)")]
public string v71 { get; set; }
/// <summary>
/// 申請大賽組織的融資路演是否 (修改 申請大賽推薦投資機構(gòu))
/// </summary>
[Description("申請大賽推薦投資機構(gòu)")]
public string v72 { get; set; }
/// <summary>
/// 申請國家科技成果轉(zhuǎn)化引導(dǎo)基金設(shè)立的子基金推薦 (修改 申請大賽組織的融資路演)
/// </summary>
[Description("申請大賽組織的融資路演")]
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;
}
}
其中重要的地方是:需要給每個字段一個Description,這里面的值對應(yīng)的就是word模板中的名稱,如下:

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

5、將查出來的數(shù)據(jù),插入到word中,完成最終的導(dǎo)出:
ExportFileOperator.InsertFormData(enterprise, ref doc.Item1);//實體數(shù)據(jù)插入 return new ExportFileOperator().FileResult(title, doc.Item1);
其中最重要的方法就是InsertFormData這個,他的實現(xiàn)如下:
public static void InsertFormData<T>(T objFormData, ref Document document)
{
NodeCollection allTables = document.GetChildNodes(NodeType.Table, true);
List<string> headDescribeNameList = GetObjectHeadDescription<T>();//獲取實體中每個Description中的值
foreach (Table tableFirst in allTables)
{
for (int headIndex = 0; headIndex < headDescribeNameList.Count; headIndex++)//循環(huán)實體中的每個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匹配上,并且這個單元的右邊的cell或者下邊cell有占位,而且是空,就在此處插入值
{
var objValue = GetObjectValueByPropName(objFormData, headDescribeNameList[headIndex]);//根據(jù)DescribeName獲取對應(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è)如果沒有就在下邊空位插入值
break;
}
}
}
}
}
}
public static List<string> GetObjectHeadDescription<T>()
{
var obj = Activator.CreateInstance<T>();
MethodInfo method = obj.GetType().GetMethod("GetThisDescriptionName", new Type[] { });//每個實體需要有GetThisDescriptionName這個方法
return (List<string>)(method?.Invoke(obj, null));
}
其中GetThisDescriptionName方法需求在每個實體類中有實現(xiàn):

根據(jù)descriptionName獲取實體中的值:
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 ? "無" : propertyInfo.GetValue(objFormData).ToString();
}
}
return "無";
}
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);
}
最后一個方法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");
}
最終效果:


最后說一下,其中有一些細節(jié)的地方還是需要做一些處理,暫時沒時間寫,后期有時間補,先就這樣了
大家有什么好的想法或者更好的實現(xiàn)方式,盡管提出來,共同進步
git地址:https://github.com/Binzm/ExportWorkdModel.git
總結(jié)
到此這篇關(guān)于C#中word導(dǎo)出功能騷操作的文章就介紹到這了,更多相關(guān)C# word導(dǎo)出功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

