2015-06-17 30 views
2

我使用Aspose.Words(評估模式)爲.Net生成Word文檔我要建一個表如下在Word文檔中有n列的表格以適合每個頁面大小,並允許截斷列使用Aspose.words打破.Net

Document doc = new Document(); 
    DocumentBuilder builder = new DocumentBuilder(doc); 
    Table table = builder.StartTable(); 
    for (int i = 0; i < 5; i++) 
    { 
     for(int j = 0; j < 20; j++) 
     { 
      builder.InsertCell(); 
      builder.Write("Column : "+ j.toString()); 
     } 
    builder.EndRow(); 
    } 
    builder.EndTable(); 
    doc.Save(ms, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(SaveFormat.Doc)); 
    FileStream file = new FileStream(@"c:\NewDoc.doc", FileMode.Create, FileAccess.Write); 
    ms.WriteTo(file); 
    file.Close(); 
    ms.Close(); 

現在,這個代碼給出了下面的隱形列字的文件,它應該給20列

Table with truncated columns

有什麼辦法可以將不可見的列分解到下一頁?

回答

0

行可以進入下一頁,而不是列,這是Microsoft Word的行爲。您可以更改文檔的設計和格式以使所有列可見。以下是幾點提示。

  1. 減少頁邊距(左和右)
  2. 使細胞固定寬度。這樣,如果找到更多字符,每個單元格內的文本將向下打破。
  3. 改變方向爲風景,你會有一個更寬的頁面。

查看Aspose.Words documentation website的相關文章和代碼示例。

嘗試下面的更新代碼示例:

string dst = dataDir + "table.doc"; 

Aspose.Words.Document doc = new Aspose.Words.Document(); 
DocumentBuilder builder = new DocumentBuilder(doc); 

// Set margins 
doc.FirstSection.PageSetup.LeftMargin = 10; 
//doc.FirstSection.PageSetup.TopMargin = 0; 
doc.FirstSection.PageSetup.RightMargin = 10; 
//doc.FirstSection.PageSetup.BottomMargin = 0; 

// Set oriantation 
doc.FirstSection.PageSetup.Orientation = Aspose.Words.Orientation.Landscape; 

Aspose.Words.Tables.Table table = builder.StartTable(); 

for (int i = 0; i < 5; i++) 
{ 
    for (int j = 0; j < 20; j++) 
    { 
     builder.InsertCell(); 
     // Fixed width 
     builder.CellFormat.Width = ConvertUtil.InchToPoint(0.5); 
     builder.Write("Column : " + j); 
    } 
    builder.EndRow(); 
} 
builder.EndTable(); 

// Set table auto fit behavior to fixed width columns 
table.AutoFit(AutoFitBehavior.FixedColumnWidths); 

doc.Save(dst, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc)); 

我的Aspose工作作爲一個開發者傳播者。

+0

不錯的解決方案,但在我的情況下,我不能使固定寬度的單元格,單元格寬度將根據列標題的字符長度更改,是否可以手動將列分隔到下一頁? –

+0

列無法進入下一頁。你可以把它分成兩個表格。創建第一個表格,添加分頁符並創建第二個表格。 –

相關問題