2013-02-28 70 views
1

我想使用OpenXML WordProcessing製作表格。我想格式化單元格內的字體。這是我的代碼格式字體內部表OpenXML C#

MainDocumentPart mainDocumentPart = doc.AddMainDocumentPart(); 
mainDocumentPart.Document = new Document(); 
Body body = mainDocumentPart.Document.AppendChild(new Body()); 

RunProperties runHeader = new RunProperties(); 
RunFonts runFont = new RunFonts(); 
runFont.Ascii = "Lucida Sans"; 
runHeader.Append(runFont);      
runHeader.Append(new Bold()); 
runHeader.Append(new FontSize() { Val = "16" }); 

//// Create a new table 
Table tbl = new Table(); 

tr = new TableRow(); 
tc = new TableCell(); 

Paragraph paraHeader = new Paragraph(); 
Text heading_text = new Text("Company Name"); 
runHeader.Append(heading_text); 
paraHeader.Append(runHeader); 
tc.Append(paraHeader); 
tr.Append(tc); 
tbl.Append(tr); 

body.AppendChild(tbl); 

但是當我在Microsoft Word上打開時,出現錯誤。它說文件有內容問題

+0

你檢查此鏈接: http://stackoverflow.com/questions/7956630/how-can-i-change-the-font-open-xml – Bryan 2013-02-28 17:24:26

回答

2

您正在將您的文本附加到您的運行屬性,它需要被追加到運行。 嘗試:

Text heading_text = new Text("Company Name"); 

////create the run 
Run runHeaderRun = new Run(); 

////append the run properties and text to the run 
runHeaderRun.Append(runHeader); 
runHeaderRun.Append(heading_text); 

////append the run to the paragraph 
paraHeader.Append(runHeaderRun); 

tc.Append(paraHeader); 
+0

感謝您的答案:d – l1th1um 2013-03-04 05:46:30

0
RunProperties rp = new RunProperties(); 
RunFonts runFont = new RunFonts() { Ascii = "Calibri Light" }; 
rp.Append(runFont); 
rp.Append(new Color() { Val = "#2E74B5" }); 

body.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new Run(rp, new Text("3. Risk Assessment")))); 

這是一個非常簡單的使用,試試這個。