之前寫了一篇開源組件DocX讀寫Word的文章,當時時間比較匆忙選了這個組件,使用過程中還是有些不便,不能提前定義好模版,插入Form表單域進行替換。最近無意中發(fā)現(xiàn)Spire.Doc組件功能很強大,目前來看基本上符合我的所有使用場景。本篇將挑選幾個重要的應(yīng)用場景進行介紹。
閱讀目錄
使用word的FormField預(yù)先插入占位符,然后在代碼中獲取所有FormField,進行替換。這種場景特別適用于,模版固定動態(tài)更改內(nèi)容。看看最終效果
表單域制作步驟
1.打開word中的開發(fā)工具選項,對于導(dǎo)航欄中沒有這一項的,可以通過 文件->選項->自定義功能區(qū)->開發(fā)工具 進行打開
2.插入TextField, 屬性->設(shè)置書簽名稱
模版制作完成后,來看看實現(xiàn)代碼
class PRogram { private static BindingFlags s_flag = BindingFlags.Instance | BindingFlags.Public; static void Main(string[] args) { Console.WriteLine("/nRunning Examples"); Resume personResume = new Resume { Name = "Spire.Doc",//姓名 Marriage = "未婚",//婚姻 Birth = "2010-09-19",//出生年月 Political = "團員",//政治面貌 Sex = "男",//性別 Nation = "漢族",//民族 Degree = "大學(xué)本科",//學(xué)位 Mobile = "13567890987",//移動電話 Professional = "軟件工程",//專業(yè) Email = "2345678@QQ.com",//郵箱 Adress = "故宮", //地址 MajorCourse = "數(shù)據(jù)結(jié)構(gòu),C語言,算法,C++",//主修課程 PersonalAbility = "熟練掌握DocX操作Word,SQL能力強悍",//個人能力 ComputerAbility = "高級軟件工程師",//計算機能力 LanguageLevel = "CET-4,CET-6",//外語水平 Awards = "1999年幾月 曾獲優(yōu)秀班干部,3等獎學(xué)金1999年幾月 曾獲校優(yōu)秀干部,學(xué)生會先進集體,2等獎學(xué)金20**年幾月 曾獲優(yōu)秀學(xué)習(xí)委員,網(wǎng)絡(luò)技術(shù)協(xié)會負責(zé)人,……………………",//獎勵情況 SelfEvaluation = "本人性格開朗、穩(wěn)重、有活力,待人熱情、真誠;工作認真負責(zé),積極主動,能吃苦耐勞,用于承受壓力,勇于創(chuàng)新;有很強的組織能力和團隊協(xié)作精神,具有較強的適應(yīng)能力;紀律性強,工作積極配合;意志堅強,具有較強的無私奉獻精神"//自我評價 }; CreateResume(personResume); Console.WriteLine("/nPress any key to exit."); Console.ReadKey(); } private static void CreateResume(Resume personResume) { Document doc = new Document(@"ResumeTemplate.docx"); //清除表單域陰影 doc.Properties.FormFieldShading=false; try { Type type = typeof(Resume); PropertyInfo[] properties = type.GetProperties(s_flag); int length = properties.Length; Dictionary<string, PropertyInfo> dict = new Dictionary<string, PropertyInfo>(length, StringComparer.OrdinalIgnoreCase); foreach (PropertyInfo prop in properties) { dict[prop.Name] = prop; } object value = null; foreach (FormField field in doc.Sections[0].Body.FormFields) {
//field.name對應(yīng)設(shè)置模版文本域名稱 PropertyInfo prop = dict[field.Name]; if (prop != null) { value = prop.GetValue(personResume, null); if (value != null && value != DBNull.Value) { switch (field.Type) { case FieldType.FieldFormTextInput: field.Text = value.ToString(); break; default: break; } } } } doc.SaveToFile(@"DocXResume.docx", FileFormat.Docx); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
public class Resume { public string Name { get; set; } public string Marriage { get; set; } public string Birth { get; set; } public string Political { get; set; } public string Sex { get; set; } public string Nation { get; set; } public string Degree { get; set; } public string Mobile { get; set; } public string Professional { get; set; } public string Email { get; set; } public string Adress { get; set; } public string MajorCourse { get; set; } public string PersonalAbility { get; set; } public string ComputerAbility { get; set; } public string LanguageLevel { get; set; } public string Awards { get; set; } public string SelfEvaluation { get; set; } }
重點關(guān)注上面標紅的代碼,可以看到通過簡單的代碼即可完成一份簡歷文檔
使用Spire.Doc可以很方便的將word轉(zhuǎn)換成HTML,RTF,PDF,TXT,wps...等格式
Document doc = new Document(@"SpireDocResume.docx"); //Save doc file.doc.SaveToFile("SpireDocResume.html", FileFormat.Html);
private static void addTable(Section section) { String[] header = { "Name", "Capital", "Continent", "Area", "Population" }; String[][] data = { new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"}, new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"}, new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"}, new String[]{"Chile", "Santiago", "South America", "756943", "13200000"}, new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"}, new String[]{"Cuba", "Havana", "North America", "114524", "10600000"}, new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"}, new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"}, new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"}, new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"}, new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"}, new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"}, new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"}, new String[]{"Peru", "Lima", "South America", "1285215", "21600000"}, new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"}, new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"}, new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"} }; Spire.Doc.Table table = section.AddTable(); table.ResetCells(data.Length + 1, header.Length); // ***************** First Row ************************* TableRow row = table.Rows[0]; row.IsHeader = true; row.Height = 20; //unit: point, 1point = 0.3528 mm row.HeightType = TableRowHeightType.Exactly; row.RowFormat.BackColor = Color.Gray; for (int i = 0; i < header.Length; i++) { row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; Paragraph p = row.Cells[i].AddParagraph(); p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; TextRange txtRange = p.AppendText(header[i]); txtRange.CharacterFormat.Bold = true; } for (int r = 0; r < data.Length; r++) { TableRow dataRow = table.Rows[r + 1]; dataRow.Height = 20; dataRow.HeightType = TableRowHeightType.Exactly; dataRow.RowFormat.BackColor = Color.Empty; for (int c = 0; c < data[r].Length; c++) { dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; dataRow.Cells[c].AddParagraph().AppendText(data[r][c]); } } }
通過上面三個簡單的例子,粗略的了解了Spire.Doc。下面就我個人對DocX和Spire.Doc使用,列出兩種優(yōu)缺點。
Spire.Doc | DocX | |
API | 介紹簡單 | 無API介紹 |
Demo | 提供了很多Demo方便學(xué)習(xí) | demo少 |
收費 | 收費 | 開源免費 |
功能對比 | 1.支持FormField模版替換 2.Table讀寫功能強大 | 1.對自定義屬性讀寫存在BUG |
兼容性 | 兼容word各版本 | 支持2007即以上版本 |
依賴性 | 不依賴于office,即使服務(wù)器未裝office也能正常操作 |
實際開發(fā)中可以根據(jù)自己需要來選擇使用Sprie.Doc或者DocX。
本文例子Demo下載地址:SpireDoc_Demo
|
新聞熱點
疑難解答