【发布时间】:2013-01-22 21:29:19
【问题描述】:
我使用 Open Xml 创建了一个 Word 文档。创建 Web 部件中的按钮时会创建文档。目前我已经在文档中创建了一个表格来测试它是否有效。我现在要做的是能够为新创建的文档设置页边距。
我不确定如何继续。实现这一目标的最简单方法是什么?
(下面是我当前创建的带有表格的word文档的代码)
void GenerateBadges_Click(object sender, EventArgs e)
{
//Creating a word document using the the Open XML SDK 2.0
WordprocessingDocument document = WordprocessingDocument.Create(@"C:\Users\Daniel.Perez
\Documents\sample-badges.docx", WordprocessingDocumentType.Document);
//create a paragraph
MainDocumentPart mainDocumenPart = document.AddMainDocumentPart();
mainDocumenPart.Document = new Document();
Body documentBody = new Body();
mainDocumenPart.Document.Append(documentBody);
//adding a table to the document
Table table = new Table();
TableProperties tblProps = new TableProperties();
TableBorders tblBorders = new TableBorders();
tblBorders.TopBorder = new TopBorder();
tblBorders.TopBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
tblBorders.BottomBorder = new BottomBorder();
tblBorders.BottomBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
tblBorders.RightBorder = new RightBorder();
tblBorders.RightBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
tblBorders.LeftBorder = new LeftBorder();
tblBorders.LeftBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
tblBorders.InsideHorizontalBorder = new InsideHorizontalBorder();
tblBorders.InsideHorizontalBorder.Val = BorderValues.Single;
tblBorders.InsideVerticalBorder = new InsideVerticalBorder();
tblBorders.InsideVerticalBorder.Val = BorderValues.Single;
tblProps.Append(tblBorders);
table.Append(tblProps);
TableRow row;
TableCell cell;
//first table row
row = new TableRow();
cell = new TableCell(new Paragraph(new Run(new Text("Table to hold the badges"))));
TableCellProperties cellProp = new TableCellProperties();
GridSpan gridSpan = new GridSpan();
gridSpan.Val = 11;
cellProp.Append(gridSpan);
cell.Append(cellProp);
row.Append(cell);
table.Append(row);
//second row
row = new TableRow();
cell = new TableCell();
cell.Append(new Paragraph(new Run(new Text("Inner Table"))));
row.Append(cell);
for (int i = 1; i <= 10; i++)
{
row.Append(new TableCell(new Paragraph (new Run(new Text(i.ToString())))));
}
table.Append(row);
for (int i = 1; i <= 10; i++)
{
row = new TableRow();
row.Append(new TableCell(new Paragraph(new Run(new Text(i.ToString())))));
for (int j = 1; j <= 10; j++)
{
row.Append(new TableCell(new Paragraph(new Run(new Text((i * j).ToString())))));
}
table.Append(row);
}
//add the table to the document - table needs to be wired into the for each loop above
documentBody.Append(table);
//Saving/Disposing of the created word Document
document.MainDocumentPart.Document.Save();
document.Dispose();
}
任何建议将不胜感激。提前致谢
【问题讨论】:
-
你试过PageMargin吗? msdn.microsoft.com/en-us/library/…
-
这显示了如何在开放的 Xml 中执行此操作,我有兴趣使用 c# 来通过代码完成
-
示例中提到的“PageSetup”没有被visual studio 2010识别有什么想法吗?
标签: c# sharepoint-2010 ms-word openxml-sdk