Example usage for org.apache.poi.ss.usermodel Row createCell

List of usage examples for org.apache.poi.ss.usermodel Row createCell

Introduction

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

Prototype

Cell createCell(int column);

Source Link

Document

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

Usage

From source file:clummy.classes.WriteToExcel.java

/**
 * this method is to write to excel based on the passed lists
 * @param appendID /*from  w  w w .ja  va2  s .  c o m*/
 * @param jfile put null if you want to disable this
 * @return 
 * @throws java.lang.Exception
 */
public boolean writeExcel(String appendID, JFileChooser jfile) throws Exception {
    // if jfile exist then execute the first line, and exist method
    if (jfile != null) {
        FileOutputStream out = new FileOutputStream(jfile.getSelectedFile() + ".xlsx");
        workbook.write(out);
        out.close();
        System.out.println("File save to" + jfile.getCurrentDirectory());
        return true;
    }

    idList = new ArrayList<>();
    rfidList = new ArrayList<>();
    firstNameList = new ArrayList<>();
    lastNameList = new ArrayList<>();
    fullNameList = new ArrayList<>();
    otherNameList = new ArrayList<>();
    modFirstNameList = new ArrayList<>();
    modLastNameList = new ArrayList<>();
    modFullNameList = new ArrayList<>();
    genderList = new ArrayList<>();
    dobList = new ArrayList<>();
    addressList = new ArrayList<>();
    countryList = new ArrayList<>();
    isoAlpha2List = new ArrayList<>();
    isoAlpha3List = new ArrayList<>();
    currencyCodeList = new ArrayList<>();
    currencyNameList = new ArrayList<>();
    isIndependentList = new ArrayList<>();
    bankAmount = new ArrayList<>();

    raceList = new ArrayList<>();
    maritalList = new ArrayList<>();
    emailList = new ArrayList<>();
    telephoneList = new ArrayList<>();
    scoreList = new ArrayList<>();
    bloodTypeList = new ArrayList<>();
    yesNoList = new ArrayList<>();
    levelList = new ArrayList<>();
    occupationList = new ArrayList<>();
    scoreLevelList = new ArrayList<>();
    ageList = new ArrayList<>();

    shuffle1 = new ArrayList<>();
    shuffle1sub = new ArrayList<>();
    shuffle2 = new ArrayList<>();
    shuffle2sub = new ArrayList<>();
    shuffle3 = new ArrayList<>();
    shuffle3sub = new ArrayList<>();
    shuffle4 = new ArrayList<>();
    shuffle4sub = new ArrayList<>();
    shuffle5 = new ArrayList<>();
    shuffle5sub = new ArrayList<>();

    shuffle6 = new ArrayList<>();
    shuffle6sub = new ArrayList<>();
    shuffle7 = new ArrayList<>();
    shuffle7sub = new ArrayList<>();
    shuffle8 = new ArrayList<>();
    shuffle8sub = new ArrayList<>();
    shuffle9 = new ArrayList<>();
    shuffle9sub = new ArrayList<>();
    shuffle10 = new ArrayList<>();
    shuffle10sub = new ArrayList<>();
    shuffle11 = new ArrayList<>();
    shuffle11sub = new ArrayList<>();
    shuffle12 = new ArrayList<>();
    shuffle12sub = new ArrayList<>();

    if (appendID == null || appendID.isEmpty())
        appendID = "";
    //Create a blank sheet
    XSSFSheet sheet = workbook.createSheet("Dummy Data");

    //This data needs to be written (Object[])
    Map<String, Object[]> data = new TreeMap<>();
    data.put("1", new Object[] { "ID", "REF-ID", "FirstName", "LastName", "FullName", "OtherName",
            "Mod First Name", "Mod Last Name", "Mod Full Name", "Gender", "DOB", "Age", "Address", "Country",
            "Country ISO3166-1-Alpha-2", "Country ISO3166-1-Alpha-3", "Country Currency Code", "Currency Name",
            "Is Country Independent", "Race", "Status", "Occupation", "Email", "Telephone", "Blood Type",
            "Yes/No", "Random Number", "Number Level", "Random Level", "Bank Amount", "Shuffle List 1",
            "Sub Shuffle 1", "Shuffle List 2", "Sub Shuffle 2", "Shuffle List 3", "Sub Shuffle 3",
            "Shuffle List 4", "Sub Shuffle 4", "Shuffle List 5", "Sub Shuffle 5", "Shuffle List 6",
            "Sub Shuffle 6", "Shuffle List 7", "Sub Shuffle 7", "Shuffle List 8", "Sub Shuffle 8",
            "Shuffle List 9", "Sub Shuffle 9", "Shuffle List 10", "Sub Shuffle 10", "Shuffle List 11",
            "Sub Shuffle 11", "Shuffle List 12", "Sub Shuffle 12" });
    int z = 2;
    String tempSourceName[], tempModName[];
    String idTemp, refTemp, countryTemp, email, maritalString, bloodString, yesNoString, randomLevelString,
            occupationString, scoreLevelString, tel, isoA2, isoA3, currencyCode, currencyName, isIndependent,
            bankAmountString, otherName, modFullName, modFirstName, modLastName,

            firstShuffle1String, shuffle1String, subsf1, firstShuffle2String, shuffle2String, subsf2,
            firstShuffle3String, shuffle3String, subsf3, firstShuffle4String, shuffle4String, subsf4,
            firstShuffle5String, shuffle5String, subsf5, firstShuffle6String, shuffle6String, subsf6,
            firstShuffle7String, shuffle7String, subsf7, firstShuffle8String, shuffle8String, subsf8,
            firstShuffle9String, shuffle9String, subsf9, firstShuffle10String, shuffle10String, subsf10,
            firstShuffle11String, shuffle11String, subsf11, firstShuffle12String, shuffle12String, subsf12;
    int scoreTemp, age;
    for (int i = 0; i < nameSourceList.size(); i++) {
        shuffle1String = returnNullAsEmpty(dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_1));
        shuffle2String = returnNullAsEmpty(dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_2));
        shuffle3String = returnNullAsEmpty(dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_3));
        shuffle4String = returnNullAsEmpty(dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_4));
        shuffle5String = returnNullAsEmpty(dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_5));
        shuffle6String = returnNullAsEmpty(dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_6));
        shuffle7String = returnNullAsEmpty(dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_7));
        shuffle8String = returnNullAsEmpty(dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_8));
        shuffle9String = returnNullAsEmpty(dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_9));
        shuffle10String = returnNullAsEmpty(
                dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_10));
        shuffle11String = returnNullAsEmpty(
                dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_11));
        shuffle12String = returnNullAsEmpty(
                dataHandlingClass.getShuffleListFile(AllFileList.SHUFFLELISTFILE_12));
        tempSourceName = nameSourceList.get(i).split(" ", 2);
        modFullName = dataHandlingClass.getDifferentNameFromFullName(nameSourceList.get(i));
        tempModName = modFullName.split(" ", 2);
        data.put("" + (i + z), new Object[] { idTemp = appendID + String.format("%05d", i + 1), //id
                refTemp = "REF-" + appendID + String.format("%05d", i + 1), tempSourceName[FIRSTNAME],
                tempSourceName[LASTNAME], nameSourceList.get(i),
                otherName = dataHandlingClass.getFirstNameAndLastNameInitial(nameSourceList.get(i)),
                modFirstName = tempModName[FIRSTNAME], modLastName = tempModName[LASTNAME], modFullName,
                genderSourceList.get(i), dobSourceList.get(i),
                age = dataHandlingClass.getAgeOfDobOfReceivedYear(dobSourceList.get(i)),
                addressSourceList.get(i),
                countryTemp = (getLastStringOf(addressSourceList.get(i), ",")).toUpperCase().substring(1), //changes for country

                isoA2 = dataHandlingClass.getISOAlpha2FromList(countryTemp),
                isoA3 = dataHandlingClass.getISOAlpha3FromList(countryTemp),
                currencyCode = dataHandlingClass.getCurrencyCodeFromList(countryTemp),
                currencyName = dataHandlingClass.getCurrencyNameFromList(countryTemp),
                isIndependent = dataHandlingClass.getInDependentFromList(countryTemp),

                raceSourceList.get(i), maritalString = dataHandlingClass.getShuffledMaritalString(age),
                occupationString = dataHandlingClass.getShuffledOccupation(age),
                email = dataHandlingClass.getEmailAddress(tempSourceName[FIRSTNAME], tempSourceName[LASTNAME]),
                tel = dataHandlingClass.getRandomPhoneNumberByCountry(countryTemp),
                bloodString = dataHandlingClass.getShuffledBloodType(),
                yesNoString = dataHandlingClass.getYesNo(),
                scoreTemp = DataHandlingClass.randBetween(DataHandlingClass.minMaxScore[0],
                        DataHandlingClass.minMaxScore[1] - 1),
                scoreLevelString = dataHandlingClass.getScoreCompareLevel(scoreTemp),
                randomLevelString = dataHandlingClass.getShuffledLevel(),
                bankAmountString = scoreTemp + " " + currencyCode,

                //random data string from shuffle file list
                firstShuffle1String = returnNullAsEmpty(
                        shuffle1String.split("\\" + seperatorForShuffleFile)[0]),
                subsf1 = getLastStringOf(shuffle1String, seperatorForShuffleFile),

                firstShuffle2String = returnNullAsEmpty(
                        shuffle2String.split("\\" + seperatorForShuffleFile)[0]),
                subsf2 = getLastStringOf(shuffle2String, seperatorForShuffleFile),

                firstShuffle3String = returnNullAsEmpty(
                        shuffle3String.split("\\" + seperatorForShuffleFile)[0]),
                subsf3 = getLastStringOf(shuffle3String, seperatorForShuffleFile),

                firstShuffle4String = returnNullAsEmpty(
                        shuffle4String.split("\\" + seperatorForShuffleFile)[0]),
                subsf4 = getLastStringOf(shuffle4String, seperatorForShuffleFile),

                firstShuffle5String = returnNullAsEmpty(
                        shuffle5String.split("\\" + seperatorForShuffleFile)[0]),
                subsf5 = getLastStringOf(shuffle5String, seperatorForShuffleFile),

                firstShuffle6String = returnNullAsEmpty(
                        shuffle6String.split("\\" + seperatorForShuffleFile)[0]),
                subsf6 = getLastStringOf(shuffle6String, seperatorForShuffleFile),

                firstShuffle7String = returnNullAsEmpty(
                        shuffle7String.split("\\" + seperatorForShuffleFile)[0]),
                subsf7 = getLastStringOf(shuffle7String, seperatorForShuffleFile),

                firstShuffle8String = returnNullAsEmpty(
                        shuffle8String.split("\\" + seperatorForShuffleFile)[0]),
                subsf8 = getLastStringOf(shuffle8String, seperatorForShuffleFile),

                firstShuffle9String = returnNullAsEmpty(
                        shuffle9String.split("\\" + seperatorForShuffleFile)[0]),
                subsf9 = getLastStringOf(shuffle9String, seperatorForShuffleFile),

                firstShuffle10String = returnNullAsEmpty(
                        shuffle10String.split("\\" + seperatorForShuffleFile)[0]),
                subsf10 = getLastStringOf(shuffle10String, seperatorForShuffleFile),

                firstShuffle11String = returnNullAsEmpty(
                        shuffle11String.split("\\" + seperatorForShuffleFile)[0]),
                subsf11 = getLastStringOf(shuffle11String, seperatorForShuffleFile),

                firstShuffle12String = returnNullAsEmpty(
                        shuffle12String.split("\\" + seperatorForShuffleFile)[0]),
                subsf12 = getLastStringOf(shuffle12String, seperatorForShuffleFile) });
        z++;
        /* to store in global variable*/
        idList.add(idTemp);
        rfidList.add(refTemp);
        firstNameList.add(tempSourceName[FIRSTNAME]);
        lastNameList.add(tempSourceName[LASTNAME]);
        fullNameList.add(nameSourceList.get(i));
        otherNameList.add(otherName);
        modFirstNameList.add(modFirstName);
        modLastNameList.add(modLastName);
        modFullNameList.add(modFullName);
        genderList.add(genderSourceList.get(i));
        dobList.add(dobSourceList.get(i));
        ageList.add(age + "");
        addressList.add(addressSourceList.get(i));
        countryList.add(countryTemp);
        isoAlpha2List.add(isoA2);
        isoAlpha3List.add(isoA3);
        currencyCodeList.add(currencyCode);
        currencyNameList.add(currencyName);
        isIndependentList.add(isIndependent);
        raceList.add(raceSourceList.get(i));
        maritalList.add(maritalString);
        occupationList.add(occupationString);
        emailList.add(email);
        telephoneList.add(tel);
        bloodTypeList.add(bloodString);
        yesNoList.add(yesNoString);
        scoreList.add(scoreTemp + "");
        scoreLevelList.add(scoreLevelString);
        levelList.add(randomLevelString);
        bankAmount.add(bankAmountString);

        shuffle1.add(firstShuffle1String);
        shuffle1sub.add(returnNullAsEmpty(subsf1));

        shuffle2.add(firstShuffle2String);
        shuffle2sub.add(returnNullAsEmpty(subsf2));

        shuffle3.add(firstShuffle3String);
        shuffle3sub.add(returnNullAsEmpty(subsf3));

        shuffle4.add(firstShuffle4String);
        shuffle4sub.add(returnNullAsEmpty(subsf4));

        shuffle5.add(firstShuffle5String);
        shuffle5sub.add(returnNullAsEmpty(subsf5));

        shuffle6.add(firstShuffle6String);
        shuffle6sub.add(returnNullAsEmpty(subsf6));

        shuffle7.add(firstShuffle7String);
        shuffle7sub.add(returnNullAsEmpty(subsf7));

        shuffle8.add(firstShuffle8String);
        shuffle8sub.add(returnNullAsEmpty(subsf8));

        shuffle9.add(firstShuffle9String);
        shuffle9sub.add(returnNullAsEmpty(subsf9));

        shuffle10.add(firstShuffle10String);
        shuffle10sub.add(returnNullAsEmpty(subsf10));

        shuffle11.add(firstShuffle11String);
        shuffle11sub.add(returnNullAsEmpty(subsf11));

        shuffle12.add(firstShuffle12String);
        shuffle12sub.add(returnNullAsEmpty(subsf12));
    }

    //Iterate over data and write to sheet
    Set<String> keyset = data.keySet();
    int rownum = 0;
    for (String key : keyset) {
        Row row = sheet.createRow(rownum++);
        Object[] objArr = data.get(key);
        int cellnum = 0;
        for (Object obj : objArr) {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof String)
                cell.setCellValue((String) obj);
            else if (obj instanceof Integer)
                cell.setCellValue((Integer) obj);
        }
    }
    try {
        //Write the workbook in file system
        if (jfile == null) {
            FileOutputStream out = new FileOutputStream(new File(AllFileList.EXCELFILE));
            workbook.write(out);
            out.close();
            System.out.println(AllFileList.EXCELFILE + " written successfully on disk.");
        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null,
                "Please make sure that no file name: " + AllFileList.EXCELFILE + " is open\n"
                        + "Please close the mentioned file and try again",
                "Operation Failed!", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
        return true;
    }
    return true;
}

