Search in sources :

Example 71 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project OpenClinica by OpenClinica.

the class SpreadSheetTableRepeating method toHTML.

public String toHTML(int sheetIndex) throws IOException {
    StringBuffer buf = new StringBuffer();
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    int numSheets = wb.getNumberOfSheets();
    for (int j = 0; j < numSheets; j++) {
        // sheetIndex);
        HSSFSheet sheet = wb.getSheetAt(j);
        String sheetName = wb.getSheetName(j);
        buf.append(sheetName + "<br>");
        buf.append("<table border=\"2\"");
        buf.append("caption=\"" + wb.getSheetName(sheetIndex) + "\"" + ">");
        int numCols = sheet.getPhysicalNumberOfRows();
        for (int i = 0; i < numCols; i++) {
            buf.append("<tr>");
            if (sheet.getRow(i) == null) {
                continue;
            }
            int numCells = sheet.getRow(i).getLastCellNum();
            for (int y = 0; y < numCells; y++) {
                HSSFCell cell = sheet.getRow(i).getCell((short) y);
                int cellType = 0;
                if (cell == null) {
                    cellType = HSSFCell.CELL_TYPE_BLANK;
                } else {
                    cellType = cell.getCellType();
                }
                switch(cellType) {
                    case HSSFCell.CELL_TYPE_BLANK:
                        buf.append("<td> </td>");
                        break;
                    case HSSFCell.CELL_TYPE_NUMERIC:
                        buf.append("<td>" + cell.getNumericCellValue() + "</td>");
                        break;
                    case HSSFCell.CELL_TYPE_STRING:
                        buf.append("<td>" + cell.getStringCellValue() + "</td>");
                        break;
                    default:
                        buf.append("<td></td>");
                }
            }
            buf.append("</tr>");
        }
        buf.append("</table>");
    }
    // end of sheet count, added by tbh 5-31
    return buf.toString();
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 72 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project head by mifos.

the class XlsLoansAccountImporter method getCellDecimalValue.

private BigDecimal getCellDecimalValue(HSSFRow row, XlsLoansImportTemplateConstants currentCell) throws XlsParsingException {
    final HSSFCell cell = row.getCell(currentCell.getValue(), HSSFRow.RETURN_BLANK_AS_NULL);
    BigDecimal result = null;
    if (cell != null) {
        switch(cell.getCellType()) {
            case HSSFCell.CELL_TYPE_STRING:
                HSSFRichTextString richText = cell.getRichStringCellValue();
                try {
                    result = new BigDecimal(richText.getString());
                } catch (NumberFormatException ex) {
                    throw new XlsParsingException(getCellError(XlsMessageConstants.NOT_A_NUMBER, row, currentCell.getValue(), null));
                }
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                double val = cell.getNumericCellValue();
                result = new BigDecimal(val);
                break;
            default:
                return null;
        }
    }
    return result;
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) BigDecimal(java.math.BigDecimal)

Example 73 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project head by mifos.

the class XlsLoansAccountImporter method getCellIntegerValue.

private Integer getCellIntegerValue(HSSFRow row, XlsLoansImportTemplateConstants currentCell) throws XlsParsingException {
    final HSSFCell cell = row.getCell(currentCell.getValue(), HSSFRow.RETURN_BLANK_AS_NULL);
    Integer result = null;
    if (cell != null) {
        switch(cell.getCellType()) {
            case HSSFCell.CELL_TYPE_STRING:
                HSSFRichTextString richText = cell.getRichStringCellValue();
                try {
                    result = new Integer(richText.getString());
                } catch (NumberFormatException ex) {
                    throw new XlsParsingException(getCellError(XlsMessageConstants.NOT_A_NUMBER, row, currentCell.getValue(), null));
                }
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                double val = cell.getNumericCellValue();
                result = new Integer((int) val);
                break;
            default:
                return null;
        }
    }
    return result;
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell)

Example 74 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project head by mifos.

the class XlsSavingsAccountImporter method getCellDecimalValue.

private BigDecimal getCellDecimalValue(HSSFRow row, XlsSavingsImportTemplateConstants currentCell) throws XlsParsingException {
    final HSSFCell cell = row.getCell(currentCell.getValue(), HSSFRow.RETURN_BLANK_AS_NULL);
    BigDecimal result = null;
    if (cell != null) {
        switch(cell.getCellType()) {
            case HSSFCell.CELL_TYPE_STRING:
                HSSFRichTextString richText = cell.getRichStringCellValue();
                try {
                    result = new BigDecimal(richText.getString());
                } catch (NumberFormatException ex) {
                    throw new XlsParsingException(getCellError(XlsMessageConstants.NOT_A_NUMBER, row, currentCell.getValue(), null));
                }
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                double val = cell.getNumericCellValue();
                result = new BigDecimal(val);
                break;
            default:
                return null;
        }
    }
    return result;
}
Also used : HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) XlsParsingException(org.mifos.application.importexport.xls.XlsLoansAccountImporter.XlsParsingException) BigDecimal(java.math.BigDecimal)

Example 75 with HSSFCell

use of org.apache.poi.hssf.usermodel.HSSFCell in project adempiere by adempiere.

the class SmjXlsReport method reportTable.

// titleTable
/**
	 * llena los datos del reporte - fill report data
	 * @param book
	 * @param data
	 * @param sheet
	 * @param fila
	 */
