Example usage for org.apache.poi.hssf.usermodel HSSFRow createCell

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow createCell

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFRow createCell.

Prototype

@Override
public HSSFCell createCell(int column) 

Source Link

Document

Use this to create new cells within the row and return it.

Usage

From source file:com.jfinal.ext.render.excel.PoiKit.java

License:Apache License

public HSSFWorkbook export() {
    Preconditions.checkNotNull(headers, "headers can not be null");
    Preconditions.checkNotNull(columns, "columns can not be null");
    Preconditions.checkArgument(cellWidth >= 0, "cellWidth < 0");
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(sheetName);
    HSSFRow row = null;
    HSSFCell cell = null;// ww w .j av  a2  s .  co m
    if (headers.length > 0) {
        row = sheet.createRow(0);
        if (headerRow <= 0) {
            headerRow = HEADER_ROW;
        }
        headerRow = Math.min(headerRow, MAX_ROWS);
        for (int h = 0, lenH = headers.length; h < lenH; h++) {
            @SuppressWarnings("deprecation")
            Region region = new Region(0, (short) h, (short) headerRow - 1, (short) h);// ?rowFromcolumnFrom
            sheet.addMergedRegion(region);// rowTocolumnTo
            // 
            sheet.getNumMergedRegions();
            if (cellWidth > 0) {
                sheet.setColumnWidth(h, cellWidth);
            }
            cell = row.createCell(h);
            cell.setCellValue(headers[h]);
        }
    }
    if (data.size() == 0) {
        return wb;
    }
    for (int i = 0, len = data.size(); i < len; i++) {
        row = sheet.createRow(i + headerRow);
        Object obj = data.get(i);
        if (obj == null) {
            continue;
        }
        if (obj instanceof Map) {
            processAsMap(columns, row, obj);
        } else if (obj instanceof Model) {
            processAsModel(columns, row, obj);
        } else if (obj instanceof Record) {
            processAsRecord(columns, row, obj);
        }
    }
    return wb;
}

From source file:com.jfinal.ext.render.excel.PoiKit.java

License:Apache License

@SuppressWarnings("unchecked")
private static void processAsMap(String[] columns, HSSFRow row, Object obj) {
    HSSFCell cell;/*from  ww  w .  j  a v a  2  s .com*/
    Map<String, Object> map = (Map<String, Object>) obj;
    if (columns.length == 0) {// 
        Set<String> keys = map.keySet();
        int columnIndex = 0;
        for (String key : keys) {
            cell = row.createCell(columnIndex);
            cell.setCellValue(map.get(key) + "");
            columnIndex++;
        }
    } else {
        for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
            cell = row.createCell(j);
            cell.setCellValue(map.get(columns[j]) + "");
        }
    }
}

From source file:com.jfinal.ext.render.excel.PoiKit.java

License:Apache License

private static void processAsModel(String[] columns, HSSFRow row, Object obj) {
    HSSFCell cell;/*w  ww.ja v  a2s . com*/
    Model<?> model = (Model<?>) obj;
    Set<Entry<String, Object>> entries = model.getAttrsEntrySet();
    if (columns.length == 0) {// 
        int columnIndex = 0;
        for (Entry<String, Object> entry : entries) {
            cell = row.createCell(columnIndex);
            cell.setCellValue(entry.getValue() + "");
            columnIndex++;
        }
    } else {
        for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
            cell = row.createCell(j);
            cell.setCellValue(model.get(columns[j]) + "");
        }
    }
}

From source file:com.jfinal.ext.render.excel.PoiKit.java

License:Apache License

private static void processAsRecord(String[] columns, HSSFRow row, Object obj) {
    HSSFCell cell;//w ww .  j  a va  2s. co  m
    Record record = (Record) obj;
    Map<String, Object> map = record.getColumns();
    if (columns.length == 0) {// 
        record.getColumns();
        Set<String> keys = map.keySet();
        int columnIndex = 0;
        for (String key : keys) {
            cell = row.createCell(columnIndex);
            cell.setCellValue(record.get(key) + "");
            columnIndex++;
        }
    } else {
        for (int j = 0, lenJ = columns.length; j < lenJ; j++) {
            cell = row.createCell(j);
            cell.setCellValue(map.get(columns[j]) + "");
        }
    }
}

