fix lgtm alerts in examples
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842707 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e88fc19ca4
commit
6673fe7ad1
@ -53,7 +53,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
* Excel Conditional Formatting -- Examples
|
||||
*
|
||||
* <p>
|
||||
* Partly based on the code snippets from
|
||||
* Partly based on the code snippets from
|
||||
* http://www.contextures.com/xlcondformat03.html
|
||||
* </p>
|
||||
*/
|
||||
@ -89,7 +89,7 @@ public class ConditionalFormats {
|
||||
|
||||
// print overlapping rule results
|
||||
evaluateRules(wb, "Overlapping");
|
||||
|
||||
|
||||
// Write the output to a file
|
||||
String file = "cf-poi.xls";
|
||||
if(wb instanceof XSSFWorkbook) {
|
||||
@ -178,11 +178,11 @@ public class ConditionalFormats {
|
||||
|
||||
sheet.getRow(2).createCell(4).setCellValue("<== Condition 1: Formula Is =$B2>75 (Blue Fill)");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Multiple conditional formatting rules can apply to
|
||||
* one cell, some combining, some beating others.
|
||||
* Done in order of the rules added to the
|
||||
* Done in order of the rules added to the
|
||||
* SheetConditionalFormatting object
|
||||
*/
|
||||
static void overlapping(Sheet sheet) {
|
||||
@ -210,39 +210,39 @@ public class ConditionalFormats {
|
||||
}
|
||||
sheet.autoSizeColumn(0);
|
||||
sheet.autoSizeColumn(1);
|
||||
|
||||
|
||||
sheet.getRow(1).createCell(3).setCellValue("Even rows are blue");
|
||||
sheet.getRow(2).createCell(3).setCellValue("Multiples of 3 have a grey background");
|
||||
sheet.getRow(4).createCell(3).setCellValue("Multiples of 5 are bold");
|
||||
sheet.getRow(9).createCell(3).setCellValue("Multiples of 10 are red (beats even)");
|
||||
|
||||
|
||||
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
|
||||
|
||||
|
||||
// Condition 1: Row divides by 10, red (will beat #1)
|
||||
ConditionalFormattingRule rule1 =
|
||||
ConditionalFormattingRule rule1 =
|
||||
sheetCF.createConditionalFormattingRule("MOD(ROW(),10)=0");
|
||||
FontFormatting font1 = rule1.createFontFormatting();
|
||||
font1.setFontColorIndex(IndexedColors.RED.index);
|
||||
|
||||
|
||||
// Condition 2: Row is even, blue
|
||||
ConditionalFormattingRule rule2 =
|
||||
ConditionalFormattingRule rule2 =
|
||||
sheetCF.createConditionalFormattingRule("MOD(ROW(),2)=0");
|
||||
FontFormatting font2 = rule2.createFontFormatting();
|
||||
font2.setFontColorIndex(IndexedColors.BLUE.index);
|
||||
|
||||
|
||||
// Condition 3: Row divides by 5, bold
|
||||
ConditionalFormattingRule rule3 =
|
||||
ConditionalFormattingRule rule3 =
|
||||
sheetCF.createConditionalFormattingRule("MOD(ROW(),5)=0");
|
||||
FontFormatting font3 = rule3.createFontFormatting();
|
||||
font3.setFontStyle(false, true);
|
||||
|
||||
|
||||
// Condition 4: Row divides by 3, grey background
|
||||
ConditionalFormattingRule rule4 =
|
||||
ConditionalFormattingRule rule4 =
|
||||
sheetCF.createConditionalFormattingRule("MOD(ROW(),3)=0");
|
||||
PatternFormatting fill4 = rule4.createPatternFormatting();
|
||||
fill4.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.index);
|
||||
fill4.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
|
||||
|
||||
|
||||
// Apply
|
||||
CellRangeAddress[] regions = {
|
||||
CellRangeAddress.valueOf("A1:F41")
|
||||
@ -441,7 +441,7 @@ public class ConditionalFormats {
|
||||
}
|
||||
|
||||
/**
|
||||
* You can use Excel conditional formatting to shade bands of rows on the worksheet.
|
||||
* You can use Excel conditional formatting to shade bands of rows on the worksheet.
|
||||
* In this example, 3 rows are shaded light grey, and 3 are left with no shading.
|
||||
* In the MOD function, the total number of rows in the set of banded rows (6) is entered.
|
||||
*/
|
||||
@ -462,7 +462,7 @@ public class ConditionalFormats {
|
||||
sheet.createRow(0).createCell(1).setCellValue("Shade Bands of Rows");
|
||||
sheet.createRow(1).createCell(1).setCellValue("Condition: Formula Is =MOD(ROW(),6)<2 (Light Grey Fill)");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Icon Sets / Multi-States allow you to have icons shown which vary
|
||||
* based on the values, eg Red traffic light / Yellow traffic light /
|
||||
@ -487,7 +487,7 @@ public class ConditionalFormats {
|
||||
r.createCell(3).setCellValue(10);
|
||||
|
||||
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
|
||||
|
||||
|
||||
CellRangeAddress[] regions = { CellRangeAddress.valueOf("B1:B4") };
|
||||
ConditionalFormattingRule rule1 =
|
||||
sheetCF.createConditionalFormattingRule(IconSet.GYR_3_TRAFFIC_LIGHTS);
|
||||
@ -497,7 +497,7 @@ public class ConditionalFormats {
|
||||
im1.getThresholds()[1].setValue(33d);
|
||||
im1.getThresholds()[2].setRangeType(RangeType.MAX);
|
||||
sheetCF.addConditionalFormatting(regions, rule1);
|
||||
|
||||
|
||||
regions = new CellRangeAddress[] { CellRangeAddress.valueOf("C1:C4") };
|
||||
ConditionalFormattingRule rule2 =
|
||||
sheetCF.createConditionalFormattingRule(IconSet.GYR_3_FLAGS);
|
||||
@ -509,7 +509,7 @@ public class ConditionalFormats {
|
||||
im2.getThresholds()[2].setRangeType(RangeType.PERCENT);
|
||||
im2.getThresholds()[2].setValue(67d);
|
||||
sheetCF.addConditionalFormatting(regions, rule2);
|
||||
|
||||
|
||||
regions = new CellRangeAddress[] { CellRangeAddress.valueOf("D1:D4") };
|
||||
ConditionalFormattingRule rule3 =
|
||||
sheetCF.createConditionalFormattingRule(IconSet.GYR_3_SYMBOLS_CIRCLE);
|
||||
@ -522,7 +522,7 @@ public class ConditionalFormats {
|
||||
im3.getThresholds()[2].setValue(7d);
|
||||
sheetCF.addConditionalFormatting(regions, rule3);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Color Scales / Colour Scales / Colour Gradients allow you shade the
|
||||
* background colour of the cell based on the values, eg from Red to
|
||||
@ -533,12 +533,12 @@ public class ConditionalFormats {
|
||||
Row r = sheet.createRow(1);
|
||||
r.createCell(0).setCellValue("Red-Yellow-Green");
|
||||
for (int i=1; i<=7; i++) {
|
||||
r.createCell(i).setCellValue((i-1)*5);
|
||||
r.createCell(i).setCellValue((i-1)*5.0);
|
||||
}
|
||||
r = sheet.createRow(2);
|
||||
r.createCell(0).setCellValue("Red-White-Blue");
|
||||
for (int i=1; i<=9; i++) {
|
||||
r.createCell(i).setCellValue((i-1)*5);
|
||||
r.createCell(i).setCellValue((i-1)*5.0);
|
||||
}
|
||||
r = sheet.createRow(3);
|
||||
r.createCell(0).setCellValue("Blue-Green");
|
||||
@ -546,9 +546,9 @@ public class ConditionalFormats {
|
||||
r.createCell(i).setCellValue((i-1));
|
||||
}
|
||||
sheet.setColumnWidth(0, 5000);
|
||||
|
||||
|
||||
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
|
||||
|
||||
|
||||
CellRangeAddress[] regions = { CellRangeAddress.valueOf("B2:H2") };
|
||||
ConditionalFormattingRule rule1 =
|
||||
sheetCF.createConditionalFormattingColorScaleRule();
|
||||
@ -561,7 +561,7 @@ public class ConditionalFormats {
|
||||
((ExtendedColor)cs1.getColors()[1]).setARGBHex("FFFFEB84");
|
||||
((ExtendedColor)cs1.getColors()[2]).setARGBHex("FF63BE7B");
|
||||
sheetCF.addConditionalFormatting(regions, rule1);
|
||||
|
||||
|
||||
regions = new CellRangeAddress[] { CellRangeAddress.valueOf("B3:J3") };
|
||||
ConditionalFormattingRule rule2 =
|
||||
sheetCF.createConditionalFormattingColorScaleRule();
|
||||
@ -574,7 +574,7 @@ public class ConditionalFormats {
|
||||
((ExtendedColor)cs2.getColors()[1]).setARGBHex("FFFCFCFF");
|
||||
((ExtendedColor)cs2.getColors()[2]).setARGBHex("FF5A8AC6");
|
||||
sheetCF.addConditionalFormatting(regions, rule2);
|
||||
|
||||
|
||||
regions = new CellRangeAddress[] { CellRangeAddress.valueOf("B4:Q4") };
|
||||
ConditionalFormattingRule rule3=
|
||||
sheetCF.createConditionalFormattingColorScaleRule();
|
||||
@ -586,7 +586,7 @@ public class ConditionalFormats {
|
||||
((ExtendedColor)cs3.getColors()[1]).setARGBHex("FF63BE7B");
|
||||
sheetCF.addConditionalFormatting(regions, rule3);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* DataBars / Data-Bars allow you to have bars shown vary
|
||||
* based on the values, from full to empty
|
||||
@ -623,7 +623,7 @@ public class ConditionalFormats {
|
||||
sheet.setColumnWidth(3, 5000);
|
||||
|
||||
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
|
||||
|
||||
|
||||
ExtendedColor color = sheet.getWorkbook().getCreationHelper().createExtendedColor();
|
||||
color.setARGBHex("FF63BE7B");
|
||||
CellRangeAddress[] regions = { CellRangeAddress.valueOf("B2:B7") };
|
||||
@ -632,7 +632,7 @@ public class ConditionalFormats {
|
||||
db1.getMinThreshold().setRangeType(RangeType.MIN);
|
||||
db1.getMaxThreshold().setRangeType(RangeType.MAX);
|
||||
sheetCF.addConditionalFormatting(regions, rule1);
|
||||
|
||||
|
||||
color = sheet.getWorkbook().getCreationHelper().createExtendedColor();
|
||||
color.setARGBHex("FF5A8AC6");
|
||||
regions = new CellRangeAddress[] { CellRangeAddress.valueOf("C2:C7") };
|
||||
@ -641,7 +641,7 @@ public class ConditionalFormats {
|
||||
db2.getMinThreshold().setRangeType(RangeType.MIN);
|
||||
db2.getMaxThreshold().setRangeType(RangeType.MAX);
|
||||
sheetCF.addConditionalFormatting(regions, rule2);
|
||||
|
||||
|
||||
color = sheet.getWorkbook().getCreationHelper().createExtendedColor();
|
||||
color.setARGBHex("FFF8696B");
|
||||
regions = new CellRangeAddress[] { CellRangeAddress.valueOf("D2:D7") };
|
||||
@ -651,7 +651,7 @@ public class ConditionalFormats {
|
||||
db3.getMaxThreshold().setRangeType(RangeType.MAX);
|
||||
sheetCF.addConditionalFormatting(regions, rule3);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Print out a summary of the conditional formatting rules applied to cells on the given sheet.
|
||||
* Only cells with a matching rule are printed, and for those, all matching rules are sumarized.
|
||||
@ -661,15 +661,19 @@ public class ConditionalFormats {
|
||||
final ConditionalFormattingEvaluator cfEval = new ConditionalFormattingEvaluator(wb, wbEvalProv);
|
||||
// if cell values have changed, clear cached format results
|
||||
cfEval.clearAllCachedValues();
|
||||
|
||||
|
||||
final Sheet sheet = wb.getSheet(sheetName);
|
||||
for (Row r : sheet) {
|
||||
for (Cell c : r) {
|
||||
final List<EvaluationConditionalFormatRule> rules = cfEval.getConditionalFormattingForCell(c);
|
||||
// check rules list for null, although current implementation will return an empty list, not null, then do what you want with results
|
||||
if (rules == null || rules.isEmpty()) continue;
|
||||
if (rules == null || rules.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
final CellReference ref = ConditionalFormattingEvaluator.getRef(c);
|
||||
if (rules.isEmpty()) continue;
|
||||
if (rules.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println("\n"
|
||||
+ ref.formatAsString()
|
||||
@ -682,7 +686,7 @@ public class ConditionalFormats {
|
||||
b.append("\tRule ")
|
||||
.append(rule.getFormattingIndex())
|
||||
.append(": ");
|
||||
|
||||
|
||||
// check for color scale
|
||||
if (cf.getColorScaleFormatting() != null) {
|
||||
b.append("\n\t\tcolor scale (caller must calculate bucket)");
|
||||
@ -709,13 +713,19 @@ public class ConditionalFormats {
|
||||
b.append("\n\t\tfont format ")
|
||||
.append("color index ")
|
||||
.append(ff.getFontColorIndex());
|
||||
if (ff.isBold()) b.append(" bold");
|
||||
if (ff.isItalic()) b.append(" italic");
|
||||
if (ff.isStruckout()) b.append(" strikeout");
|
||||
if (ff.isBold()) {
|
||||
b.append(" bold");
|
||||
}
|
||||
if (ff.isItalic()) {
|
||||
b.append(" italic");
|
||||
}
|
||||
if (ff.isStruckout()) {
|
||||
b.append(" strikeout");
|
||||
}
|
||||
b.append(" underline index ")
|
||||
.append(ff.getUnderlineType());
|
||||
}
|
||||
|
||||
|
||||
System.out.println(b);
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,6 @@ import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.ss.format.CellFormat;
|
||||
import org.apache.poi.ss.format.CellFormatResult;
|
||||
import org.apache.poi.ss.usermodel.BorderStyle;
|
||||
@ -96,10 +95,10 @@ public class ToHtml {
|
||||
BorderStyle.SLANTED_DASH_DOT, "dashed 2pt",
|
||||
BorderStyle.THICK, "solid 3pt",
|
||||
BorderStyle.THIN, "dashed 1pt");
|
||||
|
||||
|
||||
private static final int IDX_TABLE_WIDTH = -2;
|
||||
private static final int IDX_HEADER_COL_WIDTH = -1;
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
private static <K, V> Map<K, V> mapFor(Object... mapping) {
|
||||
@ -189,9 +188,14 @@ public class ToHtml {
|
||||
return;
|
||||
}
|
||||
|
||||
ToHtml toHtml = create(args[0], new PrintWriter(new FileWriter(args[1])));
|
||||
toHtml.setCompleteHTML(true);
|
||||
toHtml.printPage();
|
||||
try (
|
||||
FileWriter fw = new FileWriter(args[1]);
|
||||
PrintWriter pw = new PrintWriter(fw)
|
||||
) {
|
||||
ToHtml toHtml = create(args[0], pw);
|
||||
toHtml.setCompleteHTML(true);
|
||||
toHtml.printPage();
|
||||
}
|
||||
}
|
||||
|
||||
public void setCompleteHTML(boolean completeHTML) {
|
||||
@ -350,32 +354,32 @@ public class ToHtml {
|
||||
public void printSheet(Sheet sheet) {
|
||||
ensureOut();
|
||||
Map<Integer, Integer> widths = computeWidths(sheet);
|
||||
int tableWidth = widths.get(IDX_TABLE_WIDTH);
|
||||
int tableWidth = widths.get(IDX_TABLE_WIDTH);
|
||||
out.format("<table class=%s style=\"width:%dpx;\">%n", DEFAULTS_CLASS, tableWidth);
|
||||
printCols(widths);
|
||||
printSheetContent(sheet);
|
||||
out.format("</table>%n");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* computes the column widths, defined by the sheet.
|
||||
*
|
||||
* computes the column widths, defined by the sheet.
|
||||
*
|
||||
* @param sheet The sheet for which to compute widths
|
||||
* @return Map with key: column index; value: column width in pixels
|
||||
* <br>special keys:
|
||||
* <br>special keys:
|
||||
* <br>{@link #IDX_HEADER_COL_WIDTH} - width of the header column
|
||||
* <br>{@link #IDX_TABLE_WIDTH} - width of the entire table
|
||||
* <br>{@link #IDX_TABLE_WIDTH} - width of the entire table
|
||||
*/
|
||||
private Map<Integer, Integer> computeWidths(Sheet sheet) {
|
||||
Map<Integer, Integer> ret = new TreeMap<>();
|
||||
int tableWidth = 0;
|
||||
|
||||
ensureColumnBounds(sheet);
|
||||
|
||||
|
||||
// compute width of the header column
|
||||
int lastRowNum = sheet.getLastRowNum();
|
||||
int headerCharCount = String.valueOf(lastRowNum).length();
|
||||
int headerColWidth = widthToPixels((headerCharCount + 1) * 256);
|
||||
int headerColWidth = widthToPixels((headerCharCount + 1) * 256.0);
|
||||
ret.put(IDX_HEADER_COL_WIDTH, headerColWidth);
|
||||
tableWidth += headerColWidth;
|
||||
|
||||
@ -384,11 +388,11 @@ public class ToHtml {
|
||||
ret.put(i, colWidth);
|
||||
tableWidth += colWidth;
|
||||
}
|
||||
|
||||
|
||||
ret.put(IDX_TABLE_WIDTH, tableWidth);
|
||||
return ret ;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Probably platform-specific, but appears to be a close approximation on some systems
|
||||
* @param widthUnits POI's native width unit (twips)
|
||||
|
@ -63,7 +63,7 @@ public class BarChart {
|
||||
row = sheet.createRow((short) rowIndex);
|
||||
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
|
||||
cell = row.createCell((short) colIndex);
|
||||
cell.setCellValue(colIndex * (rowIndex + 1));
|
||||
cell.setCellValue(colIndex * (rowIndex + 1.0));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,18 +58,18 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
* <p>
|
||||
* If you really want to use this approach, which is also the one that SXSSF
|
||||
* does for you, it works as follows:
|
||||
*
|
||||
*
|
||||
* 1. create a template workbook, create sheets and global objects such as cell styles, number formats, etc.
|
||||
* 2. create an application that streams data in a text file
|
||||
* 3. Substitute the sheet in the template with the generated data
|
||||
*
|
||||
* <p>
|
||||
* Since 3.8 POI provides a low-memory footprint SXSSF API, which implements
|
||||
* Since 3.8 POI provides a low-memory footprint SXSSF API, which implements
|
||||
* ths "BigGridDemo" strategy. SXSSF is an API-compatible streaming extension
|
||||
* of XSSF to be used when very large spreadsheets have to be produced, and
|
||||
* heap space is limited. SXSSF achieves its low memory footprint by limiting
|
||||
* access to the rows that are within a sliding window, while XSSF gives access
|
||||
* to all rows in the document. Older rows that are no longer in the window
|
||||
* of XSSF to be used when very large spreadsheets have to be produced, and
|
||||
* heap space is limited. SXSSF achieves its low memory footprint by limiting
|
||||
* access to the rows that are within a sliding window, while XSSF gives access
|
||||
* to all rows in the document. Older rows that are no longer in the window
|
||||
* become inaccessible, as they are written to the disk.
|
||||
* </p>
|
||||
* See <a "http://poi.apache.org/spreadsheet/how-to.html#sxssf">
|
||||
@ -79,7 +79,7 @@ public final class BigGridDemo {
|
||||
private static final String XML_ENCODING = "UTF-8";
|
||||
|
||||
private BigGridDemo() {}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// Step 1. Create a template file. Setup sheets and workbook-level objects such as
|
||||
@ -99,7 +99,10 @@ public final class BigGridDemo {
|
||||
|
||||
//Step 2. Generate XML file.
|
||||
File tmp = File.createTempFile("sheet", ".xml");
|
||||
try (Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING)) {
|
||||
try (
|
||||
FileOutputStream stream = new FileOutputStream(tmp);
|
||||
Writer fw = new OutputStreamWriter(stream, XML_ENCODING)
|
||||
) {
|
||||
generate(fw, styles);
|
||||
}
|
||||
|
||||
@ -265,7 +268,9 @@ public final class BigGridDemo {
|
||||
public void createCell(int columnIndex, String value, int styleIndex) throws IOException {
|
||||
String ref = new CellReference(_rownum, columnIndex).formatAsString();
|
||||
_out.write("<c r=\""+ref+"\" t=\"inlineStr\"");
|
||||
if(styleIndex != -1) _out.write(" s=\""+styleIndex+"\"");
|
||||
if(styleIndex != -1) {
|
||||
_out.write(" s=\""+styleIndex+"\"");
|
||||
}
|
||||
_out.write(">");
|
||||
_out.write("<is><t>"+value+"</t></is>");
|
||||
_out.write("</c>");
|
||||
@ -278,7 +283,9 @@ public final class BigGridDemo {
|
||||
public void createCell(int columnIndex, double value, int styleIndex) throws IOException {
|
||||
String ref = new CellReference(_rownum, columnIndex).formatAsString();
|
||||
_out.write("<c r=\""+ref+"\" t=\"n\"");
|
||||
if(styleIndex != -1) _out.write(" s=\""+styleIndex+"\"");
|
||||
if(styleIndex != -1) {
|
||||
_out.write(" s=\""+styleIndex+"\"");
|
||||
}
|
||||
_out.write(">");
|
||||
_out.write("<v>"+value+"</v>");
|
||||
_out.write("</c>");
|
||||
|
@ -33,14 +33,18 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
* Demonstrates how to create a simple table using Apache POI.
|
||||
*/
|
||||
public class CreateTable {
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = (XSSFSheet) wb.createSheet();
|
||||
|
||||
// Set which area the table should be placed in
|
||||
AreaReference reference = wb.getCreationHelper().createAreaReference(
|
||||
new CellReference(0, 0), new CellReference(2, 2));
|
||||
|
||||
// Create
|
||||
XSSFTable table = sheet.createTable();
|
||||
XSSFTable table = sheet.createTable(reference);
|
||||
table.setName("Test");
|
||||
table.setDisplayName("Test_Table");
|
||||
|
||||
@ -70,7 +74,7 @@ public class CreateTable {
|
||||
if (i == 0) {
|
||||
cell.setCellValue("Column" + (j + 1));
|
||||
} else {
|
||||
cell.setCellValue((i + 1) * (j + 1));
|
||||
cell.setCellValue((i + 1.0) * (j + 1.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -79,11 +83,6 @@ public class CreateTable {
|
||||
table.createColumn("Column 2");
|
||||
table.createColumn("Column 3");
|
||||
|
||||
// Set which area the table should be placed in
|
||||
AreaReference reference = wb.getCreationHelper().createAreaReference(
|
||||
new CellReference(0, 0), new CellReference(2, 2));
|
||||
table.setCellReferences(reference);
|
||||
|
||||
// Save
|
||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
|
@ -32,7 +32,10 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
public class IterateCells {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
try (Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]))) {
|
||||
try (
|
||||
FileInputStream is = new FileInputStream(args[0]);
|
||||
Workbook wb = new XSSFWorkbook(is)
|
||||
) {
|
||||
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
|
||||
Sheet sheet = wb.getSheetAt(i);
|
||||
System.out.println(wb.getSheetName(i));
|
||||
|
@ -57,26 +57,26 @@ public class LineChart {
|
||||
row = sheet.createRow((short) rowIndex);
|
||||
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
|
||||
cell = row.createCell((short) colIndex);
|
||||
cell.setCellValue(colIndex * (rowIndex + 1));
|
||||
cell.setCellValue(colIndex * (rowIndex + 1.0));
|
||||
}
|
||||
}
|
||||
|
||||
XSSFDrawing drawing = sheet.createDrawingPatriarch();
|
||||
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
|
||||
|
||||
|
||||
XSSFChart chart = drawing.createChart(anchor);
|
||||
XDDFChartLegend legend = chart.getOrAddLegend();
|
||||
legend.setPosition(LegendPosition.TOP_RIGHT);
|
||||
|
||||
|
||||
// Use a category axis for the bottom axis.
|
||||
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
|
||||
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
|
||||
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
|
||||
|
||||
|
||||
XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
|
||||
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
|
||||
XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
|
||||
|
||||
|
||||
XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
|
||||
data.addSeries(xs, ys1);
|
||||
data.addSeries(xs, ys2);
|
||||
|
@ -59,28 +59,28 @@ public class ScatterChart {
|
||||
row = sheet.createRow((short) rowIndex);
|
||||
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
|
||||
cell = row.createCell((short) colIndex);
|
||||
cell.setCellValue(colIndex * (rowIndex + 1));
|
||||
cell.setCellValue(colIndex * (rowIndex + 1.0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
XSSFDrawing drawing = sheet.createDrawingPatriarch();
|
||||
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
|
||||
|
||||
|
||||
XSSFChart chart = drawing.createChart(anchor);
|
||||
XDDFChartLegend legend = chart.getOrAddLegend();
|
||||
legend.setPosition(LegendPosition.TOP_RIGHT);
|
||||
|
||||
|
||||
XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
|
||||
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
|
||||
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
|
||||
|
||||
|
||||
XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
|
||||
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
|
||||
XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
|
||||
|
||||
|
||||
|
||||
|
||||
XDDFChartData data = chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);
|
||||
|
||||
|
||||
data.addSeries(xs, ys1);
|
||||
data.addSeries(xs, ys2);
|
||||
chart.plot(data);
|
||||
|
@ -111,16 +111,16 @@ public class SimpleDocument {
|
||||
r5.setTextPosition(-10);
|
||||
r5.setText("For in that sleep of death what dreams may come");
|
||||
r5.addCarriageReturn();
|
||||
r5.setText("When we have shuffled off this mortal coil,"
|
||||
+ "Must give us pause: there's the respect"
|
||||
r5.setText("When we have shuffled off this mortal coil, "
|
||||
+ "Must give us pause: there's the respect "
|
||||
+ "That makes calamity of so long life;");
|
||||
r5.addBreak();
|
||||
r5.setText("For who would bear the whips and scorns of time,"
|
||||
r5.setText("For who would bear the whips and scorns of time, "
|
||||
+ "The oppressor's wrong, the proud man's contumely,");
|
||||
|
||||
r5.addBreak(BreakClear.ALL);
|
||||
r5.setText("The pangs of despised love, the law's delay,"
|
||||
+ "The insolence of office and the spurns" + ".......");
|
||||
r5.setText("The pangs of despised love, the law's delay, "
|
||||
+ "The insolence of office and the spurns " + ".......");
|
||||
|
||||
try (FileOutputStream out = new FileOutputStream("simple.docx")) {
|
||||
doc.write(out);
|
||||
|
@ -43,18 +43,29 @@ public class SimpleImages {
|
||||
for (String imgFile : args) {
|
||||
int format;
|
||||
|
||||
if (imgFile.endsWith(".emf")) format = XWPFDocument.PICTURE_TYPE_EMF;
|
||||
else if (imgFile.endsWith(".wmf")) format = XWPFDocument.PICTURE_TYPE_WMF;
|
||||
else if (imgFile.endsWith(".pict")) format = XWPFDocument.PICTURE_TYPE_PICT;
|
||||
else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) format = XWPFDocument.PICTURE_TYPE_JPEG;
|
||||
else if (imgFile.endsWith(".png")) format = XWPFDocument.PICTURE_TYPE_PNG;
|
||||
else if (imgFile.endsWith(".dib")) format = XWPFDocument.PICTURE_TYPE_DIB;
|
||||
else if (imgFile.endsWith(".gif")) format = XWPFDocument.PICTURE_TYPE_GIF;
|
||||
else if (imgFile.endsWith(".tiff")) format = XWPFDocument.PICTURE_TYPE_TIFF;
|
||||
else if (imgFile.endsWith(".eps")) format = XWPFDocument.PICTURE_TYPE_EPS;
|
||||
else if (imgFile.endsWith(".bmp")) format = XWPFDocument.PICTURE_TYPE_BMP;
|
||||
else if (imgFile.endsWith(".wpg")) format = XWPFDocument.PICTURE_TYPE_WPG;
|
||||
else {
|
||||
if (imgFile.endsWith(".emf")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_EMF;
|
||||
} else if (imgFile.endsWith(".wmf")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_WMF;
|
||||
} else if (imgFile.endsWith(".pict")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_PICT;
|
||||
} else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_JPEG;
|
||||
} else if (imgFile.endsWith(".png")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_PNG;
|
||||
} else if (imgFile.endsWith(".dib")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_DIB;
|
||||
} else if (imgFile.endsWith(".gif")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_GIF;
|
||||
} else if (imgFile.endsWith(".tiff")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_TIFF;
|
||||
} else if (imgFile.endsWith(".eps")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_EPS;
|
||||
} else if (imgFile.endsWith(".bmp")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_BMP;
|
||||
} else if (imgFile.endsWith(".wpg")) {
|
||||
format = XWPFDocument.PICTURE_TYPE_WPG;
|
||||
} else {
|
||||
System.err.println("Unsupported picture: " + imgFile +
|
||||
". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
|
||||
continue;
|
||||
@ -62,7 +73,9 @@ public class SimpleImages {
|
||||
|
||||
r.setText(imgFile);
|
||||
r.addBreak();
|
||||
r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
|
||||
try (FileInputStream is = new FileInputStream(imgFile)) {
|
||||
r.addPicture(is, format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
|
||||
}
|
||||
r.addBreak(BreakType.PAGE);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user