Table 在 Word 文档中有 n 列以适应页面大小并允许截断的列在页面上使用 Aspose.words for .Net
Table in Word document with n columns to fit as per page size and allow truncated columns to break across page using Aspose.words for .Net
我正在使用 Aspose.Words(评估模式)为 .Net 生成一个 word 文档,我正在其中构建一个 table,如下所示
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();
现在这段代码给出了以下带有不可见列的 word 文件,它应该给出 20 列
.
有什么方法可以将不可见的列分到下一页吗?
行可以转到下一页,而不是列,这是 Microsoft Word 的行为。您可以更改文档的设计和格式以使所有列可见。以下是一些提示。
- 缩小页边距(左右)
- 固定单元格宽度。这样,如果找到更多字符,每个单元格内的文本将向下分解。
- 将方向更改为横向,您将拥有更宽的页面。
查看 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 工作,担任开发人员布道师。
我正在使用 Aspose.Words(评估模式)为 .Net 生成一个 word 文档,我正在其中构建一个 table,如下所示
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();
现在这段代码给出了以下带有不可见列的 word 文件,它应该给出 20 列
有什么方法可以将不可见的列分到下一页吗?
行可以转到下一页,而不是列,这是 Microsoft Word 的行为。您可以更改文档的设计和格式以使所有列可见。以下是一些提示。
- 缩小页边距(左右)
- 固定单元格宽度。这样,如果找到更多字符,每个单元格内的文本将向下分解。
- 将方向更改为横向,您将拥有更宽的页面。
查看 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 工作,担任开发人员布道师。