From source file:com.jitendrasinghnz.excelreadutility.ExcelReadStringArrayXSL.java

License:Open Source License

public void setOutputSingletResult(String[][] inputData, String[] outputResult, String filePath) {
    String[][] outputResultTwoDimensionArray;
    outputResultTwoDimensionArray = new String[inputData.length][inputData[0].length + 1];
    Path folderPath = null;//from   w  ww .j ava2  s.c  o m
    try {
        folderPath = Paths.get(filePath);
    } catch (InvalidPathException i) {
        System.out.println("Please Check whether " + filePath + " exist ");
    }
    for (int i = 0; i < outputResultTwoDimensionArray.length; i++) {
        for (int j = 0; j < outputResultTwoDimensionArray[i].length; j++) {
            if (j == (outputResultTwoDimensionArray[i].length - 1)) {
                for (int k = j; k < outputResultTwoDimensionArray[i].length; k++) {
                    outputResultTwoDimensionArray[i][k] = outputResult[i];
                }
            } else
                outputResultTwoDimensionArray[i][j] = inputData[i][j];
        }
    }

    CharSequence filepathWindows = "\\";
    CharSequence filepathGNULinux = "/";
    try {
        if (filePath.contains(filepathWindows) || filePath.contains(filepathGNULinux)) {
            String filename = "test_output_singlet_" + String.valueOf(System.currentTimeMillis()) + ".xls";
            String finalFileName = filePath + filename;
            FileOutputStream outputFile = new FileOutputStream(finalFileName);
            HSSFWorkbook hSSFWorkbook = new HSSFWorkbook();
            HSSFSheet hSSFSheet = hSSFWorkbook.createSheet("output");
            for (int i = 0; i < outputResultTwoDimensionArray.length; i++) {
                HSSFRow row = hSSFSheet.createRow(i);
                for (int j = 0; j < outputResultTwoDimensionArray[i].length; j++) {
                    HSSFCell cell = row.createCell(j);
                    cell.setCellValue(outputResultTwoDimensionArray[i][j]);
                }
            }
            hSSFWorkbook.write(outputFile);
            outputFile.flush();
            outputFile.close();
            System.out.println("An output file named \"" + filename + "\" was created at \"" + filePath + "\"");
            System.out.println("Good Bye !!!");
        }
    } catch (FileNotFoundException fnfe) {
        System.out.println("Sorry " + filePath + " does not exist");
    }

    catch (IOException ioe) {
        System.out.println("Error to open/close file from path " + filePath);
    }

}

From source file:com.jitendrasinghnz.excelreadutility.ExcelReadStringArrayXSL.java

License:Open Source License

public void setOutputResult(String[][] inputData, String[][] outputResult, String filePath) {
    String[][] outputResultTwoDimensionArray;
    outputResultTwoDimensionArray = new String[inputData.length][inputData[0].length + outputResult[0].length];
    Path folderPath = null;//from   w ww  .  j av  a2 s. c  o m
    try {
        folderPath = Paths.get(filePath);
    } catch (InvalidPathException i) {
        System.out.println("Please Check whether " + filePath + " exist ");
    }
    for (int i = 0; i < outputResultTwoDimensionArray.length; i++) {
        for (int j = 0; j < outputResultTwoDimensionArray[i].length; j++) {
            if (j >= (outputResultTwoDimensionArray[i].length - outputResult[i].length)) {

                outputResultTwoDimensionArray[i][j] = outputResult[i][j
                        - (outputResultTwoDimensionArray[i].length - outputResult[i].length)];

            } else
                outputResultTwoDimensionArray[i][j] = inputData[i][j];
        }
    }
    CharSequence filepathWindows = "\\";
    CharSequence filepathGNULinux = "/";
    try {
        if (filePath.contains(filepathWindows) || filePath.contains(filepathGNULinux)) {
            String filename = "test_output_" + String.valueOf(System.currentTimeMillis()) + ".xls";
            String finalFileName = filePath + filename;
            FileOutputStream outputFile = new FileOutputStream(finalFileName);
            HSSFWorkbook hSSFWorkbook = new HSSFWorkbook();
            HSSFSheet hSSFSheet = hSSFWorkbook.createSheet("output");
            for (int i = 0; i < outputResultTwoDimensionArray.length; i++) {
                HSSFRow row = hSSFSheet.createRow(i);
                for (int j = 0; j < outputResultTwoDimensionArray[i].length; j++) {
                    HSSFCell cell = row.createCell(j);
                    cell.setCellValue(outputResultTwoDimensionArray[i][j]);
                }
            }
            hSSFWorkbook.write(outputFile);
            outputFile.flush();
            outputFile.close();
            System.out.println("An output file named \"" + filename + "\" was created at \"" + filePath + "\"");
            System.out.println("Good Bye !!!");
        }
    } catch (FileNotFoundException fnfe) {
        System.out.println("Sorry " + filePath + " does not exist");
    }

    catch (IOException ioe) {
        System.out.println("Error to open/close file from path " + filePath);
    }
}

