Skip to content

Commit

Permalink
introduce CellValue to better control the values
Browse files Browse the repository at this point in the history
and to make sure we are not missing them like in this task

metasfresh/metasfresh-webui-api-legacy#608
  • Loading branch information
teosarca committed Sep 22, 2017
1 parent bb87377 commit 571eab3
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public abstract class AbstractExcelExporter
* @param col column index
* @return cell value
*/
protected abstract Object getValueAt(int row, int col);
protected abstract CellValue getValueAt(int row, int col);

/**
* Check if there is a page break on given cell
Expand All @@ -126,8 +126,8 @@ public abstract class AbstractExcelExporter
/** Logger */
protected final Logger log = LogManager.getLogger(getClass());
//
private HSSFWorkbook m_workbook;
private HSSFDataFormat m_dataFormat;
private final HSSFWorkbook m_workbook;
private final HSSFDataFormat m_dataFormat;
private HSSFFont m_fontHeader = null;
private HSSFFont m_fontDefault = null;
private Language m_lang = null;
Expand Down Expand Up @@ -155,7 +155,7 @@ protected void setFreezePane(final int colSplit, final int rowSplit)
m_rowSplit = rowSplit;
}

private String fixString(final String str)
private static String fixString(final String str)
{
// ms excel doesn't support UTF8 charset
return StringUtils.stripDiacritics(str);
Expand Down Expand Up @@ -266,8 +266,8 @@ private HSSFCellStyle getStyle(final int row, final int col)
}
else if (DisplayType.isNumeric(displayType))
{
DecimalFormat df = DisplayType.getNumberFormat(displayType, getLanguage());
String format = getFormatString(df, isHighlightNegativeNumbers);
final DecimalFormat df = DisplayType.getNumberFormat(displayType, getLanguage());
final String format = getFormatString(df, isHighlightNegativeNumbers);
cs.setDataFormat(m_dataFormat.getFormat(format));
}
m_styles.put(key, cs);
Expand Down Expand Up @@ -396,8 +396,7 @@ protected void formatPage(final HSSFSheet sheet)
* @param out
* @throws Exception
*/
public void export(final OutputStream out)
throws Exception
public void export(final OutputStream out) throws Exception
{
HSSFSheet sheet = createTableSheet();
String sheetName = null;
Expand All @@ -408,7 +407,7 @@ public void export(final OutputStream out)
setCurrentRow(rownum);

boolean isPageBreak = false;
HSSFRow row = sheet.createRow(xls_rownum);
final HSSFRow row = sheet.createRow(xls_rownum);
// for all columns
int colnum = 0;
for (int col = 0; col < getColumnCount(); col++)
Expand All @@ -418,45 +417,51 @@ public void export(final OutputStream out)
//
if (isColumnPrinted(col))
{
HSSFCell cell = row.createCell(colnum);
final HSSFCell cell = row.createCell(colnum);

// 03917: poi-3.7 doesn't have this method anymore
// cell.setEncoding(HSSFCell.ENCODING_UTF_16); // Bug-2017673 - Export Report as Excel - Bad Encoding

//
// Fetch cell value
CellValue cellValue;
try
{
cellValue = getValueAt(rownum, col);
}
catch (final Exception ex)
{
log.warn("Failed extracting cell value at row={}, col={}. Considering it null.", rownum, col, ex);
cellValue = null;
}

// line row
Object obj = getValueAt(rownum, col);
int displayType = getDisplayType(rownum, col);
if (obj == null)
//
// Update the excel cell
if (cellValue == null)
{
// nothing
}
else if (DisplayType.isDate(displayType))
else if (cellValue.isDate())
{
final java.util.Date value = (java.util.Date)obj;
cell.setCellValue(value);
cell.setCellValue(cellValue.dateValue());
}
else if (DisplayType.isNumeric(displayType))
else if (cellValue.isNumber())
{
double value = 0;
if (obj instanceof Number)
{
value = ((Number)obj).doubleValue();
}
cell.setCellValue(value);
cell.setCellValue(cellValue.doubleValue());
}
else if (DisplayType.YesNo == displayType)
else if (cellValue.isBoolean())
{
final boolean value = DisplayType.toBoolean(obj);
final boolean value = cellValue.booleanValue();
cell.setCellValue(new HSSFRichTextString(Msg.getMsg(getLanguage(), value == true ? "Y" : "N")));
}
else
{
final String value = fixString(obj.toString()); // formatted
final String value = fixString(cellValue.stringValue()); // formatted
cell.setCellValue(new HSSFRichTextString(value));
}
//
HSSFCellStyle style = getStyle(rownum, col);
cell.setCellStyle(style);
cell.setCellStyle(getStyle(rownum, col));

// Page break
if (isPageBreak(rownum, col))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,117 +1,109 @@
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 2008 SC ARHIPAC SERVICE SRL. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 2008 SC ARHIPAC SERVICE SRL. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.impexp;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.compiere.util.DisplayType;
import org.compiere.util.Util;
import org.adempiere.util.Check;

import de.metas.i18n.Msg;

/**
* Export excel from ArrayList of data
*
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
*
*/
public class ArrayExcelExporter extends AbstractExcelExporter {
public class ArrayExcelExporter extends AbstractExcelExporter
{
private Properties m_ctx = null;
private ArrayList<ArrayList<Object>> m_data = null;

public ArrayExcelExporter(Properties ctx, ArrayList<ArrayList<Object>> data) {

public ArrayExcelExporter(final Properties ctx, final ArrayList<ArrayList<Object>> data)
{
super();
m_ctx = ctx;
m_data = data;
}

@Override
public Properties getCtx() {
public Properties getCtx()
{
return m_ctx;
}

@Override
public int getColumnCount() {
public int getColumnCount()
{
return m_data.get(0).size();
}

@Override
public int getDisplayType(int row, int col) {
ArrayList<Object> dataRow = m_data.get(row+1);
Object value = dataRow.get(col);
if (value == null)
;
else if (value instanceof Timestamp) {
return DisplayType.Date;
// TODO: handle DateTime
}
else if (value instanceof Number) {
if (value instanceof Integer) {
return DisplayType.Integer;
}
else {
return DisplayType.Number;
}
}
else if (value instanceof Boolean) {
return DisplayType.YesNo;
}
else {
return DisplayType.String;
}
return -1;
public int getDisplayType(final int row, final int col)
{
final List<Object> dataRow = m_data.get(row + 1);
final Object value = dataRow.get(col);
return CellValues.extractDisplayTypeFromValue(value);
}

@Override
public String getHeaderName(int col) {
public String getHeaderName(final int col)
{
Object o = m_data.get(0).get(col);
String name = o != null ? o.toString() : null;
String nameTrl = Msg.translate(getLanguage(), name);
if (Util.isEmpty(nameTrl))
if (Check.isEmpty(nameTrl))
nameTrl = name;
return nameTrl;
}

@Override
public int getRowCount() {
public int getRowCount()
{
return m_data.size() - 1;
}

@Override
public Object getValueAt(int row, int col) {
ArrayList<Object> dataRow = m_data.get(row+1);
Object value = dataRow.get(col);
return value;
public CellValue getValueAt(final int row, final int col)
{
final List<Object> dataRow = m_data.get(row + 1);
final Object value = dataRow.get(col);
return CellValues.toCellValue(value);
}

@Override
public boolean isColumnPrinted(int col) {
public boolean isColumnPrinted(final int col)
{
return true;
}

@Override
public boolean isFunctionRow() {
public boolean isFunctionRow()
{
return false;
}

@Override
public boolean isPageBreak(int row, int col) {
public boolean isPageBreak(final int row, final int col)
{
return false;
}

@Override
protected void setCurrentRow(int row) {
protected void setCurrentRow(final int row)
{
}
}
Loading

0 comments on commit 571eab3

Please sign in to comment.