Example usage for org.apache.poi.ss.usermodel Sheet autoSizeColumn

List of usage examples for org.apache.poi.ss.usermodel Sheet autoSizeColumn

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Sheet autoSizeColumn.

Prototype

void autoSizeColumn(int column);

Source Link

Document

Adjusts the column width to fit the contents.

Usage

From source file:de.quamoco.qm.editor.export.ResultCalibrationExporter.java

License:Apache License

/** Helper method to automatically set the width of all columns. */
private void autoSizeColumns(Sheet sheet) {
    for (Iterator<Cell> i = sheet.getRow(0).cellIterator(); i.hasNext();) {
        Cell cell = i.next();// w ww  .  j a  v a 2s  .  c om
        sheet.autoSizeColumn(cell.getColumnIndex());
    }
}

From source file:de.ryanthara.ja.rycon.converter.excel.Cadwork2Excel.java

License:GNU General Public License

/**
 * Converts a coordinate file from Cadwork (node.dat) into a Microsoft Excel file.
 *
 * @param isXLS     selector to distinguish between XLS and XLSX file extension
 * @param sheetName name of the sheet (file name from input file)
 *
 * @return success of the conversion/* www .j av a2s. c  o  m*/
 */
public boolean convertCadwork2Excel(boolean isXLS, String sheetName, boolean writeCommentRow) {
    // general preparation of the workbook
    if (isXLS) {
        workbook = new HSSFWorkbook();
    } else {
        workbook = new XSSFWorkbook();
    }

    String safeName = WorkbookUtil.createSafeSheetName(sheetName);
    String[] lineSplit;
    Sheet sheet = workbook.createSheet(safeName);
    Row row;
    Cell cell;

    short rowNumber = 0;
    short cellNumber = 0;

    // remove not needed headlines
    for (int i = 0; i < 3; i++) {
        readStringLines.remove(0);
    }

    if (writeCommentRow) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        lineSplit = readStringLines.get(0).trim().split("\\s+", -1);

        for (String description : lineSplit) {
            cell = row.createCell(cellNumber);
            cellNumber++;
            cell.setCellValue(description);
        }
    }

    // remove furthermore the still not needed comment line
    readStringLines.remove(0);

    for (String line : readStringLines) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        cellNumber = 0;

        lineSplit = line.trim().split("\\t", -1);

        cell = row.createCell(cellNumber); // No
        cell.setCellValue(lineSplit[0]);
        cellNumber++;

        cell = row.createCell(cellNumber); // X
        cell.setCellValue(lineSplit[1]);
        cellNumber++;

        cell = row.createCell(cellNumber); // Y
        cell.setCellValue(lineSplit[2]);
        cellNumber++;

        cell = row.createCell(cellNumber); // Z
        cell.setCellValue(lineSplit[3]);
        cellNumber++;

        cell = row.createCell(cellNumber); // Code
        cell.setCellValue(lineSplit[4]);
        cellNumber++;

        cell = row.createCell(cellNumber); // Name
        cell.setCellValue(lineSplit[5]);
    }

    // adjust column width to fit the content
    for (int i = 0; i < 5; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return rowNumber > 1;
}

From source file:de.ryanthara.ja.rycon.converter.excel.Caplan2Excel.java

License:GNU General Public License

/**
 * Converts a Caplan K file element by element into a Microsoft Excel file.
 *
 * @param isXLS           selector to distinguish between XLS and XLSX file extension
 * @param sheetName       name of the sheet (file name from input file)
 * @param writeCommentRow write comment row
 *
 * @return success conversion success/*from  ww  w .  jav  a  2  s .c  o m*/
 */
public boolean convertCaplan2Excel(boolean isXLS, String sheetName, boolean writeCommentRow) {
    // general preparation of the workbook
    if (isXLS) {
        workbook = new HSSFWorkbook();
    } else {
        workbook = new XSSFWorkbook();
    }

    String safeName = WorkbookUtil.createSafeSheetName(sheetName);
    Sheet sheet = workbook.createSheet(safeName);
    Row row;
    Cell cell;
    CellStyle cellStyle;

    DataFormat format = workbook.createDataFormat();

    short rowNumber = 0;
    short cellNumber = 0;
    short countColumns = 0;

    if (writeCommentRow) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        cell = row.createCell(cellNumber);
        cell.setCellValue(ResourceBundleUtils.getLangString(COLUMNS, Columns.pointNumber));
        cellNumber++;

        cell = row.createCell(cellNumber);
        cell.setCellValue(ResourceBundleUtils.getLangString(COLUMNS, Columns.easting));
        cellNumber++;

        cell = row.createCell(cellNumber);
        cell.setCellValue(ResourceBundleUtils.getLangString(COLUMNS, Columns.northing));
        cellNumber++;

        cell = row.createCell(cellNumber);
        cell.setCellValue(ResourceBundleUtils.getLangString(COLUMNS, Columns.height));
        cellNumber++;

        cell = row.createCell(cellNumber);
        cell.setCellValue(ResourceBundleUtils.getLangString(COLUMNS, Columns.object));
        cellNumber++;

        cell = row.createCell(cellNumber);
        cell.setCellValue(ResourceBundleUtils.getLangString(COLUMNS, Columns.attribute));
    }

    for (String line : readStringLines) {
        // skip empty lines directly after reading
        if (!line.trim().isEmpty()) {
            row = sheet.createRow(rowNumber);
            rowNumber++;

            cellNumber = 0;

            CaplanBlock caplanBlock = new CaplanBlock(line);

            if (caplanBlock.getNumber() != null) {
                cell = row.createCell(cellNumber);
                cell.setCellValue(caplanBlock.getNumber());
                cellNumber++;
            }

            if (caplanBlock.getEasting() != null) {
                cell = row.createCell(cellNumber);

                if (!caplanBlock.getEasting().equals("")) {
                    cell.setCellValue(Double.parseDouble(caplanBlock.getEasting()));
                    cellStyle = workbook.createCellStyle();
                    cellStyle.setDataFormat(format.getFormat("#,##0.0000"));
                    cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
                    cell.setCellStyle(cellStyle);
                } else {
                    cell.setCellValue("");
                }

                cellNumber++;
            }

            if (caplanBlock.getNorthing() != null) {
                cell = row.createCell(cellNumber);

                if (!caplanBlock.getNorthing().equals("")) {
                    cell.setCellValue(Double.parseDouble(caplanBlock.getNorthing()));
                    cellStyle = workbook.createCellStyle();
                    cellStyle.setDataFormat(format.getFormat("#,##0.0000"));
                    cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
                    cell.setCellStyle(cellStyle);
                } else {
                    cell.setCellValue("");
                }

                cellNumber++;
            }

            if (caplanBlock.getHeight() != null) {
                cell = row.createCell(cellNumber);

                if (!caplanBlock.getHeight().equals("")) {
                    cell.setCellValue(Double.parseDouble(caplanBlock.getHeight()));
                    cellStyle = workbook.createCellStyle();
                    cellStyle.setDataFormat(format.getFormat("#,##0.0000"));
                    cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
                    cell.setCellStyle(cellStyle);
                } else {
                    cell.setCellValue("");
                }

                cellNumber++;
            }

            if (caplanBlock.getCode() != null) {
                cell = row.createCell(cellNumber);
                cell.setCellValue(caplanBlock.getCode());
                cellNumber++;

                if (caplanBlock.getAttributes().size() > 0) {
                    for (String attribute : caplanBlock.getAttributes()) {
                        cell = row.createCell(cellNumber);
                        cell.setCellValue(attribute);
                        cellNumber++;
                    }
                }
            }

            if (cellNumber > countColumns) {
                countColumns = cellNumber;
            }
        }
    }

    // adjust column width to fit the content
    for (int i = 0; i < countColumns; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return rowNumber > 1;
}

From source file:de.ryanthara.ja.rycon.converter.excel.CSV2Excel.java

License:GNU General Public License

/**
 * Convert a CSV file element by element into an Excel file.
 *
 * @param isXLS     selector to distinguish between XLS and XLSX file extension
 * @param sheetName name of the sheet (file name from input file)
 *
 * @return success conversion success// w w  w  .  j  av a 2s.  c  om
 */