From source file:com.jk.framework.util.ExcelUtil.java

License:Apache License

/**
 *///from   w  w w.  ja v  a 2 s. co m
protected void createColumnHeaders() {
    final HSSFRow headersRow = this.sheet.createRow(0);
    final HSSFFont font = this.workbook.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    final HSSFCellStyle style = this.workbook.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    int counter = 1;
    for (int i = 0; i < this.model.getColumnCount(); i++) {
        final HSSFCell cell = headersRow.createCell(counter++);
        // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue(this.model.getColumnName(i));
        cell.setCellStyle(style);
    }
}

From source file:com.jk.framework.util.ExcelUtil.java

License:Apache License

/**
 *
 * @param rowIndex//from   ww w .j  a  va 2  s.co  m
 *            int
 */
protected void createRow(final int rowIndex) {
    final HSSFRow row = this.sheet.createRow(rowIndex + 1); // since the
    // rows in
    // excel starts from 1
    // not 0
    final HSSFCellStyle style = this.workbook.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    int counter = 1;
    for (int i = 0; i < this.model.getColumnCount(); i++) {
        final HSSFCell cell = row.createCell(counter++);
        // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        final Object value = this.model.getValueAt(rowIndex, i);
        setValue(cell, value);
        cell.setCellStyle(style);
    }
}

From source file:com.jshuabo.reportcenter.server.service.automoblie.impl.DefaultAutoRecordServiceImpl.java

License:Open Source License

