Example usage for org.apache.poi.hssf.usermodel HSSFSheet createDrawingPatriarch

List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet createDrawingPatriarch

Introduction

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

Prototype

@Override
public HSSFPatriarch createDrawingPatriarch() 

Source Link

Document

Creates the top-level drawing patriarch.

Usage

From source file:com.selfsoft.business.service.impl.TbFixEntrustServiceImpl.java

public void printTbFixEntrustTemplate(OutputStream os, String tpl, Long tbFixEntrustId) {

    /*/*from  www .  ja v a  2 s .  c  om*/
     * Workbook wb = null;
     * 
     * WritableWorkbook wwb = null;
     * 
     * WritableSheet ws = null;
     * 
     * try {
     * 
     * wb = Workbook.getWorkbook(this.getClass().getResourceAsStream(tpl));
     * 
     * wwb = Workbook.createWorkbook(os, wb);
     * 
     * ws = wwb.getSheet(0);
     * 
     * WritableCellFormat wcf = new WritableCellFormat();
     * 
     * wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
     * 
     * wcf.setAlignment(Alignment.CENTRE);
     * 
     * wcf.setWrap(true);
     * 
     * TbFixEntrust tbFixEntrust = this.findById(tbFixEntrustId);
     * 
     * TbCustomer tbCustomer =
     * tbCustomerService.findById(tbFixEntrust.getTbCustomer().getId());
     * 
     * TbCarInfo tbCarInfo =
     * tbCarInfoService.findById(tbFixEntrust.getTbCarInfo().getId());
     * 
     * Label label = null;
     * 
     * label = new Label(0, 10, tbFixEntrust.getEntrustCode(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(8, 10, tbCarInfo.getPurchaseDate()== null ? "" :
     * CommonMethod.parseDateToString(tbCarInfo.getPurchaseDate(),
     * "yyyy-MM-dd"), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(14, 10, tbFixEntrust.getTmUser().getUserRealName(),
     * wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(20, 10, tbCarInfo.getLicenseCode(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(27, 10,
     * tbCarInfo.getTmCarModelType().getModelName(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(32, 10, tbFixEntrust.getEnterStationKilo() == null
     * ? "" : tbFixEntrust.getEnterStationKilo().toString(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(41, 10, tbCarInfo.getColor() == null ? "" :
     * tbCarInfo.getColor().toString(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(45, 10, tbFixEntrust.getFixDate() == null ? "" :
     * CommonMethod.parseDateToString(tbFixEntrust.getFixDate(),
     * "yyyy-MM-dd HH:mm:ss"), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(0, 15, tbCustomer.getCustomerName() == null ? "" :
     * tbCustomer.getCustomerName(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(8, 15, tbCustomer.getTelephone() == null ? "" :
     * tbCustomer.getTelephone(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(14, 15, tbCustomer.getPhone() == null ? "" :
     * tbCustomer.getPhone(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(20, 15, tbCarInfo.getChassisCode() == null ? "" :
     * tbCarInfo.getChassisCode(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(32, 15, tbCarInfo.getEngineCode() == null ? "" :
     * tbCarInfo.getEngineCode(),wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(45, 15, tbFixEntrust.getEstimateDate() == null ? ""
     * : CommonMethod.parseDateToString(tbFixEntrust.getEstimateDate(),
     * "yyyy-MM-dd HH:mm:ss"),wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(8, 18, tbCustomer.getCustomerName() == null ? "" :
     * tbCustomer.getCustomerName(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(0, 22, tbCustomer.getAddress() == null ? "" :
     * tbCustomer.getAddress(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(8, 25, tbCustomer.getZipCode() == null ? "" :
     * tbCustomer.getZipCode(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(20, 20, (tbFixEntrust.getWrongDescribe() ==
     * null?"":tbFixEntrust.getWrongDescribe()) +";" +
     * (tbFixEntrust.getBeforeFixState
     * ()==null?"":tbFixEntrust.getBeforeFixState()) + ";" +
     * (tbFixEntrust.getCheckResult
     * ()==null?"":tbFixEntrust.getCheckResult()) + ";" +
     * (tbFixEntrust.getRemark()==null?"":tbFixEntrust.getRemark()), wcf);
     * 
     * ws.addCell(label);
     * 
     * wwb.write();
     * 
     * } catch (BiffException e) {
     * 
     * e.printStackTrace(); } catch (IOException e) {
     * 
     * e.printStackTrace(); } catch (RowsExceededException e) { // TODO
     * Auto-generated catch block e.printStackTrace(); } catch
     * (WriteException e) { // TODO Auto-generated catch block
     * e.printStackTrace(); } finally {
     * 
     * if (null != wb) {
     * 
     * wb.close();
     * 
     * }
     * 
     * if (null != wwb) {
     * 
     * try { wwb.close(); } catch (WriteException e) {
     * 
     * e.printStackTrace(); } catch (IOException e) {
     * 
     * e.printStackTrace(); } }
     * 
     * }
     */

    try {

        TbFixEntrust tbFixEntrust = this.findById(tbFixEntrustId);

        TbCustomer tbCustomer = tbCustomerService.findById(tbFixEntrust.getTbCustomer().getId());

        TbCarInfo tbCarInfo = tbCarInfoService.findById(tbFixEntrust.getTbCarInfo().getId());

        List<TbFixEntrustContent> tbFixEntrustContentList = tbFixEntrustContentService
                .findTbFixEnTrustContentListByTbFixEntrustId(tbFixEntrustId);

        /*
         * List<TbMaintainPartContent> maintainList =
         * tbMaintainPartContentService
         * .getViewEntrustMaintianContent(tbFixEntrustId);
         * 
         * 
         * List<TmStockOutDetVo> solePartList =
         * tmStockOutService.getSellByEntrustCode
         * (tbFixEntrust.getEntrustCode());
         */

        /**
         * ??
         */
        List<TbMaintianVo> maintianvos = tbMaintainPartContentService
                .getTbMaintianDetailVosByEntrustId(tbFixEntrust.getId(), Constants.BALANCE_ALL);

        /**
         * ?
         */
        List<TmStockOutDetVo> tmStockOutDetVos = tmStockOutService
                .getSellDetailByEntrustCode(tbFixEntrust.getEntrustCode(), Constants.BALANCE_ALL);

        /**
         * ??
         */

        List<TbMaintianVo> partAll = new ArrayList<TbMaintianVo>();

        if (null != maintianvos && maintianvos.size() > 0) {

            for (TbMaintianVo tbMaintianVo : maintianvos) {

                partAll.add(tbMaintianVo);

            }
        }

        if (null != tmStockOutDetVos && tmStockOutDetVos.size() > 0) {

            for (TmStockOutDetVo tmStockOutDetVo : tmStockOutDetVos) {

                TbMaintianVo tbMaintianVo = new TbMaintianVo();

                tbMaintianVo.setPartId(tmStockOutDetVo.getPartinfoId());

                tbMaintianVo.setHouseName(tmStockOutDetVo.getHouseName());

                tbMaintianVo.setPartCode(tmStockOutDetVo.getPartCode());

                tbMaintianVo.setPartName(tmStockOutDetVo.getPartName());

                tbMaintianVo.setUnitName(tmStockOutDetVo.getUnitName());

                tbMaintianVo.setPrice(tmStockOutDetVo.getPrice());

                tbMaintianVo.setPartQuantity(tmStockOutDetVo.getQuantity());

                tbMaintianVo.setTotal(tmStockOutDetVo.getTotal());

                tbMaintianVo.setIsFree(tmStockOutDetVo.getIsFree());

                tbMaintianVo.setProjectType(tmStockOutDetVo.getProjectType());

                tbMaintianVo.setZl(tmStockOutDetVo.getZl());

                tbMaintianVo.setXmlx(tmStockOutDetVo.getXmlx());

                partAll.add(tbMaintianVo);
            }
        }

        int fixSize = (tbFixEntrustContentList == null ? 0 : tbFixEntrustContentList.size());

        int maintainSize = (maintianvos == null ? 0 : maintianvos.size());

        int solePartSize = (tmStockOutDetVos == null ? 0 : tmStockOutDetVos.size());

        int partAllSize = (partAll == null ? 0 : partAll.size());

        HSSFWorkbook workbook = new HSSFWorkbook(this.getClass().getResourceAsStream(tpl));

        HSSFSheet sheet = workbook.getSheetAt(0);

        sheet.setMargin(HSSFSheet.LeftMargin, (double) 0.5);

        sheet.setMargin(HSSFSheet.RightMargin, (double) 0.5);

        HSSFCellStyle style = workbook.createCellStyle();

        style.setWrapText(true);

        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFFont font = workbook.createFont();

        font.setFontName("");

        font.setFontHeightInPoints((short) 9);

        style.setFont(font);

        HSSFRow row = null;

        HSSFCell cell = null;

        ByteArrayOutputStream byteArrayOutImgLion = new ByteArrayOutputStream();

        ByteArrayOutputStream byteArrayOutDBZF = new ByteArrayOutputStream();

        BufferedImage bufferImgLion = ImageIO.read(this.getClass().getResourceAsStream("/lion.png"));

        BufferedImage bufferImgDFBZ = ImageIO.read(this.getClass().getResourceAsStream("/dfbz.png"));

        ImageIO.write(bufferImgLion, "png", byteArrayOutImgLion);

        ImageIO.write(bufferImgDFBZ, "png", byteArrayOutDBZF);

        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

        HSSFClientAnchor anchorLion = new HSSFClientAnchor(0, 0, 1023, 200, (short) 48, 0, (short) 53, 7);

        HSSFClientAnchor anchorDBZF = new HSSFClientAnchor(0, 0, 1023, 200, (short) 0, 0, (short) 8, 7);

        patriarch.createPicture(anchorLion,
                workbook.addPicture(byteArrayOutImgLion.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

        patriarch.createPicture(anchorDBZF,
                workbook.addPicture(byteArrayOutDBZF.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

        row = sheet.getRow(10);

        cell = row.getCell(0);

        String entrustCode = tbFixEntrust.getEntrustCode();

        String[] es = entrustCode.split("-");

        String newCode = "RO" + es[0].substring(2, 6) + es[1];

        // cell.setCellValue(tbFixEntrust.getEntrustCode());

        cell.setCellValue(newCode);

        row = sheet.getRow(10);

        cell = row.getCell(8);

        cell.setCellValue(tbCarInfo.getPurchaseDate() == null ? ""
                : CommonMethod.parseDateToString(tbCarInfo.getPurchaseDate(), "yyyy-MM-dd"));

        row = sheet.getRow(10);

        cell = row.getCell(14);

        cell.setCellValue(
                tbFixEntrust.getTmUser().getUserRealName() == null ? tbFixEntrust.getTmUser().getUserName()
                        : tbFixEntrust.getTmUser().getUserRealName());

        row = sheet.getRow(10);

        cell = row.getCell(19);

        cell.setCellValue(tbCarInfo.getLicenseCode());

        row = sheet.getRow(10);

        cell = row.getCell(27);

        cell.setCellStyle(style);

        cell.setCellValue(tbCarInfo.getTmCarModelType().getModelName());

        row = sheet.getRow(10);

        cell = row.getCell(32);

        cell.setCellValue(
                tbFixEntrust.getEnterStationKilo() == null ? ""
                        : new BigDecimal(tbFixEntrust.getEnterStationKilo().toString())
                                .divide(new BigDecimal("1.00"), 0, BigDecimal.ROUND_HALF_UP).toString()
                                + "   Km");

        row = sheet.getRow(10);

        cell = row.getCell(41);

        cell.setCellValue(tbCarInfo.getColor() == null ? "" : tbCarInfo.getColor().toString());

        row = sheet.getRow(10);

        cell = row.getCell(45);

        cell.setCellValue(tbFixEntrust.getFixDate() == null ? ""
                : CommonMethod.parseDateToString(tbFixEntrust.getFixDate(), Constants.TIMEFORMATOFMINUTE));

        row = sheet.getRow(15);

        cell = row.getCell(0);

        cell.setCellValue(tbCustomer.getCustomerName() == null ? "" : tbCustomer.getContractPerson());

        row = sheet.getRow(15);

        cell = row.getCell(8);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(15);

        cell = row.getCell(14);

        cell.setCellValue(tbCustomer.getPhone() == null ? "" : tbCustomer.getPhone());

        row = sheet.getRow(15);

        cell = row.getCell(20);

        cell.setCellValue(tbCarInfo.getChassisCode() == null ? "" : tbCarInfo.getChassisCode());

        row = sheet.getRow(15);

        cell = row.getCell(32);

        // cell.setCellStyle(style);

        cell.setCellValue(tbCarInfo.getEngineCode() == null ? "" : tbCarInfo.getEngineCode());

        row = sheet.getRow(15);

        cell = row.getCell(45);

        // cell.setCellStyle(style);

        cell.setCellValue(tbFixEntrust.getEstimateDate() == null ? ""
                : CommonMethod.parseDateToString(tbFixEntrust.getEstimateDate(), Constants.TIMEFORMATOFMINUTE));

        row = sheet.getRow(18);

        cell = row.getCell(8);

        cell.setCellValue(tbCustomer.getCustomerName() == null ? "" : tbCustomer.getCustomerName());

        row = sheet.getRow(20);

        cell = row.getCell(8);

        cell.setCellStyle(style);

        cell.setCellValue(tbCustomer.getAddress() == null ? "" : tbCustomer.getAddress());

        row = sheet.getRow(25);

        cell = row.getCell(8);

        cell.setCellValue(tbCustomer.getZipCode() == null ? "" : tbCustomer.getZipCode());

        row = sheet.getRow(20);

        cell = row.getCell(23);

        cell.setCellStyle(style);

        cell.setCellValue((tbFixEntrust.getWrongDescribe() == null || "".equals(tbFixEntrust.getWrongDescribe())
                ? ""
                : tbFixEntrust.getWrongDescribe() + ";")
                + (tbFixEntrust.getBeforeFixState() == null || "".equals(tbFixEntrust.getBeforeFixState()) ? ""
                        : tbFixEntrust.getBeforeFixState() + ";"));

        Double fixCount = tbFixEntrustContentService.countTbFixEnTrustContentByTbFixEntrustId(tbFixEntrustId);

        row = sheet.getRow(70);

        cell = row.getCell(32);

        cell.setCellStyle(style);

        cell.setCellValue(new BigDecimal(fixCount).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP)
                .toString());

        row = sheet.getRow(70);

        cell = row.getCell(48);

        cell.setCellStyle(style);

        cell.setCellValue("0.00");

        BigDecimal partPriceAll = new BigDecimal("0.00");

        if (partAllSize > 0) {

            for (int i = 0; i < partAllSize; i++) {

                TbMaintianVo tbMaintianVo = partAll.get(i);

                BigDecimal total = new BigDecimal(tbMaintianVo.getPrice())
                        .multiply(new BigDecimal(tbMaintianVo.getPartQuantity()));

                partPriceAll = partPriceAll.add(total);

            }

        }

        row = sheet.getRow(73);

        cell = row.getCell(33);

        cell.setCellStyle(style);

        cell.setCellValue(partPriceAll.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        BigDecimal total = new BigDecimal(fixCount).add(partPriceAll).divide(new BigDecimal("1.00"), 2,
                BigDecimal.ROUND_HALF_UP);

        row = sheet.getRow(76);

        cell = row.getCell(34);

        cell.setCellStyle(style);

        cell.setCellValue(total.toString());

        int page = 1;

        if (fixSize / 5 >= (maintainSize + solePartSize) / 13) {

            page = fixSize / 5;

        }

        else {

            page = (maintainSize + solePartSize) / 13;

        }

        for (int i = 0; i < page; i++) {

            int p = 0;

            int k = 0;

            HSSFSheet sheetClone = workbook.cloneSheet(0);

            byteArrayOutImgLion = new ByteArrayOutputStream();

            byteArrayOutDBZF = new ByteArrayOutputStream();

            bufferImgLion = ImageIO.read(this.getClass().getResourceAsStream("/lion.png"));

            bufferImgDFBZ = ImageIO.read(this.getClass().getResourceAsStream("/dfbz.png"));

            ImageIO.write(bufferImgLion, "png", byteArrayOutImgLion);

            ImageIO.write(bufferImgDFBZ, "png", byteArrayOutDBZF);

            patriarch = sheetClone.createDrawingPatriarch();

            anchorLion = new HSSFClientAnchor(0, 0, 1023, 200, (short) 48, 0, (short) 53, 7);

            anchorDBZF = new HSSFClientAnchor(0, 0, 1023, 200, (short) 0, 0, (short) 8, 7);

            patriarch.createPicture(anchorLion,
                    workbook.addPicture(byteArrayOutImgLion.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

            patriarch.createPicture(anchorDBZF,
                    workbook.addPicture(byteArrayOutDBZF.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

            if (fixSize > 5) {

                int printFixSize = (fixSize > (i + 2) * 5 ? (i + 2) * 5 : fixSize);

                for (int j = 5 * (i + 1); j < printFixSize; j++) {

                    TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                    List<TbFixShare> tbFixShareList = tbFixShareService
                            .findTbFixShareListByTbFixEntrustContentId(content.getId());

                    String fixPersons = "";

                    if (null != tbFixShareList && tbFixShareList.size() > 0) {

                        for (TbFixShare tbFixShare : tbFixShareList) {

                            if (null != tbFixShare.getTmUser()) {

                                TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                                fixPersons += (tmUser.getUserRealName() == null
                                        || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                                : tmUser.getUserRealName())
                                        + " ";
                            }

                        }
                    }

                    row = sheetClone.getRow(31 + p * 6);

                    cell = row.getCell(0);

                    cell.setCellStyle(style);

                    cell.setCellValue(content.getStationCode() + "    " + content.getStationName());

                    cell = row.getCell(20);

                    cell.setCellStyle(style);

                    cell.setCellValue(content.getXmlx() == null ? "" : content.getXmlx());

                    cell = row.getCell(26);

                    cell.setCellStyle(style);

                    cell.setCellValue(fixPersons);

                    p++;

                }

            }

            if (partAllSize > 13) {

                int prinPartSize = (partAllSize > (i + 2) * 13 ? (i + 2) * 13 : partAllSize);

                for (int j = 13 * (i + 1); j < prinPartSize; j++) {

                    TbMaintianVo t = partAll.get(j);

                    row = sheetClone.getRow(31 + k * 3);

                    cell = row.getCell(32);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getPartName());

                    cell = row.getCell(40);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getPartQuantity());

                    cell = row.getCell(44);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(t.getPrice())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    k++;

                }
            }

        }

        if (fixSize > 0) {

            int printFixSize = (fixSize > 5 ? 5 : fixSize);

            for (int j = 0; j < printFixSize; j++) {

                TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                List<TbFixShare> tbFixShareList = tbFixShareService
                        .findTbFixShareListByTbFixEntrustContentId(content.getId());

                String fixPersons = "";

                if (null != tbFixShareList && tbFixShareList.size() > 0) {

                    for (TbFixShare tbFixShare : tbFixShareList) {

                        if (null != tbFixShare.getTmUser()) {

                            TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                            fixPersons += (tmUser.getUserRealName() == null
                                    || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                            : tmUser.getUserRealName())
                                    + " ";
                        }

                    }
                }

                row = sheet.getRow(31 + j * 6);

                cell = row.getCell(0);

                cell.setCellStyle(style);

                cell.setCellValue(content.getStationCode() + "    " + content.getStationName());

                cell = row.getCell(20);

                cell.setCellStyle(style);

                cell.setCellValue(content.getXmlx() == null ? "" : content.getXmlx());

                cell = row.getCell(26);

                cell.setCellStyle(style);

                cell.setCellValue(fixPersons);

            }

        }

        if (partAllSize > 0) {

            int printPartSize = (partAllSize > 13 ? 13 : partAllSize);

            for (int j = 0; j < printPartSize; j++) {

                TbMaintianVo t = partAll.get(j);

                row = sheet.getRow(31 + j * 3);

                cell = row.getCell(32);

                cell.setCellStyle(style);

                cell.setCellValue(t.getPartName());

                cell = row.getCell(40);

                cell.setCellStyle(style);

                cell.setCellValue(t.getPartQuantity());

                cell = row.getCell(44);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(t.getPrice())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

            }

        }

        workbook.write(os);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.selfsoft.business.service.impl.TbFixEntrustServiceImpl.java

public void printTbFixEntrustTemplateXTL(OutputStream os, String tpl, Long tbFixEntrustId) {

    try {/*from  w w w.j  a  v a  2s .  c  o  m*/

        TbFixEntrust tbFixEntrust = this.findById(tbFixEntrustId);

        TbCustomer tbCustomer = tbCustomerService.findById(tbFixEntrust.getTbCustomer().getId());

        TbCarInfo tbCarInfo = tbCarInfoService.findById(tbFixEntrust.getTbCarInfo().getId());

        List<TbFixEntrustContent> tbFixEntrustContentList = this.composeContent(
                tbFixEntrustContentService.findTbFixEnTrustContentListByTbFixEntrustId(tbFixEntrustId));

        // ??
        TmCompany tmCompany = tmCompanyService.acquireUniqueTmCompany();

        /*
         * List<TbMaintainPartContent> maintainList =
         * tbMaintainPartContentService
         * .getViewEntrustMaintianContent(tbFixEntrustId);
         * 
         * 
         * List<TmStockOutDetVo> solePartList =
         * tmStockOutService.getSellByEntrustCode
         * (tbFixEntrust.getEntrustCode());
         */

        /**
         * ??
         */
        List<TbMaintianVo> maintianvos = tbMaintainPartContentService
                .getTbMaintianDetailVosByEntrustId(tbFixEntrust.getId(), Constants.BALANCE_ALL);

        /**
         * ?
         */
        List<TmStockOutDetVo> tmStockOutDetVos = tmStockOutService
                .getSellDetailByEntrustCode(tbFixEntrust.getEntrustCode(), Constants.BALANCE_ALL);

        /**
         * ??
         */

        List<TbMaintianVo> partAll = new ArrayList<TbMaintianVo>();

        if (null != maintianvos && maintianvos.size() > 0) {

            for (TbMaintianVo tbMaintianVo : maintianvos) {

                partAll.add(tbMaintianVo);

            }
        }

        if (null != tmStockOutDetVos && tmStockOutDetVos.size() > 0) {

            for (TmStockOutDetVo tmStockOutDetVo : tmStockOutDetVos) {

                TbMaintianVo tbMaintianVo = new TbMaintianVo();

                tbMaintianVo.setPartId(tmStockOutDetVo.getPartinfoId());

                tbMaintianVo.setHouseName(tmStockOutDetVo.getHouseName());

                tbMaintianVo.setPartCode(tmStockOutDetVo.getPartCode());

                tbMaintianVo.setPartName(tmStockOutDetVo.getPartName());

                tbMaintianVo.setUnitName(tmStockOutDetVo.getUnitName());

                tbMaintianVo.setPrice(tmStockOutDetVo.getPrice());

                tbMaintianVo.setPartQuantity(tmStockOutDetVo.getQuantity());

                tbMaintianVo.setTotal(tmStockOutDetVo.getTotal());

                tbMaintianVo.setIsFree(tmStockOutDetVo.getIsFree());

                tbMaintianVo.setProjectType(tmStockOutDetVo.getProjectType());

                tbMaintianVo.setZl(tmStockOutDetVo.getZl());

                tbMaintianVo.setXmlx(tmStockOutDetVo.getXmlx());

                partAll.add(tbMaintianVo);
            }
        }

        partAll = this.composePart(partAll);

        int fixSize = (tbFixEntrustContentList == null ? 0 : tbFixEntrustContentList.size());

        int maintainSize = (maintianvos == null ? 0 : maintianvos.size());

        int solePartSize = (tmStockOutDetVos == null ? 0 : tmStockOutDetVos.size());

        int partAllSize = (partAll == null ? 0 : partAll.size());

        HSSFWorkbook workbook = new HSSFWorkbook(this.getClass().getResourceAsStream(tpl));

        HSSFSheet sheet = workbook.getSheetAt(0);

        HSSFCellStyle style = workbook.createCellStyle();

        style.setWrapText(true);

        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFFont font = workbook.createFont();

        font.setFontName("");

        font.setFontHeightInPoints((short) 9);

        style.setFont(font);

        HSSFRow row = null;

        HSSFCell cell = null;

        row = sheet.getRow(3);

        cell = row.getCell(0);

        cell.setCellValue("  ??:" + "?" + " (?:70691M)  ?:"
                + tmCompany.getCompanyAddress() + "  ?: "
                + (tmCompany.getCompanyPhone() == null ? "" : tmCompany.getCompanyPhone()));

        row = sheet.getRow(0);

        cell = row.getCell(31);

        cell.setCellStyle(style);

        cell.setCellValue("*?:" + tbCustomer.getCustomerCode() + "\n" + "??:");

        row = sheet.getRow(6);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getCustomerName() == null ? "" : tbCustomer.getCustomerName());

        row = sheet.getRow(6);

        cell = row.getCell(24);

        cell.setCellStyle(style);

        cell.setCellValue(tbCustomer.getAddress() == null ? "" : tbCustomer.getAddress());

        row = sheet.getRow(6);

        cell = row.getCell(41);

        cell.setCellValue(tbCustomer.getContractPerson() == null ? "" : tbCustomer.getContractPerson());

        row = sheet.getRow(8);

        cell = row.getCell(41);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(8);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(12);

        cell = row.getCell(0);

        String entrustCode = tbFixEntrust.getEntrustCode();

        String[] es = entrustCode.split("-");

        String newCode = "RO" + es[0].substring(2, 6) + es[1];

        cell.setCellValue(/* tbFixEntrust.getEntrustCode() */newCode);

        row = sheet.getRow(12);

        cell = row.getCell(9);

        cell.setCellValue(CommonMethod.parseDateToString(tbFixEntrust.getFixDate(), "yyyy-MM-dd HH:mm"));

        row = sheet.getRow(12);

        cell = row.getCell(18);

        cell.setCellValue(tbCarInfo.getLicenseCode());

        row = sheet.getRow(12);

        cell = row.getCell(26);

        cell.setCellStyle(style);

        cell.setCellValue(tbCarInfo.getTmCarModelType().getModelName());

        row = sheet.getRow(12);

        cell = row.getCell(34);

        cell.setCellValue(tbCarInfo.getChassisCode());

        // ?
        row = sheet.getRow(16);

        cell = row.getCell(0);

        cell.setCellValue("");

        row = sheet.getRow(16);

        cell = row.getCell(9);

        cell.setCellValue(CommonMethod.parseDateToString(tbCarInfo.getPurchaseDate(), "yyyy-MM-dd"));

        // ?
        row = sheet.getRow(16);

        cell = row.getCell(18);

        cell.setCellValue("");

        row = sheet.getRow(16);

        cell = row.getCell(26);

        cell.setCellValue(tbFixEntrust.getEnterStationKilo() == null ? ""
                : new BigDecimal(tbFixEntrust.getEnterStationKilo().toString())
                        .divide(new BigDecimal("1.00"), 0, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(16);

        cell = row.getCell(34);

        cell.setCellValue(CommonMethod.parseDateToString(tbFixEntrust.getEstimateDate(), "yyyy-MM-dd HH:mm"));

        Double fixCount = tbFixEntrustContentService.countTbFixEnTrustContentByTbFixEntrustId(tbFixEntrustId);

        row = sheet.getRow(75);

        cell = row.getCell(30);

        cell.setCellValue(new BigDecimal(fixCount).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP)
                .toString());

        BigDecimal partPriceAll = new BigDecimal("0.00");

        if (partAllSize > 0) {

            for (int i = 0; i < partAllSize; i++) {

                TbMaintianVo tbMaintianVo = partAll.get(i);

                BigDecimal total = new BigDecimal(tbMaintianVo.getPrice())
                        .multiply(new BigDecimal(tbMaintianVo.getPartQuantity()));

                partPriceAll = partPriceAll.add(total);

            }

        }

        row = sheet.getRow(77);

        cell = row.getCell(30);

        cell.setCellValue(partPriceAll.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        BigDecimal total = new BigDecimal(fixCount).add(partPriceAll).divide(new BigDecimal("1.00"), 2,
                BigDecimal.ROUND_HALF_UP);

        row = sheet.getRow(81);

        cell = row.getCell(30);

        cell.setCellValue(total.toString());

        row = sheet.getRow(79);

        cell = row.getCell(30);

        cell.setCellValue("0.00");

        ByteArrayOutputStream byteArrayOutImgLion = new ByteArrayOutputStream();

        ByteArrayOutputStream byteArrayOutDBZF = new ByteArrayOutputStream();

        BufferedImage bufferImgLion = ImageIO.read(this.getClass().getResourceAsStream("/xtl.png"));

        BufferedImage bufferImgDFBZ = ImageIO.read(this.getClass().getResourceAsStream("/xtl_logo.png"));

        ImageIO.write(bufferImgLion, "png", byteArrayOutImgLion);

        ImageIO.write(bufferImgDFBZ, "png", byteArrayOutDBZF);

        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

        HSSFClientAnchor anchorLion = new HSSFClientAnchor(0, 0, 1023, 200, (short) 40, 0, (short) 47, 2);

        HSSFClientAnchor anchorDBZF = new HSSFClientAnchor(0, 0, 1023, 100, (short) 0, 0, (short) 3, 3);

        patriarch.createPicture(anchorLion,
                workbook.addPicture(byteArrayOutImgLion.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

        patriarch.createPicture(anchorDBZF,
                workbook.addPicture(byteArrayOutDBZF.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

        int page = 1;

        if (fixSize / 8 >= (maintainSize + solePartSize) / 12) {

            page = fixSize / 8;

        }

        else {

            page = (maintainSize + solePartSize) / 12;

        }

        for (int i = 0; i < page; i++) {

            int p = 0;

            int k = 0;

            HSSFSheet sheetClone = workbook.cloneSheet(0);

            byteArrayOutImgLion = new ByteArrayOutputStream();

            byteArrayOutDBZF = new ByteArrayOutputStream();

            bufferImgLion = ImageIO.read(this.getClass().getResourceAsStream("/xtl.png"));

            bufferImgDFBZ = ImageIO.read(this.getClass().getResourceAsStream("/xtl_logo.png"));

            ImageIO.write(bufferImgLion, "png", byteArrayOutImgLion);

            ImageIO.write(bufferImgDFBZ, "png", byteArrayOutDBZF);

            patriarch = sheetClone.createDrawingPatriarch();

            anchorLion = new HSSFClientAnchor(0, 0, 1023, 200, (short) 40, 0, (short) 47, 2);

            anchorDBZF = new HSSFClientAnchor(0, 0, 1023, 200, (short) 0, 0, (short) 3, 3);

            patriarch.createPicture(anchorLion,
                    workbook.addPicture(byteArrayOutImgLion.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

            patriarch.createPicture(anchorDBZF,
                    workbook.addPicture(byteArrayOutDBZF.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

            if (fixSize > 8) {

                int printFixSize = (fixSize > (i + 2) * 8 ? (i + 2) * 8 : fixSize);

                for (int j = 8 * (i + 1); j < printFixSize; j++) {

                    TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                    List<TbFixShare> tbFixShareList = tbFixShareService
                            .findTbFixShareListByTbFixEntrustContentId(content.getId());

                    String fixPersons = "";

                    String workTypes = "";

                    if (null != tbFixShareList && tbFixShareList.size() > 0) {

                        for (TbFixShare tbFixShare : tbFixShareList) {

                            if (null != tbFixShare.getTmUser()) {

                                TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                                fixPersons += (tmUser.getUserRealName() == null
                                        || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                                : tmUser.getUserRealName())
                                        + " ";

                                workTypes += (tmUser.getTmWorkType() == null ? ""
                                        : tmUser.getTmWorkType().getWorkName()) + " ";
                            }

                        }
                    }

                    row = sheetClone.getRow(23 + p * 2);

                    cell = row.getCell(2);

                    cell.setCellStyle(style);

                    cell.setCellValue(content.getStationName());

                    cell = row.getCell(20);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(content.getFixHour())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(24);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(content.getWorkingHourPrice())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(28);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(content.getFixHourAll())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(34);

                    cell.setCellStyle(style);

                    cell.setCellValue(content.getProjectType() == null ? "" : content.getProjectType());

                    cell = row.getCell(40);

                    cell.setCellStyle(style);

                    cell.setCellValue(workTypes);

                    cell = row.getCell(45);

                    cell.setCellStyle(style);

                    cell.setCellValue(fixPersons);

                    p++;

                }

            }

            if (partAllSize > 12) {

                int prinPartSize = (partAllSize > (i + 2) * 12 ? (i + 2) * 12 : partAllSize);

                for (int j = 12 * (i + 1); j < prinPartSize; j++) {

                    TbMaintianVo t = partAll.get(j);

                    row = sheetClone.getRow(41 + k * 2);

                    cell = row.getCell(2);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getPartCode());

                    cell = row.getCell(11);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getPartName());

                    cell = row.getCell(20);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(t.getPartQuantity())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(24);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(t.getPrice())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(28);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(t.getPartQuantity()).multiply(new BigDecimal(t.getPrice()))
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(34);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getProjectType() == null ? "" : t.getProjectType());

                    // 
                    cell = row.getCell(40);

                    cell.setCellStyle(style);

                    cell.setCellValue("");

                    k++;

                }
            }

        }

        if (fixSize > 0) {

            int printFixSize = (fixSize > 8 ? 8 : fixSize);

            for (int j = 0; j < printFixSize; j++) {

                TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                List<TbFixShare> tbFixShareList = tbFixShareService
                        .findTbFixShareListByTbFixEntrustContentId(content.getId());

                String fixPersons = "";

                String workTypes = "";

                if (null != tbFixShareList && tbFixShareList.size() > 0) {

                    for (TbFixShare tbFixShare : tbFixShareList) {

                        if (null != tbFixShare.getTmUser()) {

                            TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                            fixPersons += (tmUser.getUserRealName() == null
                                    || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                            : tmUser.getUserRealName())
                                    + " ";

                            workTypes += (tmUser.getTmWorkType() == null ? ""
                                    : tmUser.getTmWorkType().getWorkName()) + " ";
                        }

                    }
                }

                row = sheet.getRow(23 + j * 2);

                cell = row.getCell(2);

                cell.setCellStyle(style);

                cell.setCellValue(content.getStationName());

                cell = row.getCell(20);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(content.getFixHour())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(24);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(content.getWorkingHourPrice())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(28);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(content.getFixHourAll())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(34);

                cell.setCellStyle(style);

                cell.setCellValue(content.getProjectType() == null ? "" : content.getProjectType());

                cell = row.getCell(40);

                cell.setCellStyle(style);

                cell.setCellValue(workTypes);

                cell = row.getCell(45);

                cell.setCellStyle(style);

                cell.setCellValue(fixPersons);

            }

        }

        if (partAllSize > 0) {

            int printPartSize = (partAllSize > 12 ? 12 : partAllSize);

            for (int j = 0; j < printPartSize; j++) {

                TbMaintianVo t = partAll.get(j);

                row = sheet.getRow(41 + j * 2);

                cell = row.getCell(2);

                cell.setCellStyle(style);

                cell.setCellValue(t.getPartCode());

                cell = row.getCell(11);

                cell.setCellStyle(style);

                cell.setCellValue(t.getPartName());

                cell = row.getCell(20);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(t.getPartQuantity())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(24);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(t.getPrice())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(28);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(t.getPartQuantity()).multiply(new BigDecimal(t.getPrice()))
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(34);

                cell.setCellStyle(style);

                cell.setCellValue(t.getProjectType() == null ? "" : t.getProjectType());

                // 
                cell = row.getCell(40);

                cell.setCellStyle(style);

                cell.setCellValue("");

            }

        }

        workbook.write(os);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.util.poi.ExcelView.java

License:Open Source License

/**
 * ?Excel/*from  w w  w .ja v  a2s  .c  o  m*/
 * 
 * @param model
 *            ?
 * @param workbook
 *            workbook
 * @param request
 *            request
 * @param response
 *            response
 */
public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Assert.notEmpty(properties);
    HSSFSheet sheet;
    if (StringUtils.isNotEmpty(sheetName)) {
        sheet = workbook.createSheet(sheetName);
    } else {
        sheet = workbook.createSheet();
    }
    int rowNumber = 0;
    if (titles != null && titles.length > 0) {
        HSSFRow header = sheet.createRow(rowNumber);
        header.setHeight((short) 400);
        for (int i = 0; i < properties.length; i++) {
            HSSFCell cell = header.createCell(i);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 11);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            if (i == 0) {
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                HSSFComment comment = patriarch
                        .createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 4, 4));
                //comment.setString(new HSSFRichTextString("P" + "o" + "w" + "e" + "r" + "e" + "d" + " " + "B" + "y" + " " + "S" + "H" + "O" + "P" + "+" + "+"));
                cell.setCellComment(comment);
            }
            if (titles.length > i && titles[i] != null) {
                cell.setCellValue(titles[i]);
            } else {
                cell.setCellValue(properties[i]);
            }
            if (widths != null && widths.length > i && widths[i] != null) {
                sheet.setColumnWidth(i, widths[i]);
            } else {
                sheet.autoSizeColumn(i);
            }
        }
        rowNumber++;
    }
    if (data != null) {
        for (Object item : data) {
            HSSFRow row = sheet.createRow(rowNumber);
            for (int i = 0; i < properties.length; i++) {
                HSSFCell cell = row.createCell(i);
                if (converters != null && converters.length > i && converters[i] != null) {
                    Class<?> clazz = PropertyUtils.getPropertyType(item, properties[i]);
                    ConvertUtils.register(converters[i], clazz);
                    /*   Map<String, Object> map=toHashMap(item);
                       cell.setCellValue(map.get(properties[i]).toString());*/
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                    ConvertUtils.deregister(clazz);
                    if (clazz.equals(Date.class)) {
                        DateConverter dateConverter = new DateConverter();
                        dateConverter.setPattern(DEFAULT_DATE_PATTERN);
                        ConvertUtils.register(dateConverter, Date.class);
                    }
                } else {
                    /*Map<String, Object> map=toHashMap(item);
                    cell.setCellValue(map.get(properties[i]).toString());*/
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                }
                if (rowNumber == 0 || rowNumber == 1) {
                    if (widths != null && widths.length > i && widths[i] != null) {
                        sheet.setColumnWidth(i, widths[i]);
                    } else {
                        sheet.autoSizeColumn(i);
                    }
                }
            }
            rowNumber++;
        }
    }
    if (contents != null && contents.length > 0) {
        rowNumber++;
        for (String content : contents) {
            HSSFRow row = sheet.createRow(rowNumber);
            HSSFCell cell = row.createCell(0);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.GREY_50_PERCENT.index);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(content);
            rowNumber++;
        }
    }
    response.setContentType("application/force-download");
    if (StringUtils.isNotEmpty(filename)) {
        response.setHeader("Content-disposition",
                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
    } else {
        response.setHeader("Content-disposition", "attachment");
    }
}

From source file:controller.VisitasController.java

public void exportExcel() {

    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XLS", "*.xls"));
    File f = fileChooser.showSaveDialog(null);

    try {/*from w ww. j ava2s  . co m*/
        String filename = f.getAbsolutePath();
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("FirstSheet");
        int bool = 1;

        InputStream inputStream = getClass().getResourceAsStream("/images/excel-logo.jpg");

        byte[] imageBytes = IOUtils.toByteArray(inputStream);

        int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG);

        inputStream.close();

        CreationHelper helper = workbook.getCreationHelper();

        Drawing drawing = sheet.createDrawingPatriarch();

        ClientAnchor anchor = helper.createClientAnchor();

        anchor.setCol1(1);
        anchor.setRow1(0);

        Picture pict = drawing.createPicture(anchor, pictureureIdx);

        HSSFRow rowhead = sheet.createRow(8);

        rowhead.createCell(0);
        rowhead.createCell(1).setCellValue("Cedula");
        rowhead.createCell(2).setCellValue("Cliente");
        rowhead.createCell(3).setCellValue("Contrato");
        rowhead.createCell(4).setCellValue("Plan");
        rowhead.createCell(5).setCellValue("Fecha");
        rowhead.createCell(6).setCellValue("Hora");
        rowhead.createCell(7).setCellValue("Invitados");
        makeRowBold(workbook, rowhead);

        for (int i = 0; i < table.getItems().size(); i++) {
            HSSFRow row = sheet.createRow(i + 9);
            Asistencia a = (Asistencia) table.getItems().get(i);
            row.createCell(1).setCellValue(a.getCedula());
            row.createCell(2).setCellValue(a.getNombre());
            row.createCell(3).setCellValue(a.getContrato());
            row.createCell(4).setCellValue(a.getPlan());
            row.createCell(5).setCellValue(a.getFecha());
            row.createCell(6).setCellValue(a.getHora());
            row.createCell(7).setCellValue(Integer.parseInt(a.getInvitados()));
            centerRow(workbook, row);
        }
        autoSizeColumns(workbook);
        pict.resize();
        FileOutputStream fileOut = new FileOutputStream(filename);
        workbook.write(fileOut);
        fileOut.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:gov.guilin.ExcelView.java

License:Open Source License

/**
 * ?Excel/*from   w  w  w. ja v  a2s  .c  om*/
 * 
 * @param model
 *            ?
 * @param workbook
 *            workbook
 * @param request
 *            request
 * @param response
 *            response
 */
public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Assert.notEmpty(properties);
    HSSFSheet sheet;
    if (StringUtils.isNotEmpty(sheetName)) {
        sheet = workbook.createSheet(sheetName);
    } else {
        sheet = workbook.createSheet();
    }
    int rowNumber = 0;
    if (titles != null && titles.length > 0) {
        HSSFRow header = sheet.createRow(rowNumber);
        header.setHeight((short) 400);
        for (int i = 0; i < properties.length; i++) {
            HSSFCell cell = header.createCell(i);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 11);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            if (i == 0) {
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                HSSFComment comment = patriarch
                        .createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 4, 4));
                comment.setString(new HSSFRichTextString("P" + "o" + "w" + "e" + "r" + "e" + "d" + " " + "B"
                        + "y" + " " + "S" + "H" + "O" + "P" + "+" + "+"));
                cell.setCellComment(comment);
            }
            if (titles.length > i && titles[i] != null) {
                cell.setCellValue(titles[i]);
            } else {
                cell.setCellValue(properties[i]);
            }
            if (widths != null && widths.length > i && widths[i] != null) {
                sheet.setColumnWidth(i, widths[i]);
            } else {
                sheet.autoSizeColumn(i);
            }
        }
        rowNumber++;
    }
    if (data != null) {
        for (Object item : data) {
            HSSFRow row = sheet.createRow(rowNumber);
            for (int i = 0; i < properties.length; i++) {
                HSSFCell cell = row.createCell(i);
                if (converters != null && converters.length > i && converters[i] != null) {
                    Class<?> clazz = PropertyUtils.getPropertyType(item, properties[i]);
                    ConvertUtils.register(converters[i], clazz);
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                    ConvertUtils.deregister(clazz);
                    if (clazz.equals(Date.class)) {
                        DateConverter dateConverter = new DateConverter();
                        dateConverter.setPattern(DEFAULT_DATE_PATTERN);
                        ConvertUtils.register(dateConverter, Date.class);
                    }
                } else {
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                }
                if (rowNumber == 0 || rowNumber == 1) {
                    if (widths != null && widths.length > i && widths[i] != null) {
                        sheet.setColumnWidth(i, widths[i]);
                    } else {
                        sheet.autoSizeColumn(i);
                    }
                }
            }
            rowNumber++;
        }
    }
    if (contents != null && contents.length > 0) {
        rowNumber++;
        for (String content : contents) {
            HSSFRow row = sheet.createRow(rowNumber);
            HSSFCell cell = row.createCell(0);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.GREY_50_PERCENT.index);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(content);
            rowNumber++;
        }
    }
    response.setContentType("application/force-download");
    if (StringUtils.isNotEmpty(filename)) {
        response.setHeader("Content-disposition",
                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
    } else {
        response.setHeader("Content-disposition", "attachment");
    }
}

From source file:gov.nih.nci.cananolab.service.publication.impl.PublicationExporter.java

License:BSD License

public static void exportDetail(PublicationBean aPub, OutputStream out) throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("detailSheet");
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    int startRow = 0;
    setDetailSheet(aPub, wb, sheet, patriarch, startRow);
    wb.write(out);/*from w  w  w. ja va  2  s .c  om*/
    if (out != null) {
        out.flush();
        out.close();
    }
}

From source file:gov.nih.nci.cananolab.service.sample.impl.CharacterizationExporter.java

License:BSD License

/**
 * Output Sample Characterization Summary report (==>
 * bodyCharacterizationSummaryPrintViewTable.jsp)
 *
 * @param summaryBean//w  w  w  . ja va 2 s .c  o m
 * @param wb
 * @throws IOException
 */
private static void outputSummarySheet(List<String> charTypes, CharacterizationSummaryViewBean summaryBean,
        String downloadURL, HSSFWorkbook wb) throws IOException {
    HSSFFont headerFont = wb.createFont();
    headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    HSSFCellStyle headerStyle = wb.createCellStyle();
    headerStyle.setFont(headerFont);

    HSSFCellStyle hlinkStyle = wb.createCellStyle();
    HSSFFont hlinkFont = wb.createFont();
    hlinkFont.setUnderline(HSSFFont.U_SINGLE);
    hlinkFont.setColor(HSSFColor.BLUE.index);
    hlinkStyle.setFont(hlinkFont);

    int charCount = 1;
    Map<String, SortedSet<CharacterizationBean>> charBeanMap = summaryBean.getType2Characterizations();
    for (String type : charTypes) {
        // Output data of report
        SortedSet<CharacterizationBean> charBeans = charBeanMap.get(type);
        if (charBeans != null && !charBeans.isEmpty()) {
            for (CharacterizationBean charBean : charBeans) {
                int rowIndex = 0;

                // Create one work sheet for each Characterization.
                HSSFSheet sheet = wb.createSheet(charCount++ + "." + charBean.getCharacterizationName());
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

                // 1. Output Characterization type at (0, 0).
                rowIndex = outputHeader(charBean, sheet, headerStyle, rowIndex);
                // 2. Output Assay Type (2, 0).
                rowIndex = outputAssayType(charBean, sheet, headerStyle, rowIndex);
                // 3. Output POC at (3, 0).
                rowIndex = outputPOC(charBean, sheet, headerStyle, rowIndex);
                // 4. Output Characterization Date at (4, 0).
                rowIndex = outputCharDate(charBean, sheet, headerStyle, rowIndex);
                // 5. Output Protocol at (5, 0).
                rowIndex = outputProtocol(charBean, sheet, headerStyle, rowIndex);
                // 6. Output Properties at (6, 0).
                rowIndex = outputProperties(charBean, sheet, headerStyle, rowIndex);
                // 7. Output Design Description at (7, 0).
                rowIndex = outputDesignDescription(charBean, sheet, headerStyle, rowIndex);
                // 8. Output Technique and Instruments at (8, 0).
                rowIndex = outputTechInstruments(charBean, sheet, headerStyle, rowIndex);
                // 9. Output Characterization Results at (9, 0).
                rowIndex = outputCharResults(charBean, downloadURL, wb, sheet, headerStyle, hlinkStyle,
                        patriarch, rowIndex);
                // 10.Output Analysis and Conclusion at (10, 0).
                rowIndex = outputConclusion(charBean, sheet, headerStyle, rowIndex);
            }
        }
    }
}

From source file:gov.nih.nci.cananolab.service.sample.impl.CompositionExporter.java

License:BSD License

/**
 * Outputting ChemicalEntities data =>
 * bodyChemicalAssociationSummaryView.jsp
 *
 * @param compBean//from  w  w w .j a  v a2s  .c  om
 * @param sheet
 * @param headerStyle
 * @param rowIndex
 */
private static int outputChemicalEntities(CompositionBean compBean, HSSFWorkbook wb, HSSFCellStyle headerStyle,
        HSSFCellStyle hlinkStyle, int entityCount, String downloadURL) {
    List<ChemicalAssociationBean> nanoList = compBean.getChemicalAssociations();
    if (nanoList != null && !nanoList.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        for (ChemicalAssociationBean nanoEntity : nanoList) {
            if (!StringUtils.isEmpty(nanoEntity.getType())) {
                int rowIndex = 0;
                sb.setLength(0);
                sb.append(entityCount++).append('.').append(nanoEntity.getType());

                // Create one work sheet for each Composition.
                HSSFSheet sheet = wb.createSheet(sb.toString());
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

                // 1. Output Composition type at (0, 0).
                rowIndex = outputHeader(CompositionBean.CHEMICAL_SELECTION, nanoEntity.getType(), sheet,
                        headerStyle, rowIndex);

                // 2. Output Bond Type.
                if (nanoEntity.getAttachment().getId() != null
                        && !StringUtils.isEmpty(nanoEntity.getAttachment().getBondType())) {
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Bond Type");
                    ExportUtils.createCell(row, 1, nanoEntity.getAttachment().getBondType());
                }

                // 3. Output Description.
                if (!StringUtils.isEmpty(nanoEntity.getDescription())) {
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Description");
                    ExportUtils.createCell(row, 1, nanoEntity.getDescription());
                }

                // Output Associated Elements.
                rowIndex++; // Create an empty line as seperator.
                HSSFRow row = sheet.createRow(rowIndex++);
                ExportUtils.createCell(row, 0, headerStyle, "Associated Elements");

                // 4a. Output Associated Element A.
                row = sheet.createRow(rowIndex++);
                sb.setLength(0);
                sb.append(nanoEntity.getAssociatedElementA().getCompositionType());
                sb.append(' ');
                sb.append(nanoEntity.getAssociatedElementA().getEntityDisplayName());
                Long compElementId = nanoEntity.getAssociatedElementA().getComposingElement().getId();
                if (compElementId != null) {
                    sb.append(" composing element of type ");
                    sb.append(nanoEntity.getAssociatedElementA().getComposingElement().getType());
                    sb.append(" (name: ");
                    sb.append(nanoEntity.getAssociatedElementA().getComposingElement().getName());
                    sb.append(')');
                } else {
                    sb.append(" (name: ");
                    sb.append(nanoEntity.getAssociatedElementA().getDomainElement().getName());
                    sb.append(')');
                }
                ExportUtils.createCell(row, 0, sb.toString());

                ExportUtils.createCell(row, 1, "associated with");

                // 4b. Output Associated Element B.
                sb.setLength(0);
                sb.append(nanoEntity.getAssociatedElementB().getCompositionType());
                sb.append(' ');
                sb.append(nanoEntity.getAssociatedElementB().getEntityDisplayName());
                compElementId = nanoEntity.getAssociatedElementB().getComposingElement().getId();
                if (compElementId != null) {
                    sb.append(" composing element of type ");
                    sb.append(nanoEntity.getAssociatedElementB().getComposingElement().getType());
                    sb.append(" (name: ");
                    sb.append(nanoEntity.getAssociatedElementB().getComposingElement().getName());
                    sb.append(')');
                } else {
                    sb.append(" (name: ");
                    sb.append(nanoEntity.getAssociatedElementB().getDomainElement().getName());
                    sb.append(')');
                }
                ExportUtils.createCell(row, 2, sb.toString());
                rowIndex++; // Create an empty line as seperator.

                // 5. Output Files: bodyFileView.jsp
                List<FileBean> fileBeans = nanoEntity.getFiles();
                if (fileBeans != null && !fileBeans.isEmpty()) {
                    rowIndex++; // Create one empty line as separator.
                    row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Files");
                    outputFiles(fileBeans, downloadURL, wb, sheet, headerStyle, hlinkStyle, patriarch,
                            rowIndex);
                }
            }
        }
    }
    return entityCount;
}

From source file:gov.nih.nci.cananolab.service.sample.impl.CompositionExporter.java

License:BSD License

/**
 * Output Composition Files data => bodyCompositionFileSummaryView.jsp
 *
 * @param compBean/*from  w w w  .j  a  v  a2 s.  co m*/
 * @param sheet
 * @param headerStyle
 * @param rowIndex
 */
private static int outputFilesEntities(CompositionBean compBean, HSSFWorkbook wb, HSSFCellStyle headerStyle,
        HSSFCellStyle hlinkStyle, int entityCount, String downloadURL) {
    List<FileBean> nanoList = compBean.getFiles();
    if (nanoList != null && !nanoList.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        for (FileBean nanoEntity : nanoList) {
            if (!StringUtils.isEmpty(nanoEntity.getDomainFile().getType())) {
                int rowIndex = 0;
                sb.setLength(0);
                sb.append(entityCount++).append('.').append(nanoEntity.getDomainFile().getType());

                // Create one work sheet for each Composition File.
                HSSFSheet sheet = wb.createSheet(sb.toString());
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

                // 1. Output Composition type at (0, 0).
                HSSFRow row = sheet.createRow(rowIndex++);
                ExportUtils.createCell(row, 0, headerStyle, CompositionBean.FILE_SELECTION);
                rowIndex++; // Create one empty line as separator.

                // 2. Output File info, one File per sheet.
                outputFile(nanoEntity, downloadURL, wb, sheet, headerStyle, hlinkStyle, patriarch, rowIndex);
            }
        }
    }
    return entityCount;
}

From source file:gov.nih.nci.cananolab.service.sample.impl.CompositionExporter.java

License:BSD License

/**
 * Output FunctionalEntities data =>/* ww  w  .j  av a  2s. c  o m*/
 * bodyFunctionalizingEntitySummaryView.jsp
 *
 * @param compBean
 * @param sheet
 * @param headerStyle
 * @param rowIndex
 */
private static int outputFunctionalEntities(CompositionBean compBean, HSSFWorkbook wb,
        HSSFCellStyle headerStyle, HSSFCellStyle hlinkStyle, int entityCount, String downloadURL) {
    List<FunctionalizingEntityBean> nanoList = compBean.getFunctionalizingEntities();
    if (nanoList != null && !nanoList.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        for (FunctionalizingEntityBean nanoEntity : nanoList) {
            if (!StringUtils.isEmpty(nanoEntity.getType())) {
                int rowIndex = 0;
                sb.setLength(0);
                sb.append(entityCount++).append('.').append(nanoEntity.getType());

                // Create 1 work sheet for each Functional Entity.
                HSSFSheet sheet = wb.createSheet(sb.toString());
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

                // 1. Output Composition type at (0, 0).
                rowIndex = outputHeader(CompositionBean.FUNCTIONALIZING_SELECTION, nanoEntity.getType(), sheet,
                        headerStyle, rowIndex);

                // 2. Output Name.
                if (!StringUtils.isEmpty(nanoEntity.getName())) {
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Name");
                    ExportUtils.createCell(row, 1, nanoEntity.getName());
                }

                // 3. Output PubChem.
                if (nanoEntity.getDomainEntity().getPubChemId() != null) {
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "PubChem ID");
                    Long pubChemId = nanoEntity.getDomainEntity().getPubChemId();
                    String pubChemDs = nanoEntity.getDomainEntity().getPubChemDataSourceName();
                    sb.setLength(0);
                    sb.append(pubChemId).append(' ');
                    sb.append('(').append(pubChemDs).append(')');
                    ExportUtils.createCell(row, 1, hlinkStyle, sb.toString(),
                            CompositionBean.getPubChemURL(pubChemDs, pubChemId));
                }

                // Output Amount.
                if (nanoEntity.getValue() != null) {
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Amount");
                    sb.setLength(0);
                    sb.append(nanoEntity.getValue()).append(' ');
                    sb.append(nanoEntity.getValueUnit());
                    ExportUtils.createCell(row, 1, sb.toString());
                }

                // 5. Output Molecular Formula.
                if (!StringUtils.isEmpty(nanoEntity.getMolecularFormulaDisplayName())) {
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Molecular Formula");
                    ExportUtils.createCell(row, 1, nanoEntity.getMolecularFormulaDisplayName());
                }

                // 6. Output Properties: 3x) "functionalizingEntity/*.jsp"
                if (nanoEntity.isWithProperties()) {
                    rowIndex++; // Create one empty line as separator.
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Properties");

                    FunctionalizingEntity domainEntity = nanoEntity.getDomainEntity();
                    if (domainEntity instanceof Antibody) {
                        rowIndex = outputFuncProperties((Antibody) domainEntity, sheet, headerStyle, rowIndex);
                    } else if (domainEntity instanceof SmallMolecule) {
                        rowIndex = outputFuncProperties((SmallMolecule) domainEntity, sheet, headerStyle,
                                rowIndex);
                    } else if (domainEntity instanceof gov.nih.nci.cananolab.domain.agentmaterial.Biopolymer) {
                        rowIndex = outputFuncProperties(
                                (gov.nih.nci.cananolab.domain.agentmaterial.Biopolymer) domainEntity, sheet,
                                headerStyle, rowIndex);
                    }
                    rowIndex++; // Create one empty line as separator.
                }

                // 7. Output Functions: bodyFunctionView.jsp
                List<FunctionBean> functions = nanoEntity.getFunctions();
                if (functions != null && !functions.isEmpty()) {
                    if (!nanoEntity.isWithProperties()) {
                        rowIndex++; // Create one empty line as separator.
                    }
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Function(s)");

                    // 6.1. output Functions table header.
                    int colIndex = 0;
                    row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, colIndex++, headerStyle, "Type");
                    if (nanoEntity.isWithImagingFunction()) {
                        ExportUtils.createCell(row, colIndex++, headerStyle, "Image Modality");
                    }
                    if (nanoEntity.isWithTargetingFunction()) {
                        ExportUtils.createCell(row, colIndex++, headerStyle, "Targets");
                    }
                    ExportUtils.createCell(row, colIndex, headerStyle, "Description");

                    // 6.2. output Functions table data.
                    for (FunctionBean function : functions) {
                        colIndex = 0;
                        row = sheet.createRow(rowIndex++);

                        ExportUtils.createCell(row, colIndex++, function.getType());
                        if (nanoEntity.isWithImagingFunction()) {
                            if (!StringUtils.isEmpty(function.getImagingFunction().getModality())) {
                                ExportUtils.createCell(row, colIndex++,
                                        function.getImagingFunction().getModality());
                            }
                        }
                        if (nanoEntity.isWithTargetingFunction()) {
                            String[] displayNames = function.getTargetDisplayNames();
                            if (displayNames != null && displayNames.length > 0) {
                                sb.setLength(0);
                                for (String name : displayNames) {
                                    sb.append(',').append(' ').append(name);
                                }
                                ExportUtils.createCell(row, colIndex++, sb.substring(2));
                            }
                        }
                        ExportUtils.createCell(row, colIndex, function.getDescription());
                    }
                    rowIndex++; // Create one empty line as separator.
                } // 7. End of Output Functions: bodyFunctionView.jsp

                // 8. Output Activation Method.
                if (!StringUtils.isEmpty(nanoEntity.getActivationMethodDisplayName())) {
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Activation Method");
                    ExportUtils.createCell(row, 1, nanoEntity.getActivationMethodDisplayName());
                }

                // 9. Output Description.
                if (!StringUtils.isEmpty(nanoEntity.getDescription())) {
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Description");
                    ExportUtils.createCell(row, 1, nanoEntity.getDescription());
                }

                // 10. Output Files: bodyFileView.jsp
                List<FileBean> fileBeans = nanoEntity.getFiles();
                if (fileBeans != null && !fileBeans.isEmpty()) {
                    rowIndex++; // Create one empty line as separator.
                    HSSFRow row = sheet.createRow(rowIndex++);
                    ExportUtils.createCell(row, 0, headerStyle, "Files");
                    outputFiles(fileBeans, downloadURL, wb, sheet, headerStyle, hlinkStyle, patriarch,
                            rowIndex);
                }
            }
        } // End of iterating nanoList.
    }
    return entityCount;
}