public boolean convertCSV2Excel(boolean isXLS, String sheetName) {
    // general preparation of the workbook
    if (isXLS) {
        workbook = new HSSFWorkbook();
    } else {
        workbook = new XSSFWorkbook();
    }

    String safeName = WorkbookUtil.createSafeSheetName(sheetName);
    Sheet sheet = workbook.createSheet(safeName);
    Row row;
    Cell cell;

    short rowNumber = 0;
    short cellNumber;
    short countColumns = 0;

    for (String[] csvLine : readCSVLines) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        cellNumber = 0;

        for (String element : csvLine) {
            cell = row.createCell(cellNumber);
            cellNumber++;
            cell.setCellValue(element);
        }

        if (cellNumber > countColumns) {
            countColumns = cellNumber;
        }
    }

    // adjust column width to fit the content
    for (int i = 0; i < countColumns; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return rowNumber > 1;
}

From source file:de.ryanthara.ja.rycon.converter.excel.CSVBaselStadt2Excel.java

License:GNU General Public License

/**
 * Converts a comma separated coordinate file from the geodata server Basel Stadt (Switzerland)
 * into a Zeiss REC formatted file./*from  w w w.  j a va  2s . c om*/
 *
 * @param isXLS           selector to distinguish between XLS and XLSX file extension
 * @param sheetName       name of the sheet (file name from input file)
 * @param writeCommentRow write comment row
 *
 * @return success conversion success
 */
public boolean convertCSVBaselStadt2Excel(boolean isXLS, String sheetName, boolean writeCommentRow) {
    // general preparation of the workbook
    if (isXLS) {
        workbook = new HSSFWorkbook();
    } else {
        workbook = new XSSFWorkbook();
    }

    String safeName = WorkbookUtil.createSafeSheetName(sheetName);
    Sheet sheet = workbook.createSheet(safeName);
    Row row;
    Cell cell;
    CellStyle cellStyle;

    DataFormat format = workbook.createDataFormat();

    short rowNumber = 0;
    short cellNumber = 0;

    if (writeCommentRow) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        String[] commentLine = readCSVLines.get(0);

        for (String description : commentLine) {
            cell = row.createCell(cellNumber);
            cellNumber++;
            cell.setCellValue(description);
        }
    }

    // remove furthermore the still not needed comment line
    readCSVLines.remove(0);

    for (String[] csvLine : readCSVLines) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        cellNumber = 0;

        for (int i = 0; i < csvLine.length; i++) {
            cell = row.createCell(cellNumber);
            cellNumber++;

            switch (i) {
            case 0:
            case 1:
                cell.setCellValue(csvLine[i]);
                break;
            case 2:
            case 3:
            case 4:
            case 5:
                if (csvLine[i].equalsIgnoreCase("")) {
                    cell.setCellValue(csvLine[i]);
                } else {
                    cell.setCellValue(Double.parseDouble(csvLine[i]));
                    cellStyle = workbook.createCellStyle();
                    cellStyle.setDataFormat(format.getFormat("#,##0.000"));
                    cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
                    cell.setCellStyle(cellStyle);
                }
                break;
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                cell.setCellValue(csvLine[i]);
                break;

            default:
                System.err.println(
                        "Error in convertCSVBaselStadt2Excel: unknown element found or to much columns");
            }
        }
    }

    // adjust column width to fit the content
    for (int i = 0; i < readCSVLines.get(0).length; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return rowNumber > 1;
}

From source file:de.ryanthara.ja.rycon.converter.excel.GSI2Excel.java

License:GNU General Public License

/**
 * Converts a GSI file element by element into an Excel file.
 *
 * @param isXLS     selector to distinguish between XLS and XLSX file extension
 * @param sheetName name of the sheet (file name from input file)
 *
 * @return success conversion success/* w  w  w . ja  v a2  s. c  o m*/
 */
public boolean convertGSI2Excel(boolean isXLS, String sheetName, boolean writeCommentRow) {
    // general preparation of the workbook
    if (isXLS) {
        workbook = new HSSFWorkbook();
    } else {
        workbook = new XSSFWorkbook();
    }

    String safeName = WorkbookUtil.createSafeSheetName(sheetName);
    Sheet sheet = workbook.createSheet(safeName);
    Row row;
    Cell cell;
    CellStyle cellStyle;

    DataFormat format = workbook.createDataFormat();

    short rowNumber = 0;
    short cellNumber = 0;

    if (writeCommentRow) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        for (int wordIndex : baseToolsGSI.getFoundAllWordIndices()) {
            cell = row.createCell(cellNumber);
            cellNumber++;

            cell.setCellValue(
                    ResourceBundleUtils.getLangString(WORDINDICES, WordIndices.valueOf("WI" + wordIndex)));
        }
    }

    // fill gsi content into rows and cells
    for (ArrayList<GSIBlock> blocksInLine : baseToolsGSI.getEncodedLinesOfGSIBlocks()) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        cellNumber = 0;

        for (GSIBlock block : blocksInLine) {
            cell = row.createCell(cellNumber);
            cellNumber++;

            switch (block.getWordIndex()) {
            // GENERAL
            case 11: // Point number (includes block number)
            case 12: // Instrument serial no
            case 13: // Instrument type
            case 18: // Time format 1: pos. 8-9 year, 10-11 sec, 12-14 msec
            case 19: // Time format 2 : pos, 8-9 month 10-11 day, 12-13 hour, 14-15 min
                cell.setCellValue(block.toPrintFormatCSV());
                break;

            // ANGLES
            case 21: // Horizontal Circle (Hz)
            case 22: // Vertical Angle (V)
            case 25: // Horizontal circle difference (Hz0-Hz)
                cell.setCellValue(Double.parseDouble(block.toPrintFormatCSV()));
                break;

            // DISTANCE
            case 31: // Slope Distance
            case 32: // Horizontal Distance
            case 33: // Height Difference
                cell.setCellValue(Double.parseDouble(block.toPrintFormatCSV()));
                break;

            // CODE BLOCK
            case 41: // Code number ( include block number)
            case 42: // Information 1
            case 43: // Information 2
            case 44: // Information 3
            case 45: // Information 4
            case 46: // Information 5
            case 47: // Information 6
            case 48: // Information 7
            case 49: // Information 8
                cell.setCellValue(block.toPrintFormatCSV());
                break;

            // DISTANCE (additional information)
            case 51: // Constants(ppm, mm)
            case 52: // Number of measurements, standard deviation
            case 53: // Deviation
            case 58: // Signal strength
            case 59: // Reflector constant (1/10 mm)ppm
                cell.setCellValue(block.toPrintFormatCSV());
                break;

            // POINT CODING
            case 71: // Point Code
            case 72: // Attribute 1
            case 73: // Attribute 2
            case 74: // Attribute 3
            case 75: // Attribute 4
            case 76: // Attribute 5
            case 77: // Attribute 6
            case 78: // Attribute 7
            case 79: // Attribute 8
                cell.setCellValue(block.toPrintFormatCSV());
                break;

            // COORDINATES
            case 81: // Easting (Target)
            case 82: // Northing (Target)
            case 83: // Elevation (Target)
            case 84: // Station Easting (E0)
            case 85: // Station Northing (N0)
            case 86: // Station Elevation (H0)
                cell.setCellValue(Double.parseDouble(block.toPrintFormatCSV()));
                cellStyle = workbook.createCellStyle();
                cellStyle.setDataFormat(format.getFormat("#,##0.0000"));
                cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
                cell.setCellStyle(cellStyle);
                break;

            case 87: // Reflector height (above ground)
            case 88: // Instrument height (above ground)
                cell.setCellValue(Double.parseDouble(block.toPrintFormatCSV()));
                cellStyle = workbook.createCellStyle();
                cellStyle.setDataFormat(format.getFormat("#,##0.000"));
                cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
                cell.setCellStyle(cellStyle);
                break;

            default:
                System.err.println("GSI2Excel.convertGSI2Excel() : line contains unknown word index "
                        + block.toPrintFormatCSV());
            }
        }
    }

    // adjust column width to fit the content
    for (int i = 0; i < baseToolsGSI.getEncodedLinesOfGSIBlocks().size(); i++) {
        sheet.autoSizeColumn((short) i);
    }

    // check number of written lines
    return rowNumber > 1;
}

From source file:de.ryanthara.ja.rycon.converter.excel.TXT2Excel.java

License:GNU General Public License

/**
 * Converts a TXT file element by element into an Excel file.
 *
 * @param isXLS     selector to distinguish between XLS and XLSX file extension
 * @param sheetName name of the sheet (file name from input file)
 *
 * @return success conversion success/*ww  w . j a  va2  s  . c o m*/
 */
