List of usage examples for org.apache.poi.ss.usermodel Drawing createPicture
Picture createPicture(ClientAnchor anchor, int pictureIndex);
From source file:packtest.WorkingWithPictures.java
License:Apache License
public static void main(String[] args) throws IOException { //create a new workbook Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); try {//from ww w . ja va 2 s . c om CreationHelper helper = wb.getCreationHelper(); //add a picture in this workbook. InputStream is = new FileInputStream(args[0]); byte[] bytes = IOUtils.toByteArray(is); is.close(); int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); //create sheet Sheet sheet = wb.createSheet(); //create drawing Drawing drawing = sheet.createDrawingPatriarch(); //add a picture shape ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(1); anchor.setRow1(1); Picture pict = drawing.createPicture(anchor, pictureIdx); //auto-size picture pict.resize(2); //save workbook String file = "picture.xls"; if (wb instanceof XSSFWorkbook) file += "x"; // NOSONAR OutputStream fileOut = new FileOutputStream(file); try { wb.write(fileOut); } finally { fileOut.close(); } } finally { wb.close(); } }
From source file:poi.xssf.usermodel.examples.WorkingWithPictures.java
License:Apache License
public static void main(String[] args) throws IOException { //create a new workbook Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); CreationHelper helper = wb.getCreationHelper(); //add a picture in this workbook. InputStream is = new FileInputStream(args[0]); byte[] bytes = IOUtils.toByteArray(is); is.close();//w w w. j a va2 s . c om int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); //create sheet Sheet sheet = wb.createSheet(); //create drawing Drawing drawing = sheet.createDrawingPatriarch(); //add a picture shape ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(1); anchor.setRow1(1); Picture pict = drawing.createPicture(anchor, pictureIdx); //auto-size picture pict.resize(2); //save workbook String file = "picture.xls"; if (wb instanceof XSSFWorkbook) file += "x"; FileOutputStream fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); }
From source file:tw.edu.chit.struts.action.registration.ReportPrintAction.java
/** * ???//w w w .jav a2 s . com * * @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); } }
From source file:uk.co.spudsoft.birt.emitters.excel.handlers.PageHandler.java
License:Open Source License
/** * <p>/*from w w w.j a v a 2 s . c om*/ * Process a CellImage from the images list and place the image on the sheet. * </p><p> * This involves changing the row height as necesssary and determining the column spread of the image. * </p> * @param cellImage * The image to be placed on the sheet. */ private void processCellImage(HandlerState state, Drawing drawing, CellImage cellImage) { Coordinate location = cellImage.location; Cell cell = state.currentSheet.getRow(location.getRow()).getCell(location.getCol()); IImageContent image = cellImage.image; StyleManagerUtils smu = state.getSmu(); float ptHeight = cell.getRow().getHeightInPoints(); if (image.getHeight() != null) { ptHeight = smu.fontSizeInPoints(image.getHeight().toString()); } // Get image width int endCol = cell.getColumnIndex(); double lastColWidth = ClientAnchorConversions .widthUnits2Millimetres((short) state.currentSheet.getColumnWidth(endCol)) + 2.0; int dx = smu.anchorDxFromMM(lastColWidth, lastColWidth); double mmWidth = 0.0; if (smu.isAbsolute(image.getWidth())) { mmWidth = image.getWidth().convertTo(DimensionType.UNITS_MM); } else if (smu.isPixels(image.getWidth())) { mmWidth = ClientAnchorConversions.pixels2Millimetres(image.getWidth().getMeasure()); } // Allow image to span multiple columns CellRangeAddress mergedRegion = getMergedRegionBegunBy(state.currentSheet, location.getRow(), location.getCol()); if ((cellImage.spanColumns) || (mergedRegion != null)) { log.debug("Image size: ", image.getWidth(), " translates as mmWidth = ", mmWidth); if (mmWidth > 0) { double mmAccumulatedWidth = 0; int endColLimit = cellImage.spanColumns ? 256 : mergedRegion.getLastColumn(); for (endCol = cell.getColumnIndex(); mmAccumulatedWidth < mmWidth && endCol < endColLimit; ++endCol) { lastColWidth = ClientAnchorConversions .widthUnits2Millimetres((short) state.currentSheet.getColumnWidth(endCol)) + 2.0; mmAccumulatedWidth += lastColWidth; log.debug("lastColWidth = ", lastColWidth, "; mmAccumulatedWidth = ", mmAccumulatedWidth); } if (mmAccumulatedWidth > mmWidth) { mmAccumulatedWidth -= lastColWidth; --endCol; double mmShort = mmWidth - mmAccumulatedWidth; dx = smu.anchorDxFromMM(mmShort, lastColWidth); } } } else { float widthRatio = (float) (mmWidth / lastColWidth); ptHeight = ptHeight / widthRatio; } int rowsSpanned = state.findRowsSpanned(cell.getRowIndex(), cell.getColumnIndex()); float neededRowHeightPoints = ptHeight; for (int i = 0; i < rowsSpanned; ++i) { int rowIndex = cell.getRowIndex() + 1 + i; neededRowHeightPoints -= state.currentSheet.getRow(rowIndex).getHeightInPoints(); } if (neededRowHeightPoints > cell.getRow().getHeightInPoints()) { cell.getRow().setHeightInPoints(neededRowHeightPoints); } // ClientAnchor anchor = wb.getCreationHelper().createClientAnchor(); ClientAnchor anchor = state.getWb().getCreationHelper().createClientAnchor(); anchor.setCol1(cell.getColumnIndex()); anchor.setRow1(cell.getRowIndex()); anchor.setCol2(endCol); anchor.setRow2(cell.getRowIndex() + rowsSpanned); anchor.setDx2(dx); anchor.setDy2(smu.anchorDyFromPoints(ptHeight, cell.getRow().getHeightInPoints())); anchor.setAnchorType(ClientAnchor.MOVE_DONT_RESIZE); drawing.createPicture(anchor, cellImage.imageIdx); }
From source file:uk.co.spudsoft.birt.emitters.excel2.handlers.PageHandler.java
License:Open Source License
/** * <p>//from ww w . j a v a 2s . co m * Process a CellImage from the images list and place the image on the sheet. * </p><p> * This involves changing the row height as necesssary and determining the column spread of the image. * </p> * @param cellImage * The image to be placed on the sheet. */ private void processCellImage(HandlerState state, Drawing drawing, CellImage cellImage) { Coordinate location = cellImage.location; Cell cell = state.currentSheet.getRow(location.getRow()).getCell(location.getCol()); IImageContent image = cellImage.image; StyleManagerUtils smu = state.getSmu(); float ptHeight = cell.getRow().getHeightInPoints(); if (image.getHeight() != null) { ptHeight = smu.fontSizeInPoints(image.getHeight().toString()); } // Get image width int endCol = cell.getColumnIndex(); double lastColWidth = ClientAnchorConversions .widthUnits2Millimetres((short) state.currentSheet.getColumnWidth(endCol)) + 2.0; double mmWidth = 0.0; if (smu.isAbsolute(image.getWidth())) { mmWidth = image.getWidth().convertTo(DimensionType.UNITS_MM); } else if (smu.isPixels(image.getWidth())) { mmWidth = ClientAnchorConversions.pixels2Millimetres(image.getWidth().getMeasure()); } int dx = smu.anchorDxFromMM(mmWidth, lastColWidth); // Allow image to span multiple columns CellRangeAddress mergedRegion = getMergedRegionBegunBy(state.currentSheet, location.getRow(), location.getCol()); if ((cellImage.spanColumns) || (mergedRegion != null)) { log.debug("Image size: ", image.getWidth(), " translates as mmWidth = ", mmWidth); if (mmWidth > 0) { double mmAccumulatedWidth = 0; int endColLimit = cellImage.spanColumns ? 256 : mergedRegion.getLastColumn(); for (endCol = cell.getColumnIndex(); mmAccumulatedWidth < mmWidth && endCol < endColLimit; ++endCol) { lastColWidth = ClientAnchorConversions .widthUnits2Millimetres((short) state.currentSheet.getColumnWidth(endCol)) + 2.0; mmAccumulatedWidth += lastColWidth; log.debug("lastColWidth = ", lastColWidth, "; mmAccumulatedWidth = ", mmAccumulatedWidth); } if (mmAccumulatedWidth > mmWidth) { mmAccumulatedWidth -= lastColWidth; --endCol; double mmShort = mmWidth - mmAccumulatedWidth; dx = smu.anchorDxFromMM(mmShort, lastColWidth); } } /* } else { float widthRatio = (float)(mmWidth / lastColWidth); ptHeight = ptHeight * widthRatio; */ } int rowsSpanned = state.findRowsSpanned(cell.getRowIndex(), cell.getColumnIndex()); float neededRowHeightPoints = ptHeight; for (int i = 0; i < rowsSpanned; ++i) { int rowIndex = cell.getRowIndex() + 1 + i; neededRowHeightPoints -= state.currentSheet.getRow(rowIndex).getHeightInPoints(); } if (neededRowHeightPoints > cell.getRow().getHeightInPoints()) { cell.getRow().setHeightInPoints(neededRowHeightPoints); } // ClientAnchor anchor = wb.getCreationHelper().createClientAnchor(); ClientAnchor anchor = state.getWb().getCreationHelper().createClientAnchor(); anchor.setCol1(cell.getColumnIndex()); anchor.setRow1(cell.getRowIndex()); anchor.setCol2(endCol); anchor.setRow2(cell.getRowIndex() + rowsSpanned); anchor.setDx2(dx); anchor.setDy2(smu.anchorDyFromPoints(ptHeight, cell.getRow().getHeightInPoints())); anchor.setAnchorType(ClientAnchor.MOVE_DONT_RESIZE); drawing.createPicture(anchor, cellImage.imageIdx); }
From source file:utilities.XLSResultsManager.java
License:Open Source License
private void closeRecord(ArrayList<CellItem> record, Sheet sheet, Map<String, CellStyle> styles, boolean embedImages) throws IOException { CreationHelper createHelper = wb.getCreationHelper(); Row row = sheet.createRow(rowIndex++); if (embedImages) { row.setHeight((short) 1000); }// w ww .jav a 2 s .c o m for (int i = 0; i < record.size(); i++) { CellItem ci = record.get(i); Cell cell = row.createCell(i); if (ci.v != null && (ci.v.startsWith("https://") || ci.v.startsWith("http://"))) { if (embedImages) { if (ci.v.endsWith(".jpg") || ci.v.endsWith(".png")) { int idx = ci.v.indexOf("attachments"); int idxName = ci.v.lastIndexOf('/'); if (idx > 0 && idxName > 0) { String fileName = ci.v.substring(idxName); String stem = basePath + "/" + ci.v.substring(idx, idxName); String imageName = stem + "/thumbs" + fileName + ".jpg"; try { InputStream inputStream = new FileInputStream(imageName); byte[] imageBytes = IOUtils.toByteArray(inputStream); int pictureureIdx = wb.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG); inputStream.close(); ClientAnchor anchor = createHelper.createClientAnchor(); anchor.setCol1(i); anchor.setRow1(rowIndex - 1); anchor.setCol2(i + 1); anchor.setRow2(rowIndex); anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE); //sheet.setColumnWidth(i, 20 * 256); Drawing drawing = sheet.createDrawingPatriarch(); Picture pict = drawing.createPicture(anchor, pictureureIdx); //pict.resize(); } catch (Exception e) { log.info("Error: Missing image file: " + imageName); } } } } cell.setCellStyle(styles.get("link")); if (isXLSX) { XSSFHyperlink url = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL); url.setAddress(ci.v); cell.setHyperlink(url); } else { HSSFHyperlink url = new HSSFHyperlink(HSSFHyperlink.LINK_URL); url.setAddress(ci.v); cell.setHyperlink(url); } cell.setCellValue(ci.v); } else { /* * Write the value as double or string */ boolean cellWritten = false; if (ci.type == CellItem.DECIMAL || ci.type == CellItem.INTEGER && ci.v != null) { try { double vDouble = Double.parseDouble(ci.v); cell.setCellStyle(styles.get("default")); cell.setCellValue(vDouble); cellWritten = true; } catch (Exception e) { // Ignore } } else if (ci.type == CellItem.DATETIME) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { java.util.Date date = dateFormat.parse(ci.v); cell.setCellStyle(styles.get("datetime")); cell.setCellValue(date); cellWritten = true; } catch (Exception e) { // Ignore } } else if (ci.type == CellItem.DATE) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { java.util.Date date = dateFormat.parse(ci.v); cell.setCellStyle(styles.get("date")); cell.setCellValue(date); cellWritten = true; } catch (Exception e) { // Ignore } } if (!cellWritten) { cell.setCellStyle(styles.get("default")); cell.setCellValue(ci.v); } } } }
From source file:Valuacion.Exporta.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: if(t_orden.getText().compareTo("")!=0) {//w w w . j a v a2 s . c o m FileNameExtensionFilter filtroImagen=new FileNameExtensionFilter("XLS","xls"); aviso.setFileFilter(filtroImagen); int r=aviso.showSaveDialog(null); if(r==aviso.APPROVE_OPTION) { boolean respuesta=true; File a=aviso.getSelectedFile(); File archivoXLS=null; if(a.exists()==true) { int i=JOptionPane.showConfirmDialog(null, "Deseas remplazar el archivo?", "confirmacin", JOptionPane.YES_NO_OPTION); if(i!=0) { respuesta=false; } else archivoXLS=a; } else { if(a.getName().indexOf(".xls")==-1) a= new File(a.getAbsoluteFile()+".xls"); archivoXLS=a; } if(respuesta==true) { Session session = HibernateUtil.getSessionFactory().openSession(); try { orden_act = (Orden)session.get(Orden.class, orden_act.getIdOrden()); //if(orden_act.getPedidos().isEmpty()==true) //{ //File archivoXLS = new File(t_orden.getText()+".xls"); if(archivoXLS.exists()) archivoXLS.delete(); Biff8EncryptionKey.setCurrentUserPassword("04650077"); archivoXLS.createNewFile(); Workbook libro = new HSSFWorkbook(); FileOutputStream archivo = new FileOutputStream(archivoXLS); Sheet hoja1 = libro.createSheet("especialidad"); Sheet hoja2 = libro.createSheet("catalogo"); Sheet hoja3 = libro.createSheet("marca"); Sheet hoja4 = libro.createSheet("tipo"); Sheet hoja5 = libro.createSheet("ejemplar"); Sheet hoja6 = libro.createSheet("orden"); Sheet hoja7 = libro.createSheet("partida"); Sheet hoja8 = libro.createSheet("compania"); Sheet hoja9 = libro.createSheet("imagen"); //***************************imagen******************************* Foto[] fotos = (Foto[]) orden_act.getFotos().toArray(new Foto[0]); for(int k=0;k<fotos.length-1;k++) { for(int f=0;f<(fotos.length-1)-k;f++) { if (fotos[f].getFecha().after(fotos[f+1].getFecha())==true) { Foto aux; aux=fotos[f]; fotos[f]=fotos[f+1]; fotos[f+1]=aux; } } } if(fotos.length>0) { try { InputStream is = new FileInputStream(ruta+"ordenes/"+orden_act.getIdOrden()+"/miniatura/"+fotos[0].getDescripcion()); byte[] bytes = IOUtils.toByteArray(is); int pictureIdx = libro.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); is.close(); CreationHelper helper = libro.getCreationHelper(); Drawing drawing = hoja9.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(3); anchor.setRow1(2); Picture pict = drawing.createPicture(anchor, pictureIdx); pict.resize(); }catch(Exception e){e.printStackTrace();} } //************************especialidad***************************** Especialidad[] especialidad = (Especialidad[])session.createCriteria(Especialidad.class).list().toArray(new Especialidad[0]); Row h1r0=hoja1.createRow(0); h1r0.createCell(0).setCellValue("Partida"); h1r0.createCell(1).setCellValue("sub"); h1r0.createCell(2).setCellValue("registro"); if(especialidad.length>0) { for(int i=0; i<especialidad.length; i++) { Row fh1 = hoja1.createRow(i+1); fh1.createCell(0).setCellValue(especialidad[i].getIdGrupoMecanico()); if(especialidad[i].getDescripcion()!=null) fh1.createCell(1).setCellValue(especialidad[i].getDescripcion()); else fh1.createCell(1).setCellValue(""); fh1.createCell(2).setCellValue("o"); } } //************************catalogo***************************** Catalogo[] catalogo = (Catalogo[])session.createCriteria(Catalogo.class).list().toArray(new Catalogo[0]); Row h2r0=hoja2.createRow(0); h2r0.createCell(0).setCellValue("id_catalogo"); h2r0.createCell(1).setCellValue("nombre"); h2r0.createCell(2).setCellValue("id_especialidad"); h2r0.createCell(3).setCellValue("registro"); if(catalogo.length>0) { for(int i=0; i<catalogo.length; i++) { Row fh2 = hoja2.createRow(i+1); fh2.createCell(0).setCellValue(catalogo[i].getIdCatalogo()); if(catalogo[i].getNombre()!=null) fh2.createCell(1).setCellValue(catalogo[i].getNombre()); else fh2.createCell(1).setCellValue(""); fh2.createCell(2).setCellValue(catalogo[i].getEspecialidad().getIdGrupoMecanico()); fh2.createCell(3).setCellValue("o"); } } //************************marca***************************** Marca[] marca = (Marca[])session.createCriteria(Marca.class).list().toArray(new Marca[0]); Row h3r0=hoja3.createRow(0); h3r0.createCell(0).setCellValue("id_marca"); h3r0.createCell(1).setCellValue("nombre_marca"); h3r0.createCell(2).setCellValue("registro"); if(marca.length>0) { for(int i=0; i<marca.length; i++) { Row fh3 = hoja3.createRow(i+1); fh3.createCell(0).setCellValue(marca[i].getIdMarca()); fh3.createCell(1).setCellValue(marca[i].getMarcaNombre()); fh3.createCell(2).setCellValue("o"); } } //************************tipo***************************** Tipo[] tipo = (Tipo[])session.createCriteria(Tipo.class).list().toArray(new Tipo[0]); Row h4r0=hoja4.createRow(0); h4r0.createCell(0).setCellValue("tipo_nombre"); h4r0.createCell(1).setCellValue("e_pesado"); h4r0.createCell(2).setCellValue("registro"); if(tipo.length>0) { for(int i=0; i<tipo.length; i++) { Row fh4 = hoja4.createRow(i+1); fh4.createCell(0).setCellValue(tipo[i].getTipoNombre()); fh4.createCell(1).setCellValue(tipo[i].getEPesado()); fh4.createCell(2).setCellValue("o"); } } //************************ejemplar***************************** Ejemplar[] ejemplar = (Ejemplar[])session.createCriteria(Ejemplar.class).list().toArray(new Ejemplar[0]); Row h5r0=hoja5.createRow(0); h5r0.createCell(0).setCellValue("id_parte"); h5r0.createCell(1).setCellValue("id_marca"); h5r0.createCell(2).setCellValue("tipo_nombre"); h5r0.createCell(3).setCellValue("modelo"); h5r0.createCell(4).setCellValue("id_catalogo"); h5r0.createCell(5).setCellValue("comentario"); h5r0.createCell(6).setCellValue("registro"); if(ejemplar.length>0) { for(int i=0; i<ejemplar.length; i++) { Row fh5 = hoja5.createRow(i+1); fh5.createCell(0).setCellValue(ejemplar[i].getIdParte()); if(ejemplar[i].getMarca()!=null) fh5.createCell(1).setCellValue(ejemplar[i].getMarca().getIdMarca()); else fh5.createCell(1).setCellValue(""); if(ejemplar[i].getTipo()!=null) fh5.createCell(2).setCellValue(ejemplar[i].getTipo().getTipoNombre()); else fh5.createCell(2).setCellValue(""); if(ejemplar[i].getModelo()!=null) fh5.createCell(3).setCellValue(ejemplar[i].getModelo()); else fh5.createCell(3).setCellValue(""); if(ejemplar[i].getCatalogo()!=null) fh5.createCell(4).setCellValue(ejemplar[i].getCatalogo()); else fh5.createCell(4).setCellValue(""); if(ejemplar[i].getComentario()!=null) fh5.createCell(5).setCellValue(ejemplar[i].getComentario()); else fh5.createCell(5).setCellValue(""); fh5.createCell(6).setCellValue("o"); } } //************************orden***************************** orden_act = (Orden)session.get(Orden.class, orden_act.getIdOrden()); Row h6r0=hoja6.createRow(0); h6r0.createCell(0).setCellValue("id_orden"); h6r0.createCell(1).setCellValue("aseguradora"); h6r0.createCell(2).setCellValue("poliza"); h6r0.createCell(3).setCellValue("siniestro"); h6r0.createCell(4).setCellValue("inciso"); h6r0.createCell(5).setCellValue("reporte"); h6r0.createCell(6).setCellValue("fecha"); h6r0.createCell(7).setCellValue("nombre"); h6r0.createCell(8).setCellValue("tipo_cliente"); h6r0.createCell(9).setCellValue("id_marca"); h6r0.createCell(10).setCellValue("tipo"); h6r0.createCell(11).setCellValue("anio"); h6r0.createCell(12).setCellValue("registro"); Row fh6 = hoja6.createRow(1); fh6.createCell(0).setCellValue(orden_act.getIdOrden()); fh6.createCell(1).setCellValue(orden_act.getCompania().getIdCompania()); if(orden_act.getPoliza()!=null) fh6.createCell(2).setCellValue(orden_act.getPoliza()); else fh6.createCell(2).setCellValue(""); if(orden_act.getSiniestro()!=null) fh6.createCell(3).setCellValue(orden_act.getSiniestro()); else fh6.createCell(3).setCellValue(""); if(orden_act.getInciso()!=null) fh6.createCell(4).setCellValue(orden_act.getInciso()); else fh6.createCell(4).setCellValue(""); if(orden_act.getNoReporte()!=null) fh6.createCell(5).setCellValue(orden_act.getNoReporte()); else fh6.createCell(5).setCellValue(""); fh6.createCell(6).setCellValue(orden_act.getFecha()); fh6.createCell(7).setCellValue(orden_act.getClientes().getNombre()); fh6.createCell(8).setCellValue(orden_act.getTipoCliente()); fh6.createCell(9).setCellValue(orden_act.getMarca().getIdMarca()); fh6.createCell(10).setCellValue(orden_act.getTipo().getTipoNombre()); fh6.createCell(11).setCellValue(orden_act.getModelo()); fh6.createCell(12).setCellValue("o"); Compania com=orden_act.getCompania(); Row h8r0=hoja8.createRow(0); h8r0.createCell(0).setCellValue("id_compania"); h8r0.createCell(1).setCellValue("nombre"); h8r0.createCell(2).setCellValue("importe_hota"); h8r0.createCell(3).setCellValue("importe_max"); h8r0.createCell(4).setCellValue("registro"); Row fh8 = hoja8.createRow(1); fh8.createCell(0).setCellValue(com.getIdCompania()); fh8.createCell(1).setCellValue(com.getNombre()); fh8.createCell(2).setCellValue(com.getImporteHora()); fh8.createCell(3).setCellValue(com.getImporteMax()); fh8.createCell(4).setCellValue("o"); //************************partida***************************** Partida[] cuentas = (Partida[])session.createCriteria(Partida.class).add(Restrictions.eq("ordenByIdOrden.idOrden", orden_act.getIdOrden())).addOrder(Order.asc("idEvaluacion")).addOrder(Order.asc("subPartida")).list().toArray(new Partida[0]); Partida[] enlazadas = (Partida[])session.createCriteria(Partida.class).add(Restrictions.eq("ordenByEnlazada.idOrden", orden_act.getIdOrden())).addOrder(Order.asc("idEvaluacion")).addOrder(Order.asc("subPartida")).list().toArray(new Partida[0]); Row h7r0=hoja7.createRow(0); h7r0.createCell(0).setCellValue("id_partida"); h7r0.createCell(1).setCellValue("id_evaluacion"); h7r0.createCell(2).setCellValue("sub_partida"); h7r0.createCell(3).setCellValue("esp_hoj"); h7r0.createCell(4).setCellValue("esp_mec"); h7r0.createCell(5).setCellValue("esp_sus"); h7r0.createCell(6).setCellValue("esp_ele"); h7r0.createCell(7).setCellValue("dm"); h7r0.createCell(8).setCellValue("cam"); h7r0.createCell(9).setCellValue("rep_min"); h7r0.createCell(10).setCellValue("rep_med"); h7r0.createCell(11).setCellValue("rep_max"); h7r0.createCell(12).setCellValue("pint"); h7r0.createCell(13).setCellValue("cant"); h7r0.createCell(14).setCellValue("med"); h7r0.createCell(15).setCellValue("id_catalogo"); h7r0.createCell(16).setCellValue("id_parte"); h7r0.createCell(17).setCellValue("incluida"); h7r0.createCell(18).setCellValue("ori"); h7r0.createCell(19).setCellValue("nal"); h7r0.createCell(20).setCellValue("desm"); h7r0.createCell(21).setCellValue("pd"); h7r0.createCell(22).setCellValue("tot"); h7r0.createCell(23).setCellValue("int_desm"); h7r0.createCell(24).setCellValue("int_camb"); h7r0.createCell(25).setCellValue("int_rep_min"); h7r0.createCell(26).setCellValue("int_rep_med"); h7r0.createCell(27).setCellValue("int_rep_max"); h7r0.createCell(28).setCellValue("int_pin_min"); h7r0.createCell(29).setCellValue("int_pin_med"); h7r0.createCell(30).setCellValue("int_pin_max"); h7r0.createCell(31).setCellValue("instruccion"); h7r0.createCell(32).setCellValue("tipo"); h7r0.createCell(33).setCellValue("enlazada"); h7r0.createCell(34).setCellValue("autorizado_valuacion"); h7r0.createCell(35).setCellValue("c_u"); h7r0.createCell(36).setCellValue("porcentaje"); h7r0.createCell(37).setCellValue("precio_cia"); h7r0.createCell(38).setCellValue("cant_aut"); h7r0.createCell(39).setCellValue("precio_aut"); h7r0.createCell(40).setCellValue("autorizado"); h7r0.createCell(41).setCellValue("horas"); h7r0.createCell(42).setCellValue("ref_coti"); h7r0.createCell(43).setCellValue("ref_com"); h7r0.createCell(44).setCellValue("so"); h7r0.createCell(45).setCellValue("pedido"); h7r0.createCell(46).setCellValue("entrega"); h7r0.createCell(47).setCellValue("id_orden"); h7r0.createCell(48).setCellValue("pcp"); h7r0.createCell(49).setCellValue("registro"); if(cuentas.length>0) { for(int i=0; i<cuentas.length; i++) { Row fh7 = hoja7.createRow(i+1); fh7.createCell(0).setCellValue(cuentas[i].getIdPartida()); fh7.createCell(1).setCellValue(cuentas[i].getIdEvaluacion()); fh7.createCell(2).setCellValue(cuentas[i].getSubPartida()); fh7.createCell(3).setCellValue(cuentas[i].isEspHoj()); fh7.createCell(4).setCellValue(cuentas[i].isEspMec()); fh7.createCell(5).setCellValue(cuentas[i].isEspSus()); fh7.createCell(6).setCellValue(cuentas[i].isEspEle()); fh7.createCell(7).setCellValue(cuentas[i].getDm()); fh7.createCell(8).setCellValue(cuentas[i].getCam()); fh7.createCell(9).setCellValue(cuentas[i].getRepMin()); fh7.createCell(10).setCellValue(cuentas[i].getRepMed()); fh7.createCell(11).setCellValue(cuentas[i].getRepMax()); fh7.createCell(12).setCellValue(cuentas[i].getPint()); fh7.createCell(13).setCellValue(cuentas[i].getCant()); fh7.createCell(14).setCellValue(cuentas[i].getMed()); fh7.createCell(15).setCellValue(cuentas[i].getCatalogo().getIdCatalogo()); if(cuentas[i].getEjemplar()!=null) fh7.createCell(16).setCellValue(cuentas[i].getEjemplar().getIdParte()); else fh7.createCell(16).setCellValue(""); fh7.createCell(17).setCellValue(cuentas[i].isIncluida()); fh7.createCell(18).setCellValue(cuentas[i].isOri()); fh7.createCell(19).setCellValue(cuentas[i].isNal()); fh7.createCell(20).setCellValue(cuentas[i].isDesm()); fh7.createCell(21).setCellValue(cuentas[i].isPd()); if(cuentas[i].getProveedor()!=null) fh7.createCell(22).setCellValue(cuentas[i].getProveedor().getIdProveedor()); else fh7.createCell(22).setCellValue(""); fh7.createCell(23).setCellValue(cuentas[i].getIntDesm()); fh7.createCell(24).setCellValue(cuentas[i].getIntCamb()); fh7.createCell(25).setCellValue(cuentas[i].getIntRepMin()); fh7.createCell(26).setCellValue(cuentas[i].getIntRepMed()); fh7.createCell(27).setCellValue(cuentas[i].getIntRepMax()); fh7.createCell(28).setCellValue(cuentas[i].getIntPinMin()); fh7.createCell(29).setCellValue(cuentas[i].getIntPinMed()); fh7.createCell(30).setCellValue(cuentas[i].getIntPinMax()); if(cuentas[i].getInstruccion()!=null) fh7.createCell(31).setCellValue(cuentas[i].getInstruccion()); else fh7.createCell(31).setCellValue(""); fh7.createCell(32).setCellValue(cuentas[i].getTipo()); if(cuentas[i].getOrdenByEnlazada()!=null) fh7.createCell(33).setCellValue(cuentas[i].getOrdenByEnlazada().getIdOrden()); else fh7.createCell(33).setCellValue(""); fh7.createCell(34).setCellValue(cuentas[i].isAutorizadoValuacion()); fh7.createCell(35).setCellValue(cuentas[i].getCU()); fh7.createCell(36).setCellValue(cuentas[i].getPorcentaje()); fh7.createCell(37).setCellValue(cuentas[i].getPrecioCiaSegurosCU()); fh7.createCell(38).setCellValue(cuentas[i].getCantidadAut()); fh7.createCell(39).setCellValue(cuentas[i].getPrecioAutCU()); fh7.createCell(40).setCellValue(cuentas[i].isAutorizado()); fh7.createCell(41).setCellValue(cuentas[i].getHoras()); fh7.createCell(42).setCellValue(cuentas[i].isRefCoti()); fh7.createCell(43).setCellValue(cuentas[i].isRefComp()); fh7.createCell(44).setCellValue(cuentas[i].isSo()); if(cuentas[i].getPedido()!=null) { fh7.createCell(45).setCellValue(cuentas[i].getPedido().getIdPedido()); if(cuentas[i].getPlazo()!=null) fh7.createCell(46).setCellValue(cuentas[i].getPlazo()); else fh7.createCell(46).setCellValue(""); } else { fh7.createCell(45).setCellValue(""); fh7.createCell(46).setCellValue(""); } fh7.createCell(47).setCellValue(cuentas[i].getOrdenByIdOrden().getIdOrden()); fh7.createCell(48).setCellValue(cuentas[i].getPcp()); fh7.createCell(49).setCellValue("o"); } } libro.write(archivo); Biff8EncryptionKey.setCurrentUserPassword(null); archivo.close(); JOptionPane.showMessageDialog(null, "Archivo guardado!"); /*} else { if(session.isOpen()) session.close(); JOptionPane.showMessageDialog(null, "La orden ya contiene partidas!"); }*/ } catch (Exception he) { he.printStackTrace(); session.getTransaction().rollback(); } if(session!=null) if(session.isOpen()) session.close(); } } } else JOptionPane.showMessageDialog(this, "Debes seleccionar una orden de taller primero"); }