2012-03-11 46 views

回答

3

如果是我,這是什麼,我會用

DOCX

2

最好的選擇(爲DOCX格式至少)是 http://docx.codeplex.com/

在下面的博客文章,你可以找到的代碼示例比較DocX,Microsoft的OOXML API和經典Office Interop庫的非常簡單的文檔操作: http://cathalscorner.blogspot.com/2010/06/cathal-why-did-you-create-docx.html

+0

在模板文件我已經預先製作的表,我不知道docx有填充預製表的功能。 – user1261700 2012-03-11 06:39:51

+1

絕對,當您使用DocX.Load函數讀取現有文檔時,生成的文檔對象具有可用於處理單元格中的數據的Tables列表。更多信息(這是爲了創建表格,但它顯示了我正在談論的對象)http://cathalscorner.blogspot.com/2010/06/docx-and-tables.html – Dirk 2012-03-11 21:44:35

0

如果您對商業產品感興趣並使用DOCX文件格式,可以試試我們的GemBox.Document組件。

它具有自己的讀/寫引擎和簡單的內容模型,可以使用沒有安裝MS Word。

下面是一個簡單的C#代碼如何創建,將使用郵件合併功能的數據擴展表一個簡單的模板文件:

// Use the component in free mode. 
ComponentInfo.SetLicense("FREE-LIMITED-KEY"); 

// Define DataTable with two columns: 'Name' and 'Surname', and fill it with some data. 
// You don't have to do this if you already have a DataTable instance. 
var dataTable = new DataTable("People") 
{ 
    Columns = 
    { 
     new DataColumn("Name", typeof(string)), 
     new DataColumn("Surname", typeof(string)) 
    }, 
    Rows = 
    { 
     new object[] { "John", "Doe" }, 
     new object[] { "Fred", "Nurk" }, 
     new object[] { "Hans", "Meier" }, 
     new object[] { "Ivan", "Horvat" } 
    } 
}; 

// Create and save a template document. 
// You don't have to do this if you already have a template document. 
// This code is only provided as a reference how template document should look like. 
var document = new DocumentModel(); 
document.Sections.Add(
    new Section(document, 
     new Table(document, 
      new TableRow(document, 
       new TableCell(document, 
        new Paragraph(document, "Name")), 
       new TableCell(document, 
        new Paragraph(document, "Surname"))), 
      new TableRow(document, 
       new TableCell(document, 
        new Paragraph(document, 
         new Field(document, FieldType.MergeField, "RangeStart:People"), 
         new Field(document, FieldType.MergeField, "Name"))), 
       new TableCell(document, 
        new Paragraph(document, 
         new Field(document, FieldType.MergeField, "Surname"), 
         new Field(document, FieldType.MergeField, "RangeEnd:People"))))))); 
document.Save("TemplateDocument.docx", SaveOptions.DocxDefault); 

// Load a template document. 
document = DocumentModel.Load("TemplateDocument.docx", LoadOptions.DocxDefault); 

// Mail merge template document with DataTable. 
// Important: DataTable.TableName and RangeStart/RangeEnd merge field names must match. 
document.MailMerge.ExecuteRange(dataTable); 

// Save the mail merged document. 
document.Save("Document.docx", SaveOptions.DocxDefault); 
相關問題