public boolean convertTXT2Excel(boolean isXLS, String sheetName) {
    // general preparation of the workbook
    if (isXLS) {
        workbook = new HSSFWorkbook();
    } else {
        workbook = new XSSFWorkbook();
    }

    String safeName = WorkbookUtil.createSafeSheetName(sheetName);
    Sheet sheet = workbook.createSheet(safeName);
    Row row;
    Cell cell;

    short rowNumber = 0;
    short cellNumber;
    short countColumns = 0;

    for (String line : readStringLines) {
        String[] lineSplit = line.trim().split("\\s+");

        row = sheet.createRow(rowNumber);
        rowNumber++;

        cellNumber = 0;

        for (String element : lineSplit) {
            cell = row.createCell(cellNumber);
            cellNumber++;
            cell.setCellValue(element);
            if (cellNumber > countColumns) {
                countColumns = cellNumber;
            }

        }
    }

    // adjust column width to fit the content
    for (int i = 0; i < countColumns; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return rowNumber > 1;
}

From source file:de.ryanthara.ja.rycon.converter.excel.TXTBaselLandschaft2Excel.java

License:GNU General Public License

/**
 * Converts a txt file from the geodata server Basel Landschaft (Switzerland) element by element into an Excel file.
 *
 * @param isXLS           selector to distinguish between XLS and XLSX file extension
 * @param sheetName       name of the sheet (file name from input file)
 * @param writeCommentRow write comment row
 *
 * @return success conversion success/*from  ww  w.  j  ava  2  s.  com*/
 */
public boolean convertTXTBaselLand2Excel(boolean isXLS, String sheetName, boolean writeCommentRow) {
    // general preparation of the workbook
    if (isXLS) {
        workbook = new HSSFWorkbook();
    } else {
        workbook = new XSSFWorkbook();
    }

    String safeName = WorkbookUtil.createSafeSheetName(sheetName);
    Sheet sheet = workbook.createSheet(safeName);
    Row row;
    Cell cell;
    CellStyle cellStyle;

    DataFormat format = workbook.createDataFormat();

    short rowNumber = 0;
    short cellNumber = 0;
    short countColumns = 0;

    if (writeCommentRow) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        String[] lineSplit = readStringLines.get(0).trim().split("\\t", -1);

        for (String description : lineSplit) {
            cell = row.createCell(cellNumber);
            cellNumber++;
            cell.setCellValue(description);
        }
    }

    // remove furthermore the still not needed comment line
    readStringLines.remove(0);

    for (String line : readStringLines) {
        row = sheet.createRow(rowNumber);
        rowNumber++;

        String[] lineSplit = line.trim().split("\\t", -1);

        cellNumber = 0;

        switch (lineSplit.length) {
        case 5: // HFP file
            cell = row.createCell(cellNumber); // Art
            cell.setCellValue(lineSplit[0]);
            cellNumber++;

            cell = row.createCell(cellNumber); // Number
            cell.setCellValue(lineSplit[1]);
            cellNumber++;

            cell = row.createCell(cellNumber); // X
            cell.setCellValue(Double.parseDouble(lineSplit[2]));
            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(format.getFormat("#,##0.000"));
            cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
            cell.setCellStyle(cellStyle);
            cellNumber++;

            cell = row.createCell(cellNumber); // Y
            cell.setCellValue(Double.parseDouble(lineSplit[3]));
            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(format.getFormat("#,##0.000"));
            cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
            cell.setCellStyle(cellStyle);
            cellNumber++;

            cell = row.createCell(cellNumber); // Z
            if (lineSplit[4].equalsIgnoreCase("NULL")) {
                cell.setCellValue("NULL");
            } else {
                cell.setCellValue(Double.parseDouble(lineSplit[4]));
                cellStyle = workbook.createCellStyle();
                cellStyle.setDataFormat(format.getFormat("#,##0.000"));
                cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
                cell.setCellStyle(cellStyle);
            }

            countColumns = 5;
            break;

        case 6: // LFP file
            cell = row.createCell(cellNumber); // Art
            cell.setCellValue(lineSplit[0]);
            cellNumber++;

            cell = row.createCell(cellNumber); // Number
            cell.setCellValue(lineSplit[1]);
            cellNumber++;

            cell = row.createCell(cellNumber); // VArt
            cell.setCellValue(lineSplit[2]);
            cellNumber++;

            cell = row.createCell(cellNumber); // X
            cell.setCellValue(Double.parseDouble(lineSplit[3]));
            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(format.getFormat("#,##0.000"));
            cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
            cell.setCellStyle(cellStyle);
            cellNumber++;

            cell = row.createCell(cellNumber); // Y
            cell.setCellValue(Double.parseDouble(lineSplit[4]));
            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(format.getFormat("#,##0.000"));
            cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
            cell.setCellStyle(cellStyle);
            cellNumber++;

            cell = row.createCell(cellNumber); // Z
            if (lineSplit[5].equalsIgnoreCase("NULL")) {
                cell.setCellValue("NULL");
            } else {
                cell.setCellValue(Double.parseDouble(lineSplit[5]));
                cellStyle = workbook.createCellStyle();
                cellStyle.setDataFormat(format.getFormat("#,##0.000"));
                cellStyle.setVerticalAlignment(CellStyle.ALIGN_RIGHT);
                cell.setCellStyle(cellStyle);
            }

            countColumns = 6;
            break;

        default:
            System.err.println(
                    "TXTBaselLandschaft2Excel.convertTXTBaselLand2Excel() : line contains less or more tokens "
                            + line);
        }
    }

    // adjust column width to fit the content
    for (int i = 0; i < countColumns; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return rowNumber > 1;
}

From source file:de.ryanthara.ja.rycon.converter.excel.Zeiss2Excel.java

License:GNU General Public License

/**
 * Convert a Zeiss REC file element by element into an Excel file.
 *
 * @param isXLS           selector to distinguish between XLS and XLSX file extension
 * @param sheetName       name of the sheet (file name from input file)
 * @param writeCommentRow write comment row
 *
 * @return success conversion success//from  www.ja v  a 2  s . c o  m
 */
public boolean convertZeiss2Excel(boolean isXLS, String sheetName, boolean writeCommentRow) {
    // general preparation of the workbook
    if (isXLS) {
        workbook = new HSSFWorkbook();
    } else {
        workbook = new XSSFWorkbook();
    }

    String safeName = WorkbookUtil.createSafeSheetName(sheetName);
    Sheet sheet = workbook.createSheet(safeName);
    Row row;
    Cell cell;
    //        CellStyle cellStyle;

    //        DataFormat format = workbook.createDataFormat();

    short rowNumber = 0;
    short cellNumber = 0;
    short countColumns = 0;

    // TODO implement comment row and multi line stored values

    /*
    if (writeCommentRow) {
    row = sheet.createRow(rowNumber);
    rowNumber++;
            
    cell = row.createCell(cellNumber);
    cell.setCellValue(I18N.getCaplanColumnTyp("pointNumber"));
    cellNumber++;
            
    cell = row.createCell(cellNumber);
    cell.setCellValue(I18N.getCaplanColumnTyp("easting"));
    cellNumber++;
            
    cell = row.createCell(cellNumber);
    cell.setCellValue(I18N.getCaplanColumnTyp("northing"));
    cellNumber++;
            
    cell = row.createCell(cellNumber);
    cell.setCellValue(I18N.getCaplanColumnTyp("height"));
    cellNumber++;
            
    cell = row.createCell(cellNumber);
    cell.setCellValue(I18N.getCaplanColumnTyp("object"));
    cellNumber++;
            
    cell = row.createCell(cellNumber);
    cell.setCellValue(I18N.getCaplanColumnTyp("attribute"));
    }
    */

    for (String line : readStringLines) {

        // skip empty lines directly after reading
        if (!line.trim().isEmpty()) {
            row = sheet.createRow(rowNumber);
            rowNumber++;

            cellNumber = 0;

            ZeissDecoder decoder = new ZeissDecoder();

            for (ZeissBlock zeissBlock : decoder.getZeissBlocks()) {
                cell = row.createCell(cellNumber);
                cell.setCellValue(zeissBlock.getValue());
                cellNumber++;
                countColumns++;
            }

            if (cellNumber > countColumns) {
                countColumns = cellNumber;
            }
        }
    }

    // adjust column width to fit the content
    for (int i = 0; i < countColumns; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return rowNumber > 1;
}

From source file:document.ExcelDocument.java

protected void autoSizeColumns(Sheet sheet, int length) {
    for (int i = 0; i < length; ++i) {
        sheet.autoSizeColumn(i);
    }//from  ww w . ja v a2  s.  co  m
}