From source file:cn.afterturn.easypoi.excel.export.base.BaseExportService.java

License:Apache License

/**
 * Cell//from www  .j a va2s . c  om
 */
public void createImageCell(Drawing patriarch, ExcelExportEntity entity, Row row, int i, String imagePath,
        Object obj) throws Exception {
    Cell cell = row.createCell(i);
    byte[] value = null;
    if (entity.getExportImageType() != 1) {
        value = (byte[]) (entity.getMethods() != null ? getFieldBySomeMethod(entity.getMethods(), obj)
                : entity.getMethod().invoke(obj, new Object[] {}));
    }
    createImageCell(cell, 50 * entity.getHeight(), entity.getExportImageType() == 1 ? imagePath : null, value);

}

From source file:cn.afterturn.easypoi.excel.export.base.BaseExportService.java

License:Apache License

/**
 * Cell// www  .j  a v a 2s  .  c o m
 */
public void createStringCell(Row row, int index, String text, CellStyle style, ExcelExportEntity entity) {
    Cell cell = row.createCell(index);
    if (style != null && style.getDataFormat() > 0 && style.getDataFormat() < 12) {
        cell.setCellValue(Double.parseDouble(text));
        cell.setCellType(CellType.NUMERIC);
    } else {
        RichTextString rtext;
        if (type.equals(ExcelType.HSSF)) {
            rtext = new HSSFRichTextString(text);
        } else {
            rtext = new XSSFRichTextString(text);
        }
        cell.setCellValue(rtext);
    }
    if (style != null) {
        cell.setCellStyle(style);
    }
    addStatisticsData(index, text, entity);
}