@Override
public String importDataToExcel(HttpServletRequest request, HttpServletResponse response) {
    String realPath = request.getSession().getServletContext().getRealPath("/");
    Map<String, Object> params = new HashMap<String, Object>();
    Subject subject = SecurityUtils.getSubject();
    User user = (User) subject.getPrincipal();
    String userId = user.getId().toString();
    if (!"1".equals(userId) && !"50".equals(userId) && !"38".equals(userId) && !"53".equals(userId)
            && !"15".equals(userId) && !"16".equals(userId) && !"17".equals(userId)) {
        // FY FY_NJ 33
        params.put("subStation", user.getId());
    } else {/*from w  w  w.j  a  v a 2s . c  o  m*/
        // ggz bj WHY zht
        params.put("subStation", request.getParameter("subStation"));
    }
    params.put("name", request.getParameter("name"));
    params.put("sortOrder", null);
    params.put("offset", null);
    params.put("rows", null);
    List<AutoRecordData> autoRecordDataList = autoRecordDataMapper.page(params);
    String _fileName = null;
    FileOutputStream out = null;
    Map<String, Object> subStationMap = new HashMap<String, Object>();
    subStationMap.put("18", "?");
    subStationMap.put("19", "");
    subStationMap.put("20", "");
    subStationMap.put("21", "");
    subStationMap.put("22", "?");
    subStationMap.put("23", "");
    subStationMap.put("24", "?");
    subStationMap.put("25", "");
    subStationMap.put("26", "");
    subStationMap.put("27", "?");
    subStationMap.put("28", "");
    subStationMap.put("29", "");
    subStationMap.put("30", "");
    subStationMap.put("31", "?");
    subStationMap.put("32", "");
    subStationMap.put("33", "?");
    subStationMap.put("51", "");
    try {
        // 
        HSSFWorkbook wb = new HSSFWorkbook();
        // 
        HSSFSheet sheet = wb.createSheet("");
        HSSFRow firstRow = sheet.createRow(0);
        firstRow.createCell(0).setCellValue("");
        firstRow.createCell(1).setCellValue("???");
        firstRow.createCell(2).setCellValue("");
        firstRow.createCell(3).setCellValue("?");
        firstRow.createCell(4).setCellValue("?(???)");
        firstRow.createCell(5).setCellValue("?");
        firstRow.createCell(6).setCellValue("");
        firstRow.createCell(7).setCellValue("");
        firstRow.createCell(8).setCellValue("??");
        firstRow.createCell(9).setCellValue("???");
        firstRow.createCell(10).setCellValue("???");
        firstRow.createCell(11).setCellValue("??");
        firstRow.createCell(12).setCellValue("??");
        firstRow.createCell(13).setCellValue("");
        firstRow.createCell(14).setCellValue("??");
        firstRow.createCell(15).setCellValue("");
        firstRow.createCell(16).setCellValue("?");
        firstRow.createCell(17).setCellValue("???");
        firstRow.createCell(18).setCellValue("???");
        firstRow.createCell(19).setCellValue("???");
        firstRow.createCell(20).setCellValue("?");
        firstRow.createCell(21).setCellValue("??");
        firstRow.createCell(22).setCellValue("????");
        firstRow.createCell(23).setCellValue("????");
        firstRow.createCell(24).setCellValue("???");
        firstRow.createCell(25).setCellValue("??");
        firstRow.createCell(26).setCellValue("??");
        firstRow.createCell(27).setCellValue("???");
        firstRow.createCell(28).setCellValue("??");
        firstRow.createCell(29).setCellValue("??");
        firstRow.createCell(30).setCellValue("??");
        firstRow.createCell(31).setCellValue("??");
        firstRow.createCell(32).setCellValue("??");
        firstRow.createCell(33).setCellValue("????");
        firstRow.createCell(34).setCellValue("?");

        if (autoRecordDataList.size() > 0) {
            for (int j = 1; j < autoRecordDataList.size() + 1; ++j) {
                HSSFRow row = sheet.createRow(j);

                // subStation 
                HSSFCell subStation = row.createCell(0);
                subStation.setCellValue(
                        subStationMap.get(autoRecordDataList.get(j - 1).getSubStation()).toString());

                // deputyCard ???
                HSSFCell deputyCard = row.createCell(1);
                deputyCard.setCellValue(autoRecordDataList.get(j - 1).getDeputyCard());

                // carKind 
                HSSFCell carKind = row.createCell(2);
                carKind.setCellValue(autoRecordDataList.get(j - 1).getCarKind());

                // licenseNo ?
                HSSFCell licenseNo = row.createCell(3);
                licenseNo.setCellValue(autoRecordDataList.get(j - 1).getLicenseNo());

                // license ?(???)
                HSSFCell license = row.createCell(4);
                license.setCellValue(autoRecordDataList.get(j - 1).getLicense());

                // licenseDate ?
                HSSFCell licenseDate = row.createCell(5);
                licenseDate.setCellValue(null == autoRecordDataList.get(j - 1).getLicenseDate() ? ""
                        : DateFormatUtils.format(autoRecordDataList.get(j - 1).getLicenseDate(),
                                DateFormatUtils.ymd));

                // licenseName 
                HSSFCell licenseName = row.createCell(6);
                licenseName.setCellValue(autoRecordDataList.get(j - 1).getLicenseName());

                // inspectionDate 
                HSSFCell inspectionDate = row.createCell(7);
                inspectionDate.setCellValue(null == autoRecordDataList.get(j - 1).getInspectionDate() ? ""
                        : DateFormatUtils.format(autoRecordDataList.get(j - 1).getInspectionDate(),
                                DateFormatUtils.ymd));

                // name ??
                HSSFCell name = row.createCell(8);
                name.setCellValue(autoRecordDataList.get(j - 1).getName());

                // idCard ???
                HSSFCell idCard = row.createCell(9);
                idCard.setCellValue(autoRecordDataList.get(j - 1).getIdCard());

                // ftReceive ???
                HSSFCell ftReceive = row.createCell(10);
                ftReceive.setCellValue(null == autoRecordDataList.get(j - 1).getFtReceive() ? ""
                        : DateFormatUtils.format(autoRecordDataList.get(j - 1).getFtReceive(),
                                DateFormatUtils.ymd));

                // changeDate ??
                HSSFCell changeDate = row.createCell(11);
                changeDate.setCellValue(null == autoRecordDataList.get(j - 1).getChangeDate() ? ""
                        : DateFormatUtils.format(autoRecordDataList.get(j - 1).getChangeDate(),
                                DateFormatUtils.ymd));

                // telephone ??
                HSSFCell telephone = row.createCell(12);
                telephone.setCellValue(autoRecordDataList.get(j - 1).getTelephone());

                // strongInsDate 
                HSSFCell strongInsDate = row.createCell(13);
                strongInsDate.setCellValue(null == autoRecordDataList.get(j - 1).getStrongInsDate() ? ""
                        : DateFormatUtils.format(autoRecordDataList.get(j - 1).getStrongInsDate(),
                                DateFormatUtils.ymd));

                // tLInsurance ?? (?)
                HSSFCell tLInsurance = row.createCell(14);
                Double _tLInsurance = autoRecordDataList.get(j - 1).gettLInsurance();
                if (null == _tLInsurance) {
                    tLInsurance.setCellValue("");
                } else {
                    tLInsurance.setCellValue(_tLInsurance);
                }

                // tLInsuranceDate 
                HSSFCell tLInsuranceDate = row.createCell(15);
                tLInsuranceDate.setCellValue(null == autoRecordDataList.get(j - 1).gettLInsuranceDate() ? ""
                        : DateFormatUtils.format(autoRecordDataList.get(j - 1).gettLInsuranceDate(),
                                DateFormatUtils.ymd));

                // policeProve ?
                HSSFCell policeProve = row.createCell(16);
                policeProve.setCellValue(autoRecordDataList.get(j - 1).getPoliceProve());

                // householdCopy ???
                HSSFCell householdCopy = row.createCell(17);
                householdCopy.setCellValue(autoRecordDataList.get(j - 1).getHouseholdCopy());

                // idCardCopy ???
                HSSFCell idCardCopy = row.createCell(18);
                idCardCopy.setCellValue(autoRecordDataList.get(j - 1).getIdCardCopy());

                // licenseCopy ???
                HSSFCell licenseCopy = row.createCell(19);
                licenseCopy.setCellValue(autoRecordDataList.get(j - 1).getLicenseCopy());

                // guaranRespon ?
                HSSFCell guaranRespon = row.createCell(20);
                guaranRespon.setCellValue(autoRecordDataList.get(j - 1).getGuaranRespon());

                // guaranIncome ??
                HSSFCell guaranIncome = row.createCell(21);
                guaranIncome.setCellValue(autoRecordDataList.get(j - 1).getGuaranIncome());

                // guaranHouseCopy ????
                HSSFCell guaranHouseCopy = row.createCell(22);
                guaranHouseCopy.setCellValue(autoRecordDataList.get(j - 1).getGuaranHouseCopy());

                // guaranIDCopy ????
                HSSFCell guaranIDCopy = row.createCell(23);
                guaranIDCopy.setCellValue(autoRecordDataList.get(j - 1).getGuaranIDCopy());

                // driLicenseCopy ???
                HSSFCell driLicenseCopy = row.createCell(24);
                driLicenseCopy.setCellValue(autoRecordDataList.get(j - 1).getDriLicenseCopy());

                // strongInsCopy ??
                HSSFCell strongInsCopy = row.createCell(25);
                strongInsCopy.setCellValue(autoRecordDataList.get(j - 1).getStrongInsCopy());

                // commerInsuCopy ??
                HSSFCell commerInsuCopy = row.createCell(26);
                commerInsuCopy.setCellValue(autoRecordDataList.get(j - 1).getCommerInsuCopy());

                // certificate ???
                HSSFCell certificate = row.createCell(27);
                certificate.setCellValue(autoRecordDataList.get(j - 1).getCertificate());

                // agreeDate ??
                HSSFCell agreeDate = row.createCell(28);
                agreeDate.setCellValue(null == autoRecordDataList.get(j - 1).getAgreeDate() ? ""
                        : DateFormatUtils.format(autoRecordDataList.get(j - 1).getAgreeDate(),
                                DateFormatUtils.ymd));

                // rentalAgreement ??
                HSSFCell rentalAgreement = row.createCell(29);
                rentalAgreement.setCellValue(autoRecordDataList.get(j - 1).getRentalAgreement());

                // strongInsPrompt ??
                HSSFCell strongInsPrompt = row.createCell(30);
                strongInsPrompt.setCellValue(autoRecordDataList.get(j - 1).getStrongInsPrompt());

                // tLInsurancePrompt ??
                HSSFCell tLInsurancePrompt = row.createCell(31);
                tLInsurancePrompt.setCellValue(autoRecordDataList.get(j - 1).gettLInsurancePrompt());

                // inspectionPrompt ??
                HSSFCell inspectionPrompt = row.createCell(32);
                inspectionPrompt.setCellValue(autoRecordDataList.get(j - 1).getInspectionPrompt());

                // changePrompt ????
                HSSFCell changePrompt = row.createCell(33);
                changePrompt.setCellValue(autoRecordDataList.get(j - 1).getChangePrompt());

                // status ?
                HSSFCell status = row.createCell(34);
                status.setCellValue(autoRecordDataList.get(j - 1).getStatus());
            }
        }

        _fileName = "excel" + DateFormatUtils.format(new Date(), "yyyy-MM-dd-HH_mm_ss-SSS") + ".xls";

        String fileName = realPath + File.separator + _fileName;
        out = new FileOutputStream(fileName);

        wb.write(out);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // ?
        try {
            if (null != out) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return _fileName;
}

From source file:com.kazak.comeet.admin.gui.misc.XLSExporter.java

License:Open Source License

@SuppressWarnings("deprecation")
private void createXLSPage(Document doc, String title, TableColumnModel columnsInfo, String pageTitle) {
    HSSFSheet sheet = wb.createSheet(title);
    int columnsSize = columnsInfo.getColumnCount();

    setColumnsWidth(sheet, columnsSize);

    HSSFRow titleRow = sheet.createRow(0);
    titleRow.createCell((short) 1).setCellValue(reportTitle);
    HSSFRow pageTitleRow = sheet.createRow(1);
    pageTitleRow.createCell((short) 1).setCellValue(pageTitle);

    HSSFRow blankRow = sheet.createRow(2);
    blankRow.createCell((short) 0).setCellValue(" ");

    HSSFRow row0 = sheet.createRow(3);//w  ww  .j av a 2  s  .c  om

    for (int j = 0; j < columnsSize; j++) {
        TableColumn tmp = columnsInfo.getColumn(j);
        String columnName = (String) tmp.getHeaderValue();
        row0.createCell((short) j).setCellValue(columnName);
    }

    blankRow = sheet.createRow(4);
    blankRow.createCell((short) 0).setCellValue(" ");

    List messagesList = doc.getRootElement().getChildren("row");
    Iterator messageIterator = messagesList.iterator();
    // Loading new info 
    int i = 5;
    for (; messageIterator.hasNext();) {
        Element oneMessage = (Element) messageIterator.next();
        List messagesDetails = oneMessage.getChildren();
        HSSFRow row = sheet.createRow((short) i);
        for (int k = 0; k < columnsSize; k++) {
            String data = ((Element) messagesDetails.get(k)).getText();
            row.createCell((short) k).setCellValue(data);
        }
        i++;
    }
    if (i == 0) {
        HSSFRow row = sheet.createRow((short) i);
        row.createCell((short) 0).setCellValue("No hay registros en esta pgina.");
    }
}