您好,
我透過 Aspose.Words.Tables 跨頁 Table, 設定 Border ,輸出 ODT 檔時,第一頁的 Bottom Border 不會顯示出來,我同時輸出 docx 及 odt 檔,只有 odt 檔第一頁的 bottom Border 不會出現哦!
狀況如附件,
asposeTableODTVSDOCX.png (174.5 KB)
程式如下,
static void Main(string[] args)
{
var lc = new License();
lc.SetLicense(@"C:\projects\Aspose.lic");
var prog = new Program();
prog.PrintMergeColumnTable();
}
public void PrintMergeColumnTable()
{
#region 初始設定
Document doc = new Document();
doc.RemoveAllChildren();
#endregion
#region 報表第一頁設定
Section section = new Section(doc);
doc.AppendChild(section);
// 設定邊界
section.PageSetup.TopMargin = 42.5;
section.PageSetup.BottomMargin = 42.5;
section.PageSetup.LeftMargin = 42.5;
section.PageSetup.RightMargin = 42.5;
section.PageSetup.SectionStart = SectionStart.NewPage;
// 頁面尺寸
section.PageSetup.PaperSize = PaperSize.A4;
// 頁碼起始設定
section.PageSetup.PageStartingNumber = 1;
// 頁碼格式
section.PageSetup.PageNumberStyle = NumberStyle.Arabic;
Body body = new Body(doc);
section.AppendChild(body);
#region 頁碼
// 頁尾
HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FooterPrimary);
doc.FirstSection.HeadersFooters.Add(footer);
Paragraph para = new Paragraph(doc);
// 放入頁碼
para.InsertField(Aspose.Words.Fields.FieldType.FieldPage, false, null, true);
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
Style footerStyle = doc.Styles.Add(StyleType.Paragraph, "footerStyle");
// 設定頁碼大小字型
footerStyle.Font.Size = 10;
footerStyle.Font.Name = "Times New Roman";
para.ParagraphFormat.Style = footerStyle;
footer.AppendChild(para);
#endregion
#endregion
#region 文件最上方大標題
Paragraph firstPageTitle = new Paragraph(doc);
Style firstPageTitleStyle = doc.Styles.Add(StyleType.Paragraph, "FirstPageTitleStyle");
firstPageTitleStyle.Font.Size = 24;
firstPageTitleStyle.Font.Name = "Times New Roman";
firstPageTitleStyle.Font.Name = "標楷體";
firstPageTitle.ParagraphFormat.SpaceAfter = 12;
firstPageTitle.ParagraphFormat.Alignment = ParagraphAlignment.Center;
firstPageTitle.ParagraphFormat.Style = firstPageTitleStyle;
string firstPageTitleContent = "亂馬縣政府105年度施政目標與重點";
body.AppendChild(firstPageTitle);
firstPageTitle.AppendChild(new Run(doc, firstPageTitleContent));
#region
Paragraph firstPageConten = new Paragraph(doc);
Style firstPageContentStyle = doc.Styles.Add(StyleType.Paragraph, "FirstPageContentStyle");
firstPageContentStyle.Font.Size = 14;
firstPageContentStyle.Font.Name = "標楷體";
firstPageConten.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
firstPageConten.ParagraphFormat.Style = firstPageContentStyle;
string firstPageContentStr = "參、工商旅遊處\r\n一、項目\r\n(一)項目\r\n1、項目";
body.AppendChild(firstPageConten);
firstPageConten.AppendChild(new Run(doc, firstPageContentStr));
#endregion
#endregion
#region 報表第二頁後設定
Section secondSection = new Section(doc);
doc.AppendChild(secondSection);
secondSection.PageSetup.SectionStart = SectionStart.NewPage;
secondSection.PageSetup.TopMargin = 42.5;
secondSection.PageSetup.BottomMargin = 42.5;
secondSection.PageSetup.LeftMargin = 42.5;
secondSection.PageSetup.RightMargin = 42.5;
secondSection.PageSetup.PaperSize = PaperSize.A4;
secondSection.PageSetup.RestartPageNumbering = false;
Body secondBody = new Body(doc);
secondSection.AppendChild(secondBody);
#endregion
#region 表格標題設定
Paragraph docTitle = new Paragraph(doc);
Style secondPageTitleStyle = doc.Styles.Add(StyleType.Paragraph, "SecondPageTitleStyle");
secondPageTitleStyle.ParagraphFormat.SpaceAfter = 12;
secondPageTitleStyle.Font.Size = 16;
secondPageTitleStyle.Font.Name = "標楷體";
secondPageTitleStyle.Font.Bold = true;
docTitle.ParagraphFormat.Alignment = ParagraphAlignment.Center;
docTitle.ParagraphFormat.Style = secondPageTitleStyle;
string firsttitle = "亂馬縣政府105年度重要施政計畫";
secondBody.AppendChild(docTitle);
docTitle.AppendChild(new Run(doc, firsttitle));
#endregion
#region 生產表格
Table table = new Table(doc);
int number = 1;
string[] tabletitle = { "業務別", "重要施政計畫項目", "施政內容", "預算金額\r\n(單位:千元)", "備註" };
Row titleRow = new Row(doc);
table.AppendChild(titleRow);
foreach (var title in tabletitle)
{
Cell cell = new Cell(doc);
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
titleRow.AppendChild(cell);
Paragraph p = new Paragraph(doc);
p.ParagraphFormat.Alignment = ParagraphAlignment.Center;
p.ParagraphFormat.Style.Font.Name = "標楷體";
p.ParagraphFormat.Style.Font.Bold = false;
p.ParagraphFormat.Style.Font.Size = 12;
p.ParagraphFormat.KeepWithNext = false;
cell.AppendChild(p);
cell.FirstParagraph.AppendChild(new Run(doc, title));
}
#region 表格換頁設計
titleRow.RowFormat.HeadingFormat = true;
titleRow.RowFormat.AllowBreakAcrossPages = false;
setRowWidth(titleRow);
#endregion
for (int i = 1; i < 50; i++) // 資料比數 (foreach
{
Row row = new Row(doc);
#region firstList
List firstList = doc.Lists.Add(ListTemplate.NumberDefault);
//
firstList.ListLevels[0].Font.Color = Color.Black;
firstList.ListLevels[0].Alignment = ListLevelAlignment.Center;
firstList.ListLevels[0].Font.Size = 12;
firstList.ListLevels[0].NumberStyle = NumberStyle.TradChinNum2;
firstList.ListLevels[0].TrailingCharacter = ListTrailingCharacter.Nothing;
firstList.ListLevels[0].TextPosition = 0;
#endregion
#region secondList
List secondList = doc.Lists.Add(ListTemplate.NumberDefault);
//
secondList.ListLevels[0].Font.Color = Color.Black;
secondList.ListLevels[0].Alignment = ListLevelAlignment.Center;
secondList.ListLevels[0].Font.Size = 12;
secondList.ListLevels[0].NumberStyle = NumberStyle.TradChinNum1;
secondList.ListLevels[0].TrailingCharacter = ListTrailingCharacter.Nothing;
secondList.ListLevels[0].TextPosition = 0;
#endregion
table.AppendChild(row);
#region cell
for (int j = 0; j < 5; j++) // 欄位數量 (foreach
{
// 新增儲存格
Cell cell = new Cell(doc);
row.AppendChild(cell);
Paragraph cellParagraph = new Paragraph(doc);
cellParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cellParagraph.ParagraphFormat.Style.Font.Name = "標楷體";
cellParagraph.ParagraphFormat.Style.Font.Bold = false;
cellParagraph.ParagraphFormat.Style.Font.Size = 12;
cell.AppendChild(cellParagraph);
string text;
if (j == 0)
{
// 業務別
firstList.ListLevels[0].StartAt = number;
cellParagraph.ListFormat.List = firstList;
number++;
}
else if (j == 1)
{
secondList.ListLevels[0].StartAt = i;
cellParagraph.ListFormat.List = secondList;
}
text = "123";
cell.FirstParagraph.AppendChild(new Run(doc, text));
}
row.RowFormat.AllowBreakAcrossPages = true;
row.RowFormat.HeightRule = HeightRule.AtLeast;
setRowWidth(row);
//EndRow
#endregion
}
// EndTable
SetTableStyle(table);
doc.LastSection.Body.AppendChild(table);
#endregion
doc.Save("mergeColumnTable.odt", SaveFormat.Odt);
doc.Save("mergeColumnTable.docx", SaveFormat.Docx);
}
/// <summary>
/// 設定表格邊線(只有外框線)
/// </summary>
/// <param name="table"></param>
private void SetTableStyle(Table table)
{
table.ClearBorders();
table.Alignment = TableAlignment.Center;
table.SetBorder(BorderType.Left, LineStyle.Single, 1, Color.Black, true);
table.SetBorder(BorderType.Right, LineStyle.Single, 1, Color.Black, true);
table.SetBorder(BorderType.Top, LineStyle.Single, 1, Color.Black, true);
table.SetBorder(BorderType.Bottom, LineStyle.Single, 1, Color.Black, true);
}
/// <summary>
/// 設定欄位寬度
/// </summary>
/// <param name="row"></param>
private void setRowWidth(Row row)
{
// For Odt
row.Cells[0].CellFormat.Width = 90;
row.Cells[1].CellFormat.Width = 110;
row.Cells[2].CellFormat.Width = 180;
row.Cells[3].CellFormat.Width = 90;
row.Cells[4].CellFormat.Width = 50;
}