From source file:cn.afterturn.easypoi.excel.export.base.BaseExportService.java

License:Apache License

/**
 * Cell/*from   ww  w  .  ja va2  s  .c o m*/
 */
public void createDoubleCell(Row row, int index, String text, CellStyle style, ExcelExportEntity entity) {
    Cell cell = row.createCell(index);
    cell.setCellType(CellType.NUMERIC);
    if (text != null && text.length() > 0) {
        try {
            cell.setCellValue(Double.parseDouble(text));
        } catch (NumberFormatException e) {
            cell.setCellType(CellType.STRING);
            cell.setCellValue(text);
        }
    }

    if (style != null) {
        cell.setCellStyle(style);
    }
    addStatisticsData(index, text, entity);
}

From source file:cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil.java

License:Apache License

private void setForEeachRowCellValue(boolean isCreate, Row row, int columnIndex, Object t,
        List<ExcelForEachParams> columns, Map<String, Object> map, int rowspan, int colspan,
        MergedRegionHelper mergedRegionHelper) throws Exception {
    //cell??//w w w.j a  v  a 2 s.  com
    for (int i = 0; i < rowspan; i++) {
        int size = columns.size();//?
        for (int j = columnIndex, max = columnIndex + colspan; j < max; j++) {
            if (row.getCell(j) == null) {
                row.createCell(j);
                CellStyle style = row.getRowNum() % 2 == 0
                        ? getStyles(false, size <= j - columnIndex ? null : columns.get(j - columnIndex))
                        : getStyles(true, size <= j - columnIndex ? null : columns.get(j - columnIndex));
                //styler??,?Excel,??Excel?
                if (style != null) {
                    row.getCell(j).setCellStyle(style);
                }
            }

        }
        if (i < rowspan - 1) {
            row = row.getSheet().getRow(row.getRowNum() + 1);
        }
    }
    //?
    ExcelForEachParams params;
    row = row.getSheet().getRow(row.getRowNum() - rowspan + 1);
    for (int k = 0; k < rowspan; k++) {
        int ci = columnIndex;
        short high = columns.get(0).getHeight();
        int n = k;
        while (n > 0) {
            if (columns.get(n * colspan).getHeight() == 0) {
                n--;
            } else {
                high = columns.get(n * colspan).getHeight();
                break;
            }
        }
        row.setHeight(high);
        for (int i = 0; i < colspan && i < columns.size(); i++) {
            boolean isNumber = false;
            params = columns.get(colspan * k + i);
            tempCreateCellSet.add(row.getRowNum() + "_" + (ci));
            if (params == null) {
                continue;
            }
            if (StringUtils.isEmpty(params.getName()) && StringUtils.isEmpty(params.getConstValue())) {
                row.getCell(ci).setCellStyle(params.getCellStyle());
                ci = ci + params.getColspan();
                continue;
            }
            String val = null;
            Object obj = null;
            //??
            if (StringUtils.isEmpty(params.getName())) {
                val = params.getConstValue();
            } else {
                String tempStr = new String(params.getName());
                if (isNumber(tempStr)) {
                    isNumber = true;
                    tempStr = tempStr.replaceFirst(NUMBER_SYMBOL, "");
                }
                map.put(teplateParams.getTempParams(), t);
                obj = eval(tempStr, map);
                val = obj.toString();
            }
            if (obj != null && obj instanceof ImageEntity) {
                ImageEntity img = (ImageEntity) obj;
                row.getCell(ci).setCellValue("");
                if (img.getRowspan() > 1 || img.getColspan() > 1) {
                    img.setHeight(0);
                    row.getCell(ci).getSheet()
                            .addMergedRegion(new CellRangeAddress(row.getCell(ci).getRowIndex(),
                                    row.getCell(ci).getRowIndex() + img.getRowspan() - 1,
                                    row.getCell(ci).getColumnIndex(),
                                    row.getCell(ci).getColumnIndex() + img.getColspan() - 1));
                }
                createImageCell(row.getCell(ci), img.getHeight(), img.getRowspan(), img.getColspan(),
                        img.getUrl(), img.getData());
            } else if (isNumber && StringUtils.isNotEmpty(val)) {
                row.getCell(ci).setCellValue(Double.parseDouble(val));
            } else {
                try {
                    row.getCell(ci).setCellValue(val);
                } catch (Exception e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
            if (params.getCellStyle() != null) {
                row.getCell(ci).setCellStyle(params.getCellStyle());
            }
            //??
            if (params.isNeedSum()) {
                templateSumHandler.addValueOfKey(params.getName(), val);
            }
            //??,?????
            setMergedRegionStyle(row, ci, params);
            //??
            if ((params.getRowspan() != 1 || params.getColspan() != 1)
                    && !mergedRegionHelper.isMergedRegion(row.getRowNum() + 1, ci)
                    && PoiCellUtil.isMergedRegion(row.getSheet(), row.getRowNum(), ci)) {
                PoiMergeCellUtil.addMergedRegion(row.getSheet(), row.getRowNum(),
                        row.getRowNum() + params.getRowspan() - 1, ci, ci + params.getColspan() - 1);
            }
            ci = ci + params.getColspan();
        }
        row = row.getSheet().getRow(row.getRowNum() + 1);
    }

}

From source file:cn.afterturn.easypoi.excel.imports.ExcelImportService.java

License:Apache License

/**
 * ??/*from   w w w  . ja  v a  2s  .  c  o m*/
 */
public boolean verifyingDataValidity(Object object, Row row, ImportParams params, boolean isMap,
        StringBuilder fieldErrorMsg) {
    boolean isAdd = true;
    Cell cell = null;
    if (params.isNeedVerify()) {
        String errorMsg = PoiValidationUtil.validation(object, params.getVerifyGroup());
        if (StringUtils.isNotEmpty(errorMsg)) {
            cell = row.createCell(row.getLastCellNum());
            cell.setCellValue(errorMsg);
            if (object instanceof IExcelModel) {
                IExcelModel model = (IExcelModel) object;
                model.setErrorMsg(errorMsg);
            }
            isAdd = false;
            verifyFail = true;
        }
    }
    if (params.getVerifyHandler() != null) {
        ExcelVerifyHandlerResult result = params.getVerifyHandler().verifyHandler(object);
        if (!result.isSuccess()) {
            if (cell == null) {
                cell = row.createCell(row.getLastCellNum());
            }
            cell.setCellValue(
                    (StringUtils.isNoneBlank(cell.getStringCellValue()) ? cell.getStringCellValue() + "," : "")
                            + result.getMsg());
            if (object instanceof IExcelModel) {
                IExcelModel model = (IExcelModel) object;
                model.setErrorMsg(
                        (StringUtils.isNoneBlank(model.getErrorMsg()) ? model.getErrorMsg() + "," : "")
                                + result.getMsg());
            }
            isAdd = false;
            verifyFail = true;
        }
    }
    if ((params.isNeedVerify() || params.getVerifyHandler() != null) && fieldErrorMsg.length() > 0) {
        if (object instanceof IExcelModel) {
            IExcelModel model = (IExcelModel) object;
            model.setErrorMsg((StringUtils.isNoneBlank(model.getErrorMsg()) ? model.getErrorMsg() + "," : "")
                    + fieldErrorMsg.toString());
        }
        if (cell == null) {
            cell = row.createCell(row.getLastCellNum());
        }
        cell.setCellValue(
                (StringUtils.isNoneBlank(cell.getStringCellValue()) ? cell.getStringCellValue() + "," : "")
                        + fieldErrorMsg.toString());
        isAdd = false;
        verifyFail = true;
    }
    if (cell != null) {
        cell.setCellStyle(errorCellStyle);
        failRow.add(row);
        if (isMap) {
            ((Map) object).put("excelErrorMsg", cell.getStringCellValue());
        }
    } else {
        successRow.add(row);
    }
    return isAdd;
}

From source file:cn.bzvs.excel.export.base.ExcelExportBase.java

License:Apache License

/**
 * Cell/*from  w  ww  .j av a 2  s.  com*/
 *
 * @param patriarch
 * @param entity
 * @param row
 * @param i
 * @param imagePath
 * @param obj
 * @throws Exception
 */
public void createImageCell(Drawing patriarch, ExcelExportEntity entity, Row row, int i, String imagePath,
        Object obj) throws Exception {
    row.setHeight((short) (50 * entity.getHeight()));
    row.createCell(i);
    ClientAnchor anchor;
    if (type.equals(ExcelType.HSSF)) {
        anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) i, row.getRowNum(), (short) (i + 1),
                row.getRowNum() + 1);
    } else {
        anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) i, row.getRowNum(), (short) (i + 1),
                row.getRowNum() + 1);
    }

    byte[] value = null;
    if (entity.getExportImageType() == 1) {
        if (StringUtils.isNotEmpty(imagePath)) {
            value = ImageCache.getImage(imagePath);
        }
    } else {
        value = (byte[]) (entity.getMethods() != null ? getFieldBySomeMethod(entity.getMethods(), obj)
                : entity.getMethod().invoke(obj, new Object[] {}));
    }
    if (value != null) {
        patriarch.createPicture(anchor, row.getSheet().getWorkbook().addPicture(value, getImageType(value)));
    }

}

