use of org.apache.poi.ss.format.CellFormat in project poi by apache.
the class ToHtml method printSheetContent.
private void printSheetContent(Sheet sheet) {
printColumnHeads();
out.format("<tbody>%n");
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
Row row = rows.next();
out.format(" <tr>%n");
out.format(" <td class=%s>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1);
for (int i = firstColumn; i < endColumn; i++) {
String content = " ";
String attrs = "";
CellStyle style = null;
if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
Cell cell = row.getCell(i);
if (cell != null) {
style = cell.getCellStyle();
attrs = tagStyle(cell, style);
//Set the value that is rendered for the cell
//also applies the format
CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
CellFormatResult result = cf.apply(cell);
content = result.text;
if (content.equals("")) {
content = " ";
}
}
}
out.format(" <td class=%s %s>%s</td>%n", styleName(style), attrs, content);
}
out.format(" </tr>%n");
}
out.format("</tbody>%n");
}
use of org.apache.poi.ss.format.CellFormat in project poi by apache.
the class DataFormatter method getFormat.
private Format getFormat(double cellValue, int formatIndex, String formatStrIn) {
localeChangedObservable.checkForLocaleChange();
// // Might be better to separate out the n p and z formats, falling back to p when n and z are not set.
// // That however would require other code to be re factored.
// String[] formatBits = formatStrIn.split(";");
// int i = cellValue > 0.0 ? 0 : cellValue < 0.0 ? 1 : 2;
// String formatStr = (i < formatBits.length) ? formatBits[i] : formatBits[0];
String formatStr = formatStrIn;
// TODO Going forward, we should really merge the logic between the two classes
if (formatStr.contains(";") && (formatStr.indexOf(';') != formatStr.lastIndexOf(';') || rangeConditionalPattern.matcher(formatStr).matches())) {
try {
// Ask CellFormat to get a formatter for it
CellFormat cfmt = CellFormat.getInstance(formatStr);
// CellFormat requires callers to identify date vs not, so do so
Object cellValueO = Double.valueOf(cellValue);
if (DateUtil.isADateFormat(formatIndex, formatStr) && // don't try to handle Date value 0, let a 3 or 4-part format take care of it
((Double) cellValueO).doubleValue() != 0.0) {
cellValueO = DateUtil.getJavaDate(cellValue);
}
// Wrap and return (non-cachable - CellFormat does that)
return new CellFormatResultWrapper(cfmt.apply(cellValueO));
} catch (Exception e) {
logger.log(POILogger.WARN, "Formatting failed for format " + formatStr + ", falling back", e);
}
}
// Excel's # with value 0 will output empty where Java will output 0. This hack removes the # from the format.
if (emulateCSV && cellValue == 0.0 && formatStr.contains("#") && !formatStr.contains("0")) {
formatStr = formatStr.replaceAll("#", "");
}
// See if we already have it cached
Format format = formats.get(formatStr);
if (format != null) {
return format;
}
// Is it one of the special built in types, General or @?
if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
return generalNumberFormat;
}
// Build a formatter, and cache it
format = createFormat(cellValue, formatIndex, formatStr);
formats.put(formatStr, format);
return format;
}
use of org.apache.poi.ss.format.CellFormat in project poi by apache.
the class TestDataFormatter method setUpClass.
@BeforeClass
@SuppressForbidden
public static void setUpClass() {
// some pre-checks to hunt for a problem in the Maven build
// these checks ensure that the correct locale is set, so a failure here
// usually indicates an invalid locale during test-execution
assertFalse(DateUtil.isADateFormat(-1, "_-* #,##0.00_-;-* #,##0.00_-;_-* \"-\"??_-;_-@_-"));
Locale ul = LocaleUtil.getUserLocale();
assertTrue(Locale.ROOT.equals(ul) || Locale.getDefault().equals(ul));
final String textValue = NumberToTextConverter.toText(1234.56);
assertEquals(-1, textValue.indexOf('E'));
Object cellValueO = Double.valueOf(1234.56);
/*CellFormat cellFormat = new CellFormat("_-* #,##0.00_-;-* #,##0.00_-;_-* \"-\"??_-;_-@_-");
CellFormatResult result = cellFormat.apply(cellValueO);
assertEquals(" 1,234.56 ", result.text);*/
CellFormat cfmt = CellFormat.getInstance("_-* #,##0.00_-;-* #,##0.00_-;_-* \"-\"??_-;_-@_-");
CellFormatResult result = cfmt.apply(cellValueO);
assertEquals("This failure can indicate that the wrong locale is used during test-execution, ensure you run with english/US via -Duser.language=en -Duser.country=US", " 1,234.56 ", result.text);
}
Aggregations