import java.io.File ;
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.udf.DefaultUDFFinder ;
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 ;
/**
* An example class of how to invoke a User Defined Function for a given
* 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 static void main( String[] args ) {
+ public static void main( String[] args ) throws Exception {
if( args.length != 2 ) {
System.out.println( "usage: UserDefinedFunctionExample fileName cellId" ) ;
File workbookFile = new File( args[0] ) ;
- try {
- FileInputStream fis = new FileInputStream(workbookFile);
- Workbook workbook = WorkbookFactory.create(fis);
- fis.close();
+ FileInputStream fis = new FileInputStream(workbookFile);
+ Workbook workbook = WorkbookFactory.create(fis);
+ fis.close();
- String[] functionNames = { "calculatePayment" } ;
- FreeRefFunction[] functionImpls = { new CalculateMortgage() } ;
-
- UDFFinder udfToolpack = new DefaultUDFFinder( functionNames, functionImpls ) ;
-
- // register the user-defined function in the workbook
- workbook.addToolPack(udfToolpack);
+ String[] functionNames = { "calculatePayment" } ;
+ FreeRefFunction[] functionImpls = { new CalculateMortgage() } ;
+
+ UDFFinder udfToolpack = new DefaultUDFFinder( functionNames, functionImpls ) ;
- FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
+ // register the user-defined function in the workbook
+ workbook.addToolPack(udfToolpack);
- CellReference cr = new CellReference( args[1] ) ;
- String sheetName = cr.getSheetName() ;
- Sheet sheet = workbook.getSheet( sheetName ) ;
- int rowIdx = cr.getRow() ;
- int colIdx = cr.getCol() ;
- Row row = sheet.getRow( rowIdx ) ;
- Cell cell = row.getCell( colIdx ) ;
-
- CellValue value = evaluator.evaluate( cell ) ;
-
- System.out.println("returns value: " + value ) ;
+ FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
- } catch( FileNotFoundException e ) {
- e.printStackTrace();
- } catch( InvalidFormatException e ) {
- e.printStackTrace();
- } catch( IOException e ) {
- e.printStackTrace();
- }
+ CellReference cr = new CellReference( args[1] ) ;
+ String sheetName = cr.getSheetName() ;
+ Sheet sheet = workbook.getSheet( sheetName ) ;
+ int rowIdx = cr.getRow() ;
+ int colIdx = cr.getCol() ;
+ Row row = sheet.getRow( rowIdx ) ;
+ Cell cell = row.getCell( colIdx ) ;
+
+ CellValue value = evaluator.evaluate( cell ) ;
+
+ System.out.println("returns value: " + value ) ;
+
+ workbook.close();
}
}
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
+import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* This example shows how to display a spreadsheet in HTML using the classes for
* spreadsheet display.
- *
- * @author Ken Arnold, Industrious Media LLC
*/
public class ToHtml {
private final Workbook wb;
}
private ToHtml(Workbook wb, Appendable output) {
- if (wb == null)
+ if (wb == null) {
throw new NullPointerException("wb");
- if (output == null)
+ }
+ if (output == null) {
throw new NullPointerException("output");
+ }
this.wb = wb;
this.output = output;
setupColorMap();
}
private void setupColorMap() {
- if (wb instanceof HSSFWorkbook)
+ if (wb instanceof HSSFWorkbook) {
helper = new HSSFHtmlHelper((HSSFWorkbook) wb);
- else if (wb instanceof XSSFWorkbook)
+ } else if (wb instanceof XSSFWorkbook) {
helper = new XSSFHtmlHelper();
- else
+ } else {
throw new IllegalArgumentException(
"unknown workbook type: " + wb.getClass().getSimpleName());
+ }
}
/**
out.format("</html>%n");
}
} finally {
- if (out != null)
- out.close();
+ IOUtils.closeQuietly(out);
if (output instanceof Closeable) {
- Closeable closeable = (Closeable) output;
- closeable.close();
+ IOUtils.closeQuietly((Closeable) output);
}
}
}
}
private void ensureOut() {
- if (out == null)
+ if (out == null) {
out = new Formatter(output);
+ }
}
public void printStyles() {
} catch (IOException e) {
throw new IllegalStateException("Reading standard css", e);
} finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- //noinspection ThrowFromFinallyBlock
- throw new IllegalStateException("Reading standard css", e);
- }
- }
+ IOUtils.closeQuietly(in);
}
// now add css for each used style
private void fontStyle(CellStyle style) {
Font font = wb.getFontAt(style.getFontIndex());
- if (font.getBold())
+ if (font.getBold()) {
out.format(" font-weight: bold;%n");
- if (font.getItalic())
+ }
+ if (font.getItalic()) {
out.format(" font-style: italic;%n");
+ }
int fontheight = font.getFontHeightInPoints();
if (fontheight == 9) {
}
private String styleName(CellStyle style) {
- if (style == null)
+ if (style == null) {
style = wb.getCellStyleAt((short) 0);
+ }
StringBuilder sb = new StringBuilder();
Formatter fmt = new Formatter(sb);
try {
private static CellType ultimateCellType(Cell c) {
CellType type = c.getCellTypeEnum();
- if (type == CellType.FORMULA)
+ if (type == CellType.FORMULA) {
type = c.getCachedFormulaResultTypeEnum();
+ }
return type;
}
}
private void ensureColumnBounds(Sheet sheet) {
- if (gotBounds)
+ if (gotBounds) {
return;
+ }
Iterator<Row> iter = sheet.rowIterator();
firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0);
style.getDataFormatString());
CellFormatResult result = cf.apply(cell);
content = result.text;
- if (content.equals(""))
+ if (content.equals("")) {
content = " ";
+ }
}
}
out.format(" <td class=%s %s>%s</td>%n", styleName(style),
toAdd = "";\r
}\r
\r
+ @Override\r
public int compareTo(CellNumberStringMod that) {\r
int diff = special.pos - that.special.pos;\r
return (diff != 0) ? diff : (op - that.op);\r
\r
@Override\r
public boolean equals(Object that) {\r
- try {\r
- return compareTo((CellNumberStringMod) that) == 0;\r
- } catch (RuntimeException ignored) {\r
- // NullPointerException or CastException\r
- return false;\r
- }\r
+ return (that instanceof CellNumberStringMod) && compareTo((CellNumberStringMod) that) == 0;\r
}\r
\r
@Override\r