From source file:cn.bzvs.excel.export.base.ExcelExportBase.java

License:Apache License

/**
 * Cell//  w ww  .ja  v a2 s  .c o m
 *
 * @param row
 * @param index
 * @param text
 * @param style
 * @param entity
 */
public void createStringCell(Row row, int index, String text, CellStyle style, ExcelExportEntity entity) {
    Cell cell = row.createCell(index);
    if (style != null && style.getDataFormat() > 0 && style.getDataFormat() < 12) {
        cell.setCellValue(Double.parseDouble(text));
        cell.setCellType(Cell.CELL_TYPE_NUMERIC);
    } else {
        RichTextString Rtext;
        if (type.equals(ExcelType.HSSF)) {
            Rtext = new HSSFRichTextString(text);
        } else {
            Rtext = new XSSFRichTextString(text);
        }
        cell.setCellValue(Rtext);
    }
    if (style != null) {
        cell.setCellStyle(style);
    }
    addStatisticsData(index, text, entity);
}

From source file:cn.bzvs.excel.export.base.ExcelExportBase.java

License:Apache License

/**
 * Cell//from w  w w .  ja  v a  2s  .  co  m
 *
 * @param row
 * @param index
 * @param text
 * @param style
 * @param entity
 */
public void createDoubleCell(Row row, int index, String text, CellStyle style, ExcelExportEntity entity) {
    Cell cell = row.createCell(index);
    if (text != null && text.length() > 0) {
        cell.setCellValue(Double.parseDouble(text));
    } else {
        cell.setCellValue(-1);
    }
    cell.setCellType(Cell.CELL_TYPE_NUMERIC);
    if (style != null) {
        cell.setCellStyle(style);
    }
    addStatisticsData(index, text, entity);
}

From source file:cn.bzvs.excel.export.template.ExcelExportOfTemplateUtil.java

License:Apache License

private void setForEeachRowCellValue(boolean isCreate, Row row, int columnIndex, Object t,
        List<ExcelForEachParams> columns, Map<String, Object> map, int rowspan, int colspan,
        MergedRegionHelper mergedRegionHelper) throws Exception {
    //cell??/*from ww  w.j  a  va  2s .co  m*/
    for (int i = 0; i < rowspan; i++) {
        int size = columns.size();//?
        for (int j = columnIndex, max = columnIndex + colspan; j < max; j++) {
            if (row.getCell(j) == null) {
                row.createCell(j);
                CellStyle style = row.getRowNum() % 2 == 0
                        ? getStyles(false, size >= j - columnIndex ? null : columns.get(j - columnIndex))
                        : getStyles(true, size >= j - columnIndex ? null : columns.get(j - columnIndex));
                //styler??,?Excel,??Excel?
                if (style != null)
                    row.getCell(j).setCellStyle(style);
            }

        }
        if (i < rowspan - 1) {
            row = row.getSheet().getRow(row.getRowNum() + 1);
        }
    }
    //?
    ExcelForEachParams params;
    row = row.getSheet().getRow(row.getRowNum() - rowspan + 1);
    for (int k = 0; k < rowspan; k++) {
        int ci = columnIndex;//cell??
        row.setHeight(columns.get(0 * colspan).getHeight());
        for (int i = 0; i < colspan && i < columns.size(); i++) {
            boolean isNumber = false;
            params = columns.get(colspan * k + i);
            tempCreateCellSet.add(row.getRowNum() + "_" + (ci));
            if (params == null) {
                continue;
            }
            if (StringUtils.isEmpty(params.getName()) && StringUtils.isEmpty(params.getConstValue())) {
                row.getCell(ci).setCellStyle(columns.get(i).getCellStyle());
                ci = ci + columns.get(i).getColspan();
                continue;
            }
            String val = null;
            //??
            if (StringUtils.isEmpty(params.getName())) {
                val = params.getConstValue();
            } else {
                String tempStr = new String(params.getName());
                if (isNumber(tempStr)) {
                    isNumber = true;
                    tempStr = tempStr.replaceFirst(NUMBER_SYMBOL, "");
                }
                map.put(teplateParams.getTempParams(), t);
                val = eval(tempStr, map).toString();
            }
            if (isNumber && StringUtils.isNotEmpty(val)) {
                row.getCell(ci).setCellValue(Double.parseDouble(val));
                row.getCell(ci).setCellType(Cell.CELL_TYPE_NUMERIC);
            } else {
                try {
                    row.getCell(ci).setCellValue(val);
                } catch (Exception e) {
                    e.getMessage();
                }
            }
            row.getCell(ci).setCellStyle(columns.get(i).getCellStyle());
            //??
            if (params.isNeedSum()) {
                templateSumHanlder.addValueOfKey(params.getName(), val);
            }
            //??,?????
            setMergedRegionStyle(row, ci, columns.get(i));
            //??
            if ((params.getRowspan() != 1 || params.getColspan() != 1)
                    && !mergedRegionHelper.isMergedRegion(row.getRowNum() + 1, ci)) {
                row.getSheet().addMergedRegion(new CellRangeAddress(row.getRowNum(),
                        row.getRowNum() + params.getRowspan() - 1, ci, ci + params.getColspan() - 1));
            }
            ci = ci + params.getColspan();
        }
        row = row.getSheet().getRow(row.getRowNum() + 1);
    }

}