[]
GcExcel导出PDF支持不同的文本格式和字体效果。
你可以通过下面的方式来设置不同的文本格式和字体效果并导出PDF:
要设置并导出包含带有格式的文本,请参阅以下示例代码。
// Create a new workbook and access the default worksheet
Workbook workbook = new Workbook();
IWorksheet sheet = workbook.getWorksheets().get(0);
// Assign values to the range
sheet.getRange("B3:B7").setValue(123456.789);
sheet.getRange("B9:B13").setValue(-123456.789);
// Set number format
sheet.getRange("B4, B10").setNumberFormat("0.00;[Red]0.00");
sheet.getRange("B5, B11").setNumberFormat("$#,##0.00;[Red]$#,##0.00");
sheet.getRange("B6, B12").setNumberFormat("0.00E+00");
sheet.getRange("B7, B13")
.setNumberFormat("_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(@_)");
// Customize other settings
sheet.getColumns().get(1).setColumnWidthInPixel(100);
// Save to a pdf file
workbook.save("ExportNumberFormats.pdf", SaveFileFormat.Pdf);
要设置并导出包含样式的文本,请参阅以下示例代码。
// Settings for overflow text
sheet.getRange("F2, F4").setValue("This is a test string of overflow");
sheet.getRange("F6, F8").setValue("This is a test string of overflow with right alignment");
sheet.getRange("F6, F8").setHorizontalAlignment(HorizontalAlignment.Right);
sheet.getRange("D8, H4").setValue(123);
// Apply style
sheet.getRange("A1:J10").getBorders().setLineStyle(BorderLineStyle.Thin);
// Save to a pdf file
workbook.save("ExportOverflowText.pdf", SaveFileFormat.Pdf);
要设置并导出包含字体效果(如文本对齐、换行、文本缩进、收缩到合适大小、下划线和删除线)的文本,请参阅以下示例代码。
// Set Aligment
sheet.getRange("A1").setValue("Alignment");
sheet.getRange("B2").setValue("Left Alignment");
sheet.getRange("B2").setHorizontalAlignment(HorizontalAlignment.Left);
sheet.getRange("C2").setValue("Center Alignment");
sheet.getRange("C2").setHorizontalAlignment(HorizontalAlignment.Center);
sheet.getRange("D2").setValue("Right Alignment");
sheet.getRange("D2").setHorizontalAlignment(HorizontalAlignment.Right);
sheet.getRange("B3").setValue("Top Alignment");
sheet.getRange("B3").setVerticalAlignment(VerticalAlignment.Top);
sheet.getRange("C3").setValue("Middle Alignment");
sheet.getRange("C3").setVerticalAlignment(VerticalAlignment.Center);
sheet.getRange("D3").setValue("Bottom Alignment");
sheet.getRange("D3").setVerticalAlignment(VerticalAlignment.Bottom);
sheet.getRange("B4").setValue(
"Test String. \nThis is a test string for Justify Alignment. ");
sheet.getRange("B4").setHorizontalAlignment(HorizontalAlignment.Justify);
sheet.getRange("B4").setVerticalAlignment(VerticalAlignment.Justify);
sheet.getRange("C4").setValue(
"Test String. \nThis is a test string for Distributed Alignment. ");
sheet.getRange("C4").setHorizontalAlignment(HorizontalAlignment.Distributed);
sheet.getRange("C4").setVerticalAlignment(VerticalAlignment.Distributed);
// Set wordwrap
sheet.getRange("A6").setValue("Wordwrap");
sheet.getRange("B7").setValue("This is a test string for Wordwrap");
sheet.getRange("C7").setValue("This is a test string \n for Wordwrap");
sheet.getRange("B7:C7").setWrapText(true);
// Set text indent
sheet.getRange("A9").setValue("Indent");
sheet.getRange("B10").setValue("Left Indent");;
sheet.getRange("B10").setIndentLevel(3);
sheet.getRange("C10").setValue("Right Indent");
sheet.getRange("C10").setIndentLevel(3);
sheet.getRange("C10").setHorizontalAlignment(HorizontalAlignment.Right);
// Apply Shrink to fit
sheet.getRange("A12").setValue("Shrink to fit");
sheet.getRange("B13").setValue("This is a test string for \"Shrink to fit\"");
sheet.getRange("B13").setShrinkToFit(true);
// Set Underline
sheet.getRange("A15").setValue("Underline");
sheet.getRange("B16").setValue("Single Underline");
sheet.getRange("B16").getFont().setUnderline(UnderlineType.Single);
// Use Strikethrough
sheet.getRange("A18").setValue("Strikthrough");
sheet.getRange("B19").setValue("Strikthrough");
sheet.getRange("B19").getFont().setStrikethrough(true);
// Customize other settings
sheet.getColumns().get(0).getFont().setBold(true);
sheet.getColumns().get(0).setColumnWidthInPixel(100);
sheet.getColumns().get(1).setColumnWidthInPixel(200);
sheet.getColumns().get(2).setColumnWidthInPixel(245);
sheet.getColumns().get(3).setColumnWidthInPixel(234);
sheet.getRows().get(2).setRowHeightInPixel(72);
sheet.getRows().get(3).setRowHeightInPixel(123);
sheet.getRows().get(6).setRowHeightInPixel(48);
sheet.getRange("A1:D19").getBorders().setLineStyle(BorderLineStyle.Thin);
sheet.getPageSetup().setPaperSize(PaperSize.A3);
// Save to a pdf file
workbook.save("ExportFontEffects.pdf", SaveFileFormat.Pdf);