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:
Alain Béarez 2018-10-03 11:51:41 +00:00
parent e88fc19ca4
commit 6673fe7ad1
10 changed files with 144 additions and 108 deletions

View File

@ -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");
@ -667,9 +667,13 @@ public class ConditionalFormats {
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()
@ -709,9 +713,15 @@ 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());
}

View File

@ -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;
@ -189,10 +188,15 @@ public class ToHtml {
return;
}
ToHtml toHtml = create(args[0], new PrintWriter(new FileWriter(args[1])));
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) {
this.completeHTML = completeHTML;
@ -375,7 +379,7 @@ public class ToHtml {
// 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;

View File

@ -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));
}
}

View File

@ -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>");

View File

@ -39,8 +39,12 @@ public class CreateTable {
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);

View File

@ -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));

View File

@ -57,7 +57,7 @@ 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));
}
}

View File

@ -59,7 +59,7 @@ 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));
}
}

View File

@ -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);

View File

@ -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);
}