mirror of https://github.com/apache/poi.git
SonarQube fixes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1777541 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fd1ff9f2db
commit
2802a3c153
|
@ -18,28 +18,27 @@ package org.apache.poi.ss.examples.formula;
|
||||||
|
|
||||||
import java.io.File ;
|
import java.io.File ;
|
||||||
import java.io.FileInputStream ;
|
import java.io.FileInputStream ;
|
||||||
import java.io.FileNotFoundException ;
|
|
||||||
import java.io.IOException ;
|
|
||||||
|
|
||||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException ;
|
|
||||||
import org.apache.poi.ss.formula.functions.FreeRefFunction ;
|
import org.apache.poi.ss.formula.functions.FreeRefFunction ;
|
||||||
import org.apache.poi.ss.formula.udf.DefaultUDFFinder ;
|
import org.apache.poi.ss.formula.udf.DefaultUDFFinder ;
|
||||||
import org.apache.poi.ss.formula.udf.UDFFinder ;
|
import org.apache.poi.ss.formula.udf.UDFFinder ;
|
||||||
import org.apache.poi.ss.usermodel.*;
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
import org.apache.poi.ss.usermodel.CellValue;
|
||||||
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||||
import org.apache.poi.ss.util.CellReference ;
|
import org.apache.poi.ss.util.CellReference ;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An example class of how to invoke a User Defined Function for a given
|
* An example class of how to invoke a User Defined Function for a given
|
||||||
* XLS instance using POI's UDFFinder implementation.
|
* XLS instance using POI's UDFFinder implementation.
|
||||||
*
|
|
||||||
* @author Jon Svede ( jon [at] loquatic [dot] com )
|
|
||||||
* @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class UserDefinedFunctionExample {
|
public class UserDefinedFunctionExample {
|
||||||
|
|
||||||
public static void main( String[] args ) {
|
public static void main( String[] args ) throws Exception {
|
||||||
|
|
||||||
if( args.length != 2 ) {
|
if( args.length != 2 ) {
|
||||||
System.out.println( "usage: UserDefinedFunctionExample fileName cellId" ) ;
|
System.out.println( "usage: UserDefinedFunctionExample fileName cellId" ) ;
|
||||||
|
@ -51,39 +50,32 @@ public class UserDefinedFunctionExample {
|
||||||
|
|
||||||
File workbookFile = new File( args[0] ) ;
|
File workbookFile = new File( args[0] ) ;
|
||||||
|
|
||||||
try {
|
FileInputStream fis = new FileInputStream(workbookFile);
|
||||||
FileInputStream fis = new FileInputStream(workbookFile);
|
Workbook workbook = WorkbookFactory.create(fis);
|
||||||
Workbook workbook = WorkbookFactory.create(fis);
|
fis.close();
|
||||||
fis.close();
|
|
||||||
|
|
||||||
String[] functionNames = { "calculatePayment" } ;
|
String[] functionNames = { "calculatePayment" } ;
|
||||||
FreeRefFunction[] functionImpls = { new CalculateMortgage() } ;
|
FreeRefFunction[] functionImpls = { new CalculateMortgage() } ;
|
||||||
|
|
||||||
UDFFinder udfToolpack = new DefaultUDFFinder( functionNames, functionImpls ) ;
|
UDFFinder udfToolpack = new DefaultUDFFinder( functionNames, functionImpls ) ;
|
||||||
|
|
||||||
// register the user-defined function in the workbook
|
// register the user-defined function in the workbook
|
||||||
workbook.addToolPack(udfToolpack);
|
workbook.addToolPack(udfToolpack);
|
||||||
|
|
||||||
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
|
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
|
||||||
|
|
||||||
CellReference cr = new CellReference( args[1] ) ;
|
CellReference cr = new CellReference( args[1] ) ;
|
||||||
String sheetName = cr.getSheetName() ;
|
String sheetName = cr.getSheetName() ;
|
||||||
Sheet sheet = workbook.getSheet( sheetName ) ;
|
Sheet sheet = workbook.getSheet( sheetName ) ;
|
||||||
int rowIdx = cr.getRow() ;
|
int rowIdx = cr.getRow() ;
|
||||||
int colIdx = cr.getCol() ;
|
int colIdx = cr.getCol() ;
|
||||||
Row row = sheet.getRow( rowIdx ) ;
|
Row row = sheet.getRow( rowIdx ) ;
|
||||||
Cell cell = row.getCell( colIdx ) ;
|
Cell cell = row.getCell( colIdx ) ;
|
||||||
|
|
||||||
CellValue value = evaluator.evaluate( cell ) ;
|
CellValue value = evaluator.evaluate( cell ) ;
|
||||||
|
|
||||||
System.out.println("returns value: " + value ) ;
|
System.out.println("returns value: " + value ) ;
|
||||||
|
|
||||||
} catch( FileNotFoundException e ) {
|
workbook.close();
|
||||||
e.printStackTrace();
|
|
||||||
} catch( InvalidFormatException e ) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch( IOException e ) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,13 +46,12 @@ import org.apache.poi.ss.usermodel.Sheet;
|
||||||
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||||
|
import org.apache.poi.util.IOUtils;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This example shows how to display a spreadsheet in HTML using the classes for
|
* This example shows how to display a spreadsheet in HTML using the classes for
|
||||||
* spreadsheet display.
|
* spreadsheet display.
|
||||||
*
|
|
||||||
* @author Ken Arnold, Industrious Media LLC
|
|
||||||
*/
|
*/
|
||||||
public class ToHtml {
|
public class ToHtml {
|
||||||
private final Workbook wb;
|
private final Workbook wb;
|
||||||
|
@ -154,23 +153,26 @@ public class ToHtml {
|
||||||
}
|
}
|
||||||
|
|
||||||
private ToHtml(Workbook wb, Appendable output) {
|
private ToHtml(Workbook wb, Appendable output) {
|
||||||
if (wb == null)
|
if (wb == null) {
|
||||||
throw new NullPointerException("wb");
|
throw new NullPointerException("wb");
|
||||||
if (output == null)
|
}
|
||||||
|
if (output == null) {
|
||||||
throw new NullPointerException("output");
|
throw new NullPointerException("output");
|
||||||
|
}
|
||||||
this.wb = wb;
|
this.wb = wb;
|
||||||
this.output = output;
|
this.output = output;
|
||||||
setupColorMap();
|
setupColorMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupColorMap() {
|
private void setupColorMap() {
|
||||||
if (wb instanceof HSSFWorkbook)
|
if (wb instanceof HSSFWorkbook) {
|
||||||
helper = new HSSFHtmlHelper((HSSFWorkbook) wb);
|
helper = new HSSFHtmlHelper((HSSFWorkbook) wb);
|
||||||
else if (wb instanceof XSSFWorkbook)
|
} else if (wb instanceof XSSFWorkbook) {
|
||||||
helper = new XSSFHtmlHelper();
|
helper = new XSSFHtmlHelper();
|
||||||
else
|
} else {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"unknown workbook type: " + wb.getClass().getSimpleName());
|
"unknown workbook type: " + wb.getClass().getSimpleName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -214,11 +216,9 @@ public class ToHtml {
|
||||||
out.format("</html>%n");
|
out.format("</html>%n");
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (out != null)
|
IOUtils.closeQuietly(out);
|
||||||
out.close();
|
|
||||||
if (output instanceof Closeable) {
|
if (output instanceof Closeable) {
|
||||||
Closeable closeable = (Closeable) output;
|
IOUtils.closeQuietly((Closeable) output);
|
||||||
closeable.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -236,8 +236,9 @@ public class ToHtml {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureOut() {
|
private void ensureOut() {
|
||||||
if (out == null)
|
if (out == null) {
|
||||||
out = new Formatter(output);
|
out = new Formatter(output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printStyles() {
|
public void printStyles() {
|
||||||
|
@ -255,14 +256,7 @@ public class ToHtml {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new IllegalStateException("Reading standard css", e);
|
throw new IllegalStateException("Reading standard css", e);
|
||||||
} finally {
|
} finally {
|
||||||
if (in != null) {
|
IOUtils.closeQuietly(in);
|
||||||
try {
|
|
||||||
in.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
//noinspection ThrowFromFinallyBlock
|
|
||||||
throw new IllegalStateException("Reading standard css", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// now add css for each used style
|
// now add css for each used style
|
||||||
|
@ -307,10 +301,12 @@ public class ToHtml {
|
||||||
private void fontStyle(CellStyle style) {
|
private void fontStyle(CellStyle style) {
|
||||||
Font font = wb.getFontAt(style.getFontIndex());
|
Font font = wb.getFontAt(style.getFontIndex());
|
||||||
|
|
||||||
if (font.getBold())
|
if (font.getBold()) {
|
||||||
out.format(" font-weight: bold;%n");
|
out.format(" font-weight: bold;%n");
|
||||||
if (font.getItalic())
|
}
|
||||||
|
if (font.getItalic()) {
|
||||||
out.format(" font-style: italic;%n");
|
out.format(" font-style: italic;%n");
|
||||||
|
}
|
||||||
|
|
||||||
int fontheight = font.getFontHeightInPoints();
|
int fontheight = font.getFontHeightInPoints();
|
||||||
if (fontheight == 9) {
|
if (fontheight == 9) {
|
||||||
|
@ -323,8 +319,9 @@ public class ToHtml {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String styleName(CellStyle style) {
|
private String styleName(CellStyle style) {
|
||||||
if (style == null)
|
if (style == null) {
|
||||||
style = wb.getCellStyleAt((short) 0);
|
style = wb.getCellStyleAt((short) 0);
|
||||||
|
}
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
Formatter fmt = new Formatter(sb);
|
Formatter fmt = new Formatter(sb);
|
||||||
try {
|
try {
|
||||||
|
@ -344,8 +341,9 @@ public class ToHtml {
|
||||||
|
|
||||||
private static CellType ultimateCellType(Cell c) {
|
private static CellType ultimateCellType(Cell c) {
|
||||||
CellType type = c.getCellTypeEnum();
|
CellType type = c.getCellTypeEnum();
|
||||||
if (type == CellType.FORMULA)
|
if (type == CellType.FORMULA) {
|
||||||
type = c.getCachedFormulaResultTypeEnum();
|
type = c.getCachedFormulaResultTypeEnum();
|
||||||
|
}
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,8 +370,9 @@ public class ToHtml {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureColumnBounds(Sheet sheet) {
|
private void ensureColumnBounds(Sheet sheet) {
|
||||||
if (gotBounds)
|
if (gotBounds) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Iterator<Row> iter = sheet.rowIterator();
|
Iterator<Row> iter = sheet.rowIterator();
|
||||||
firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0);
|
firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0);
|
||||||
|
@ -434,8 +433,9 @@ public class ToHtml {
|
||||||
style.getDataFormatString());
|
style.getDataFormatString());
|
||||||
CellFormatResult result = cf.apply(cell);
|
CellFormatResult result = cf.apply(cell);
|
||||||
content = result.text;
|
content = result.text;
|
||||||
if (content.equals(""))
|
if (content.equals("")) {
|
||||||
content = " ";
|
content = " ";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.format(" <td class=%s %s>%s</td>%n", styleName(style),
|
out.format(" <td class=%s %s>%s</td>%n", styleName(style),
|
||||||
|
|
|
@ -63,6 +63,7 @@ public class CellNumberStringMod implements Comparable<CellNumberStringMod> {
|
||||||
toAdd = "";
|
toAdd = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public int compareTo(CellNumberStringMod that) {
|
public int compareTo(CellNumberStringMod that) {
|
||||||
int diff = special.pos - that.special.pos;
|
int diff = special.pos - that.special.pos;
|
||||||
return (diff != 0) ? diff : (op - that.op);
|
return (diff != 0) ? diff : (op - that.op);
|
||||||
|
@ -70,12 +71,7 @@ public class CellNumberStringMod implements Comparable<CellNumberStringMod> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object that) {
|
public boolean equals(Object that) {
|
||||||
try {
|
return (that instanceof CellNumberStringMod) && compareTo((CellNumberStringMod) that) == 0;
|
||||||
return compareTo((CellNumberStringMod) that) == 0;
|
|
||||||
} catch (RuntimeException ignored) {
|
|
||||||
// NullPointerException or CastException
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue