Example usage for org.apache.poi.ss.usermodel Workbook PICTURE_TYPE_PICT

List of usage examples for org.apache.poi.ss.usermodel Workbook PICTURE_TYPE_PICT

Introduction

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

Prototype

int PICTURE_TYPE_PICT

To view the source code for org.apache.poi.ss.usermodel Workbook PICTURE_TYPE_PICT.

Click Source Link

Document

Mac PICT format

Usage

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.functions.SpreadsheetAddImage.java

License:Open Source License

public cfData execute(cfSession _session, List<cfData> parameters) throws cfmRunTimeException {
    cfSpreadSheetData spreadsheet = (cfSpreadSheetData) parameters.get(2);
    String imgPath = parameters.get(1).getString();
    String[] anchor = parameters.get(0).getString().split(",");

    // Check the anchor
    if (anchor.length != 4 && anchor.length != 8)
        throwException(_session, "Invalid Anchor parameter. Must be 4 or 8 comma separated numbers");

    // Determine the file type
    String fileExt = imgPath.substring(imgPath.lastIndexOf(".") + 1).toLowerCase();
    int imgTypeIndex = 0;
    if (fileExt.equals("dib")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_DIB;
    } else if (fileExt.equals("jpg")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_JPEG;
    } else if (fileExt.equals("emf")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_EMF;
    } else if (fileExt.equals("pict")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_PICT;
    } else if (fileExt.equals("png")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_PNG;
    } else if (fileExt.equals("wmf")) {
        imgTypeIndex = Workbook.PICTURE_TYPE_WMF;
    } else//from ww w .jav a  2 s .  c  o m
        throwException(_session, "Unknown file type: " + imgPath);

    // Read the file
    FileInputStream fs = null;
    byte[] fileBuffer = null;
    try {
        fs = new FileInputStream(imgPath);
        fileBuffer = org.apache.poi.util.IOUtils.toByteArray(fs);
    } catch (Exception fe) {
        throwException(_session, "Unable to read file: " + imgPath);
    } finally {
        try {
            fs.close();
        } catch (IOException e) {
        }
    }

    // Add the picture
    int imageIndex = spreadsheet.getWorkBook().addPicture(fileBuffer, imgTypeIndex);

    HSSFClientAnchor clientAnchor = new HSSFClientAnchor();
    if (anchor.length == 4) {
        clientAnchor.setRow1(Integer.valueOf(anchor[0]) - 1);
        clientAnchor.setCol1(Integer.valueOf(anchor[1]) - 1);
        clientAnchor.setRow2(Integer.valueOf(anchor[2]) - 1);
        clientAnchor.setCol2(Integer.valueOf(anchor[3]) - 1);
    } else {
        clientAnchor.setDx1(Integer.valueOf(anchor[0]) - 1);
        clientAnchor.setDy1(Integer.valueOf(anchor[1]) - 1);
        clientAnchor.setDx2(Integer.valueOf(anchor[2]) - 1);
        clientAnchor.setDy2(Integer.valueOf(anchor[3]) - 1);

        clientAnchor.setRow1(Integer.valueOf(anchor[4]) - 1);
        clientAnchor.setCol1(Integer.valueOf(anchor[5]) - 1);
        clientAnchor.setRow2(Integer.valueOf(anchor[6]) - 1);
        clientAnchor.setCol2(Integer.valueOf(anchor[7]) - 1);
    }

    // finalise the image to the sheet
    spreadsheet.getActiveSheet().createDrawingPatriarch().createPicture(clientAnchor, imageIndex);

    return cfBooleanData.TRUE;
}

From source file:tw.edu.chit.struts.action.registration.ReportPrintAction.java

/**
 * ???//from  ww  w.j a v a2s  . c  o  m
 * 
 * @param mapping org.apache.struts.action.ActionMapping object
 * @param form org.apache.struts.action.ActionForm object
 * @param request request javax.servlet.http.HttpServletRequest object
 * @param response response javax.servlet.http.HttpServletResponse object
 * @param sterm 
 */
private void printClassStudentsRegistrationCard(ActionMapping mapping, DynaActionForm form,
        HttpServletRequest request, HttpServletResponse response, String sterm) throws Exception {

    HttpSession session = request.getSession(false);
    MemberManager mm = (MemberManager) getBean(IConstants.MEMBER_MANAGER_BEAN_NAME);
    CourseManager cm = (CourseManager) getBean(IConstants.COURSE_MANAGER_BEAN_NAME);
    ScoreManager sm = (ScoreManager) getBean(IConstants.SCORE_MANAGER_BEAN_NAME);
    ServletContext context = request.getSession().getServletContext();
    String year = cm.getNowBy("School_year"); // ?????
    // String term = form.getString("sterm"); // ??
    List<Clazz> clazzes = sm.findClassBy(new Clazz(processClassInfo(form)),
            getUserCredential(session).getClassInChargeAry(), false);
    if (!clazzes.isEmpty()) {
        List<Student> students = null;
        HSSFSheet sheet = null;
        String departClass = null;
        RegistrationCard rc = null;
        StdImage image = null;
        String studentNo = null;
        int sheetIndex = 0, pictureIndex = 0;
        byte[] bytes = null;
        // CreationHelper helper = null;
        Drawing drawing = null;
        // ClientAnchor anchor = null;
        HSSFClientAnchor anchor1 = null;
        Picture pict = null;
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");

        File templateXLS = new File(context.getRealPath("/WEB-INF/reports/ClassStudentsRegistrationCard.xls"));
        HSSFWorkbook workbook = Toolket.getHSSFWorkbook(templateXLS);
        HSSFFont fontSize18 = workbook.createFont();
        fontSize18.setFontHeightInPoints((short) 18);
        fontSize18.setFontName("Arial Unicode MS");

        HSSFFont fontSize12 = workbook.createFont();
        fontSize12.setFontHeightInPoints((short) 12);
        fontSize12.setFontName("Arial Unicode MS");

        HSSFFont fontSize11 = workbook.createFont();
        fontSize11.setFontHeightInPoints((short) 11);
        fontSize11.setFontName("Arial Unicode MS");

        HSSFFont fontSize8 = workbook.createFont();
        fontSize8.setFontHeightInPoints((short) 8);
        fontSize8.setFontName("Arial Unicode MS");

        for (Clazz clazz : clazzes) {
            if (!Toolket.isNewStudentClass(clazz.getClassNo()))
                continue;

            departClass = clazz.getClassNo();
            students = mm.findStudentsByClassNo(departClass);
            if (!students.isEmpty()) {

                sheetIndex = 0;
                for (Student student : students) {
                    studentNo = student.getStudentNo();
                    sheet = workbook.getSheetAt(sheetIndex);
                    workbook.setSheetName(sheetIndex++, studentNo.toUpperCase());
                    // Header
                    Toolket.setCellValue(workbook, sheet, 1, 0,
                            Toolket.getCellValue(sheet, 1, 0).replaceAll("YEAR", year), fontSize18,
                            HSSFCellStyle.ALIGN_CENTER, false, null, null);
                    Toolket.setCellValue(workbook, sheet, 1, 13,
                            Toolket.getCellValue(sheet, 1, 13).replaceAll("YEAR", year), fontSize18,
                            HSSFCellStyle.ALIGN_CENTER, false, null, null);

                    // Columns
                    Toolket.setCellValue(workbook, sheet, 2, 1, student.getStudentNo(), fontSize12,
                            HSSFCellStyle.ALIGN_LEFT, true, null, null);
                    Toolket.setCellValue(workbook, sheet, 2, 4, student.getStudentName(), fontSize12,
                            HSSFCellStyle.ALIGN_LEFT, true, null, null);
                    Toolket.setCellValue(workbook, sheet, 2, 8,
                            Toolket.getClassFullName(student.getDepartClass()), fontSize12,
                            HSSFCellStyle.ALIGN_LEFT, true, null, null);
                    Toolket.setCellValue(workbook, sheet, 3, 1,
                            StringUtils.isBlank(student.getStudentEname()) ? "" : student.getStudentEname(),
                            fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                    Toolket.setCellValue(workbook, sheet, 3, 9, student.getIdno(), fontSize12,
                            HSSFCellStyle.ALIGN_LEFT, true, null, null);
                    Toolket.setCellValue(workbook, sheet, 4, 1, Toolket.getSex(student.getSex()), fontSize12,
                            HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                    // Toolket.setCellValue(workbook, sheet, 4, 3, df
                    // .format(student.getBirthday()), fontSize11,
                    // HSSFCellStyle.ALIGN_LEFT, true, null, null);
                    Toolket.setCellValue(workbook, sheet, 4, 3,
                            printNativeDate(df.format(student.getBirthday())), fontSize11,
                            HSSFCellStyle.ALIGN_LEFT, true, null, null);
                    Toolket.setCellValue(workbook, sheet, 4, 9,
                            StringUtils.isBlank(student.getIdent()) ? ""
                                    : Toolket.getIdentity(student.getIdent()),
                            fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                    Toolket.setCellValue(workbook, sheet, 9, 1,
                            (StringUtils.isBlank(student.getPermPost()) ? "" : student.getPermPost()) + "  "
                                    + (StringUtils.isBlank(student.getPermAddr()) ? "" : student.getPermAddr()),
                            fontSize12, HSSFCellStyle.ALIGN_LEFT, true, null, null);
                    Toolket.setCellValue(workbook, sheet, 9, 9, student.getTelephone(), fontSize12,
                            HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                    Toolket.setCellValue(workbook, sheet, 10, 9, student.getCellPhone(), fontSize12,
                            HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                    Toolket.setCellValue(workbook, sheet, 11, 2, student.getParentName(), fontSize12,
                            HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                    Toolket.setCellValue(workbook, sheet, 12, 2,
                            student.getCurrPost() + " " + student.getCurrAddr(), fontSize8,
                            HSSFCellStyle.ALIGN_LEFT, true, null, null);

                    image = new StdImage();
                    image.setStudentNo(student.getStudentNo());
                    image = mm.findStdImageBy(image);
                    if (image != null) {
                        bytes = image.getImage().getBytes(1l, (int) image.getImage().length());
                        // Image im =
                        // java.awt.Toolkit.getDefaultToolkit().getImage(filename);
                        try {
                            pictureIndex = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
                        } catch (Exception e) {
                            try {
                                pictureIndex = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
                            } catch (Exception e1) {
                                pictureIndex = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PICT);
                            }
                        }

                        // helper = workbook.getCreationHelper();
                        drawing = sheet.createDrawingPatriarch();
                        // anchor = helper.createClientAnchor();
                        anchor1 = new HSSFClientAnchor(0, 0, 400, 100, (short) 13, 19, (short) 17, 29);
                        anchor1.setAnchorType(0);
                        // anchor.setCol1(13);
                        // anchor.setRow1(19);
                        pict = drawing.createPicture(anchor1, pictureIndex);
                        try {
                            pict.resize(); // 154611
                            // pict.resize(0.5);
                        } catch (Exception e) {
                            e.printStackTrace();
                            log.error(e.getMessage(), e);
                        }
                    }

                    if (student.getRegistrationCard() != null) {
                        rc = student.getRegistrationCard();
                        Toolket.setCellValue(workbook, sheet, 2, 8,
                                Toolket.getCellValue(sheet, 2, 8) + " "
                                        + (rc.getDiviName() == null ? "" : rc.getDiviName().trim()),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);

                        Toolket.setCellValue(workbook, sheet, 3, 5,
                                StringUtils.isBlank(rc.getBirthCountry()) ? "" : rc.getBirthCountry().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 4, 5,
                                StringUtils.isBlank(rc.getBirthPlace()) ? "" : rc.getBirthPlace().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 5, 2,
                                StringUtils.isBlank(rc.getAborigine()) ? "" : rc.getAborigine().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 5, 5,
                                StringUtils.isBlank(rc.getForeignPlace()) ? "" : rc.getForeignPlace().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 5, 9,
                                rc.getArmyIn() == null ? "?"
                                        : "?" + df.format(rc.getArmyIn()),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 6, 9,
                                rc.getArmyOut() == null ? "?"
                                        : "?" + df.format(rc.getArmyOut()),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 7, 3,
                                StringUtils.isBlank(rc.getBeforeSchool()) ? "" : rc.getBeforeSchool().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 7, 9,
                                StringUtils.isBlank(rc.getGradeYear()) ? "" : rc.getGradeYear(), fontSize12,
                                HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 7, 11,
                                rc.getGradeType() == null ? "" : rc.getGradeType().toString(), fontSize12,
                                HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 8, 3,
                                StringUtils.isBlank(rc.getBeforeDept()) ? "" : rc.getBeforeDept().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 11, 5,
                                StringUtils.isBlank(rc.getParentAge()) ? "" : rc.getParentAge().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 11, 7,
                                StringUtils.isBlank(rc.getParentCareer()) ? "" : rc.getParentCareer().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 11, 11,
                                StringUtils.isBlank(rc.getParentRelationship()) ? ""
                                        : rc.getParentRelationship().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 12, 10,
                                StringUtils.isBlank(rc.getEmergentPhone()) ? "" : rc.getEmergentPhone().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 13, 10,
                                StringUtils.isBlank(rc.getEmergentCell()) ? "" : rc.getEmergentCell().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_LEFT, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 15, 1,
                                StringUtils.isBlank(rc.getMemberTitle1()) ? "" : rc.getMemberTitle1().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 15, 2,
                                StringUtils.isBlank(rc.getMemberName1()) ? "" : rc.getMemberName1().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 15, 4,
                                StringUtils.isBlank(rc.getMemberAge1()) ? "" : rc.getMemberAge1().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 15, 5,
                                StringUtils.isBlank(rc.getMemberCareer1()) ? "" : rc.getMemberCareer1().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);

                        Toolket.setCellValue(workbook, sheet, 15, 7,
                                StringUtils.isBlank(rc.getMemberTitle2()) ? "" : rc.getMemberTitle2().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 15, 8,
                                StringUtils.isBlank(rc.getMemberName2()) ? "" : rc.getMemberName2().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 15, 10,
                                StringUtils.isBlank(rc.getMemberAge2()) ? "" : rc.getMemberAge2().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 15, 11,
                                StringUtils.isBlank(rc.getMemberCareer2()) ? "" : rc.getMemberCareer2().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);

                        Toolket.setCellValue(workbook, sheet, 16, 1,
                                StringUtils.isBlank(rc.getMemberTitle3()) ? "" : rc.getMemberTitle3().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 16, 2,
                                StringUtils.isBlank(rc.getMemberName3()) ? "" : rc.getMemberName3().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 16, 4,
                                StringUtils.isBlank(rc.getMemberAge3()) ? "" : rc.getMemberAge3().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 16, 5,
                                StringUtils.isBlank(rc.getMemberCareer3()) ? "" : rc.getMemberCareer3().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);

                        Toolket.setCellValue(workbook, sheet, 16, 7,
                                StringUtils.isBlank(rc.getMemberTitle4()) ? "" : rc.getMemberTitle4().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 16, 8,
                                StringUtils.isBlank(rc.getMemberName4()) ? "" : rc.getMemberName4().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 16, 10,
                                StringUtils.isBlank(rc.getMemberAge4()) ? "" : rc.getMemberAge4().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                        Toolket.setCellValue(workbook, sheet, 16, 11,
                                StringUtils.isBlank(rc.getMemberCareer4()) ? "" : rc.getMemberCareer4().trim(),
                                fontSize12, HSSFCellStyle.ALIGN_CENTER, true, false, null, null);
                    }

                }

            }
        }

        File tempDir = new File(
                context.getRealPath("/WEB-INF/reports/temp/" + getUserCredential(session).getMember().getIdno()
                        + (new SimpleDateFormat("yyyyMMdd").format(new Date()))));
        if (!tempDir.exists())
            tempDir.mkdirs();

        File output = new File(tempDir, "ClassStudentRegistrationCard.xls");
        FileOutputStream fos = new FileOutputStream(output);
        workbook.write(fos);
        fos.close();

        JasperReportUtils.printXlsToFrontEnd(response, output);
        output.delete();
        tempDir.delete();
    } else {
        Map<String, String> param = new HashMap<String, String>();
        File image = new File(context.getRealPath("/pages/images/2002chitS.jpg"));
        param.put("IMAGE", image.getAbsolutePath());
        byte[] bytes = JasperRunManager.runReportToPdf(JasperReportUtils.getNoResultReport(context), param,
                new JREmptyDataSource());
        JasperReportUtils.printPdfToFrontEnd(response, bytes);
    }
}