public void reportTable(HSSFWorkbook book, LinkedList<ReportTO> data, HSSFSheet sheet, int fila) {
    HSSFRow row;
    // crea fuente - create font
    HSSFFont font = book.createFont();
    font.setFontHeightInPoints((short) 10);
    font.setFontName(HSSFFont.FONT_ARIAL);
    HSSFRichTextString text;
    Iterator<ReportTO> itRep = data.iterator();
    Boolean newRow = false;
    sheet.setColumnWidth((short) 0, (short) (13 * 256));
    sheet.setColumnWidth((short) 1, (short) (60 * 256));
    for (int i = 2; i < (cols); i++) {
        sheet.setColumnWidth((short) i, (short) (15 * 256));
    }
    //for
    // estio celda - cell style
    HSSFCellStyle cellStyle = book.createCellStyle();
    HSSFCellStyle cellStyleD = book.createCellStyle();
    HSSFCellStyle cellStyleN = book.createCellStyle();
    while (itRep.hasNext()) {
        short col = 0;
        ReportTO rpt = itRep.next();
        if (!newRow) {
            cellStyle = book.createCellStyle();
            cellStyleD = book.createCellStyle();
            cellStyleN = book.createCellStyle();
        }
        //if
        newRow = false;
        if (rpt.getReportlinestyle() != null && rpt.getReportlinestyle().equals("T")) {
            // Coloca titulo - put title
            row = sheet.createRow(fila++);
            HSSFFont fontT = book.createFont();
            fontT.setFontHeightInPoints((short) 12);
            fontT.setFontName(HSSFFont.FONT_ARIAL);
            fontT.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            HSSFCellStyle cellStyleT = book.createCellStyle();
            cellStyleT.setWrapText(true);
            cellStyleT.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyleT.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
            cellStyleT.setFont(fontT);
            Region region = new Region(fila - 1, (short) 0, fila - 1, endRegion);
            sheet.addMergedRegion(region);
            text = new HSSFRichTextString(rpt.getDescription());
            HSSFCell cellT = row.createCell(col);
            cellT.setCellStyle(cellStyleT);
            cellT.setCellValue(text);
            newRow = true;
        } else if (rpt.getReportlinestyle() != null && rpt.getReportlinestyle().equals("L")) {
            // coloca linea en el reporte - Put under line in the report
            cellStyle.setWrapText(true);
            cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
            cellStyle.setBottomBorderColor((short) 8);
            cellStyleD.setWrapText(true);
            cellStyleD.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
            cellStyleD.setBottomBorderColor((short) 8);
            cellStyleN.setWrapText(true);
            cellStyleN.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
            cellStyleN.setBottomBorderColor((short) 8);
            newRow = true;
        } else if (rpt.getReportlinestyle() != null && rpt.getReportlinestyle().equals("X")) {
            // coloca linea de total - Put total line
            cellStyle.setWrapText(true);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
            cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
            cellStyle.setBottomBorderColor((short) 8);
            newRow = true;
        } else if (rpt.getReportlinestyle() != null && rpt.getReportlinestyle().equals("Z")) {
            // coloca linea doble de total - Put total line doble
            cellStyle.setWrapText(true);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
            cellStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
            cellStyle.setBottomBorderColor((short) 8);
            //--------------
            row = sheet.createRow(fila++);
            ReportTO rptD = new ReportTO();
            putRow(cellStyle, cellStyleD, cellStyleN, sheet, row, fila, rptD);
            cellStyle = book.createCellStyle();
            newRow = true;
        } else if (rpt.getReportlinestyle() != null && rpt.getReportlinestyle().equals("D")) {
            // coloca liena de descripcion - put description line
            cellStyleD.setWrapText(true);
            cellStyleD.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
            cellStyleD.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
            cellStyleD.setBottomBorderColor((short) 8);
            newRow = true;
        } else if (rpt.getReportlinestyle() != null && rpt.getReportlinestyle().equals("S")) {
            // coloca linea en blanco - put empty line
            row = sheet.createRow(fila++);
            newRow = true;
        } else if (rpt.getTablevel() != null && rpt.getTablevel() > 0) {
            // coloca espacios a la izquierda para simular jeraquia - put
            // left spaces to simulate hierarchy
            row = sheet.createRow(fila++);
            String jerarchy = "";
            for (int i = 1; i <= rpt.getTablevel(); i++) {
                jerarchy = jerarchy + "   ";
            }
            //for
            Region region = new Region(fila - 1, (short) 0, fila - 1, endRegion);
            sheet.addMergedRegion(region);
            text = new HSSFRichTextString(jerarchy + rpt.getDescription());
            HSSFCell cellJ = row.createCell(col);
            cellJ.setCellValue(text);
            newRow = true;
        } else {
            row = sheet.createRow(fila++);
            putRow(cellStyle, cellStyleD, cellStyleN, sheet, row, fila, rpt);
        }
    //else
    }
// while itData
}
Also used : HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) ReportTO(org.compiere.model.ReportTO) Region(org.apache.poi.hssf.util.Region) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString)

Aggregations

HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)147 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)96 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)93 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)91 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)31 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)30 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)28 Test (org.junit.Test)25 FileOutputStream (java.io.FileOutputStream)24 IOException (java.io.IOException)18 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)16 File (java.io.File)13 CellValue (org.apache.poi.ss.usermodel.CellValue)13 ArrayList (java.util.ArrayList)11 AssertionFailedError (junit.framework.AssertionFailedError)10 HashMap (java.util.HashMap)9 OutputStream (java.io.OutputStream)8 Map (java.util.Map)8 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)8 FileInputStream (java.io.FileInputStream)6