Example usage for org.apache.poi.xssf.usermodel XSSFWorkbook write

List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook write

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFWorkbook write.

Prototype

@SuppressWarnings("resource")
public final void write(OutputStream stream) throws IOException 

Source Link

Document

Write out this document to an Outputstream.

Usage

From source file:com.vodafone.poms.ii.helpers.ExportManager.java

public void exportActivityForUser() {
    List<Activity> activities = activityController.getUserItems();

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Master Track");
    Row row = sheet.createRow(0);//from   w ww  . j  av a  2s  .c  om
    for (int i = 0; i < activityHeaders.length; i++) {
        row.createCell(i).setCellValue(activityHeaders[i]);
    }
    for (int i = 0; i < activities.size(); i++) {
        row = sheet.createRow(i + 1);
        row.createCell(0).setCellValue(activities.get(i).getSite().getSitePhysical());
        row.createCell(1).setCellValue(activities.get(i).getAsp().getSubcontractorName());
        row.createCell(2).setCellValue(activities.get(i).getArea().getAreaName());
        row.createCell(3).setCellValue(activities.get(i).getVendorOwner().getOwnerName());
        if (activities.get(i).getClaimStatus() != null) {
            row.createCell(4).setCellValue(activities.get(i).getClaimStatus().getClaimName());
        } else {
            row.createCell(4).setCellValue("");
        }
        if (activities.get(i).getApprovalStatus() != null) {
            row.createCell(5).setCellValue(activities.get(i).getApprovalStatus().getStatusName());
        } else {
            row.createCell(5).setCellValue("");
        }
        row.createCell(6).setCellValue(activities.get(i).getActivityType().getDomainName());
        if (activities.get(i).getPhase() != null) {
            row.createCell(7).setCellValue(activities.get(i).getPhase().getPhaseName());
        } else {
            row.createCell(7).setCellValue("");
        }
        row.createCell(8).setCellValue(activities.get(i).getActivityDate());
        row.createCell(9).setCellValue(activities.get(i).getAcMaterialId());
        row.createCell(10).setCellValue(activities.get(i).getAcDescription());
        row.createCell(11).setCellValue(activities.get(i).getActivityDetails());
        row.createCell(12).setCellValue(activities.get(i).getQty());
        row.createCell(13).setCellValue(activities.get(i).getAcVendorPrice());
        row.createCell(14).setCellValue(activities.get(i).getTotalPriceVendor());
        row.createCell(15).setCellValue(activities.get(i).getTotalPriceVendorTaxes());
        row.createCell(16).setCellValue(activities.get(i).getAcSubcontractorPrice());
        row.createCell(17).setCellValue(activities.get(i).getTotalPriceAsp());
        row.createCell(18).setCellValue(activities.get(i).getTotalUm());
        row.createCell(19).setCellValue(activities.get(i).getTotalUmPercent());
        row.createCell(20).setCellValue(activities.get(i).getActivityComment());
        row.createCell(21).setCellValue((activities.get(i).getAspPoCollection().isEmpty() ? "Uncorrelated"
                : ((AspPo) activities.get(i).getAspPoCollection().toArray()[0]).getPoNumber()));
    }
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/vnd.ms-excel");
    externalContext.setResponseHeader("Content-Disposition",
            "attachment; filename=\"G-Cairo Region Extra Work.xlsx\"");

    try {
        workbook.write(externalContext.getResponseOutputStream());
        externalContext.getResponseOutputStream().close();
    } catch (IOException ex) {
        Logger.getLogger(ExportManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    facesContext.responseComplete();
    JsfUtil.addSuccessMessage("Activity Report is now exported");

}

From source file:com.vodafone.poms.ii.helpers.ExportManager.java

public void exportActivity() {
    try {/*from   w  ww . j  a  v  a 2s .  c o m*/
        List<Activity> activities = activityController.getExportItems(fromDate, toDate);

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Master Track");

        int imgPckId = addImageToWorkbook(workbook, "/home/poms/uploaded_data/pkg.png",
                Workbook.PICTURE_TYPE_PNG);
        String imgPckRelId = addImageToSheet(sheet, imgPckId, Workbook.PICTURE_TYPE_PNG);

        Row row = sheet.createRow(0);
        for (int i = 0; i < activityHeaders.length; i++) {
            row.createCell(i).setCellValue(activityHeaders[i]);
        }
        for (int i = 0; i < activities.size(); i++) {
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(activities.get(i).getSite().getSitePhysical());
            row.createCell(1).setCellValue(activities.get(i).getAsp().getSubcontractorName());
            row.createCell(2).setCellValue(activities.get(i).getArea().getAreaName());
            row.createCell(3).setCellValue(activities.get(i).getVendorOwner().getOwnerName());
            if (activities.get(i).getClaimStatus() != null) {
                row.createCell(4).setCellValue(activities.get(i).getClaimStatus().getClaimName());
            } else {
                row.createCell(4).setCellValue("");
            }
            if (activities.get(i).getApprovalStatus() != null) {
                row.createCell(5).setCellValue(activities.get(i).getApprovalStatus().getStatusName());
            } else {
                row.createCell(5).setCellValue("");
            }
            row.createCell(6).setCellValue(activities.get(i).getActivityType().getDomainName());
            if (activities.get(i).getPhase() != null) {
                row.createCell(7).setCellValue(activities.get(i).getPhase().getPhaseName());
            } else {
                row.createCell(7).setCellValue("");
            }
            row.createCell(8).setCellValue(activities.get(i).getActivityDate());
            row.createCell(9).setCellValue(activities.get(i).getAcMaterialId());
            row.createCell(10).setCellValue(activities.get(i).getAcDescription());
            row.createCell(11).setCellValue(activities.get(i).getActivityDetails());
            row.createCell(12).setCellValue(activities.get(i).getQty());
            row.createCell(13).setCellValue(activities.get(i).getAcVendorPrice());
            row.createCell(14).setCellValue(activities.get(i).getTotalPriceVendor());
            row.createCell(15).setCellValue(activities.get(i).getTotalPriceVendorTaxes());
            row.createCell(16).setCellValue(activities.get(i).getAcSubcontractorPrice());
            row.createCell(17).setCellValue(activities.get(i).getTotalPriceAsp());
            row.createCell(18).setCellValue(activities.get(i).getTotalUm());
            row.createCell(19).setCellValue(activities.get(i).getTotalUmPercent());
            row.createCell(20).setCellValue(activities.get(i).getActivityComment());
            row.createCell(21).setCellValue((activities.get(i).getAspPoCollection().isEmpty() ? "Uncorrelated"
                    : ((AspPo) activities.get(i).getAspPoCollection().toArray()[0]).getPoNumber()));
            if (activities.get(i).getActivityAttachmentsCollection() != null) {
                if (activities.get(i).getActivityAttachmentsCollection().size() > 0) {
                    Object[] attachments = activities.get(i).getActivityAttachmentsCollection().toArray();
                    for (int j = 0; j < attachments.length; j++) {
                        XSSFClientAnchor imgAnchor1 = new XSSFClientAnchor(0, 0, 0, 0, (23 + j),
                                row.getRowNum(), (23 + j + 1), row.getRowNum() + 1);
                        String oleRelId1 = addFile(sheet,
                                ((ActivityAttachments) attachments[j]).getAttachmentLocation(),
                                (i + j + activities.get(i).getActivityId().intValue() + Math.random()));
                        int shapeId1 = addImageToShape(sheet, imgAnchor1, imgPckId);
                        addObjectToShape(sheet, imgAnchor1, shapeId1, oleRelId1, imgPckRelId,
                                "Objekt-Manager-Shellobjekt");
                    }
                }
            }
        }
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.setResponseContentType("application/vnd.ms-excel");
        externalContext.setResponseHeader("Content-Disposition",
                "attachment; filename=\"G-Cairo Region Extra Work.xlsx\"");

        try {
            workbook.write(externalContext.getResponseOutputStream());
            externalContext.getResponseOutputStream().close();
        } catch (IOException ex) {
            Logger.getLogger(ExportManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        facesContext.responseComplete();
        JsfUtil.addSuccessMessage("Activity Report is now exported");

    } catch (InvalidFormatException ex) {
        Logger.getLogger(ExportManager.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ExportManager.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.vodafone.poms.ii.helpers.ExportManager.java

public void exportCustomerPO() {
    List<VendorPo> vendorPOs = vendorPOController.getExportItems(fromDate, toDate);

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("All POs");
    Row row = sheet.createRow(0);//from   w  w w  . j a  v a2  s.co m
    for (int i = 0; i < customerPOHeaders.length; i++) {
        row.createCell(i).setCellValue(customerPOHeaders[i]);
    }
    int innerRow = 0;
    for (int i = 0; i < vendorPOs.size(); i++) {
        row = sheet.createRow(i + 1 + innerRow);
        //po#
        row.createCell(0).setCellValue(vendorPOs.get(i).getPoNumber());
        //poDate
        row.createCell(1).setCellValue(vendorPOs.get(i).getPoDate());
        //domain
        row.createCell(2).setCellValue(vendorPOs.get(i).getDomainName().getDomainName());
        //type
        row.createCell(3).setCellValue(vendorPOs.get(i).getPoType().getTypeName());
        //description
        row.createCell(4).setCellValue(vendorPOs.get(i).getPoDescription());
        //factor
        row.createCell(5).setCellValue(vendorPOs.get(i).getFactor());
        //service_value
        row.createCell(6).setCellValue(vendorPOs.get(i).getServiceValue().toString());
        //po_value
        row.createCell(7).setCellValue(vendorPOs.get(i).getPoValue().toString());
        //po_value with taxes
        row.createCell(8).setCellValue(vendorPOs.get(i).getPoValueTaxes().toString());
        //work done
        row.createCell(9).setCellValue(vendorPOs.get(i).getWorkDone());
        //remaining in po
        row.createCell(10).setCellValue(vendorPOs.get(i).getRemainingInPo().toString());
        //taxes
        row.createCell(11).setCellValue(vendorPOs.get(i).getTaxes());
        Object[] mds = vendorPOs.get(i).getVendorMdCollection().toArray();
        for (int j = 0; j < mds.length; j++) {
            if (j > 1) {
                row = sheet.createRow(i + 1 + innerRow);
                innerRow++;
            }
            //md_deserved
            row.createCell(12).setCellValue(((VendorMd) mds[j]).getMdDeserved().toString());
            //md_value
            row.createCell(13)
                    .setCellValue(((VendorMd) mds[j]).getMdValue() != null
                            ? ((VendorMd) mds[j]).getMdValue().toString()
                            : "");
            //md_date
            row.createCell(14).setCellValue(
                    ((VendorMd) mds[j]).getMdDate() != null ? ((VendorMd) mds[j]).getMdDate() : null);
            //md_number
            row.createCell(15).setCellValue(
                    ((VendorMd) mds[j]).getMdNumber() != null ? ((VendorMd) mds[j]).getMdNumber() : "");
            //invoiced
            row.createCell(16).setCellValue(
                    ((VendorMd) mds[j]).getInvoiced() != null ? ((VendorMd) mds[j]).getInvoiced() : false);
            //remaining in md
            row.createCell(17)
                    .setCellValue(((VendorMd) mds[j]).getRemainingInMd() != null
                            ? ((VendorMd) mds[j]).getRemainingInMd().toString()
                            : "");

        }

    }
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/vnd.ms-excel");
    externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"Customer POs.xlsx\"");

    try {
        workbook.write(externalContext.getResponseOutputStream());
        externalContext.getResponseOutputStream().close();
    } catch (IOException ex) {
        Logger.getLogger(ExportManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    facesContext.responseComplete();
    JsfUtil.addSuccessMessage("Customer PO Report is now exported");

}

From source file:com.vodafone.poms.ii.helpers.ExportManager.java

public void exportASPPO() {
    List<AspPo> aspPOs = aspPOController.getExportItems(fromDate, toDate);

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("All POs");
    Row row = sheet.createRow(0);/*from   w  w  w .  j  a  va 2  s .co  m*/
    for (int i = 0; i < aspPOHeaders.length; i++) {
        row.createCell(i).setCellValue(aspPOHeaders[i]);
    }
    int innerRow = 0;
    for (int i = 0; i < aspPOs.size(); i++) {
        row = sheet.createRow(i + 1 + innerRow);
        //po#
        row.createCell(0).setCellValue(aspPOs.get(i).getPoNumber());
        //poDate
        row.createCell(1).setCellValue(aspPOs.get(i).getPoDate());
        //domain
        row.createCell(2).setCellValue(aspPOs.get(i).getDomainName().getDomainName());
        //type
        row.createCell(3).setCellValue(aspPOs.get(i).getPoType().getTypeName());
        //description
        row.createCell(4).setCellValue(aspPOs.get(i).getPoDescription());
        //factor
        row.createCell(5).setCellValue(aspPOs.get(i).getFactor());
        //service_value
        row.createCell(6).setCellValue(aspPOs.get(i).getServiceValue().toString());
        //po_value
        row.createCell(7).setCellValue(aspPOs.get(i).getPoValue().toString());
        //po_value with taxes
        row.createCell(8).setCellValue(aspPOs.get(i).getPoValueTaxes().toString());
        //work done
        row.createCell(9).setCellValue(aspPOs.get(i).getWorkDone());
        //remaining in po
        row.createCell(10).setCellValue(aspPOs.get(i).getRemainingInPo().toString());
        //taxes
        row.createCell(11).setCellValue(aspPOs.get(i).getTaxes());
        //ASP
        row.createCell(12).setCellValue(aspPOs.get(i).getAsp().getSubcontractorName());
        //VendorPO
        row.createCell(13).setCellValue((aspPOs.get(i).getVendorPoCollection().isEmpty() ? "Uncorrelated"
                : ((VendorPo) aspPOs.get(i).getVendorPoCollection().toArray()[0]).getPoNumber()));
        Object[] grns = aspPOs.get(i).getAspGrnCollection().toArray();
        for (int j = 0; j < grns.length; j++) {
            if (j > 1) {
                row = sheet.createRow(i + 1 + innerRow);
                innerRow++;
            }
            //md_deserved
            row.createCell(14).setCellValue(((AspGrn) grns[j]).getGrnDeserved().toString());
            //md_value
            row.createCell(15)
                    .setCellValue(((AspGrn) grns[j]).getGrnValue() != null
                            ? ((AspGrn) grns[j]).getGrnValue().toString()
                            : "");
            //md_date
            row.createCell(16).setCellValue(
                    ((AspGrn) grns[j]).getGrnDate() != null ? ((AspGrn) grns[j]).getGrnDate() : null);
            //md_number
            row.createCell(17).setCellValue(
                    ((AspGrn) grns[j]).getGrnNumber() != null ? ((AspGrn) grns[j]).getGrnNumber() : "");
            //invoiced
            row.createCell(18).setCellValue(
                    ((AspGrn) grns[j]).getInvoiced() != null ? ((AspGrn) grns[j]).getInvoiced() : false);
            //remaining in md
            row.createCell(19)
                    .setCellValue(((AspGrn) grns[j]).getRemainingInGrn() != null
                            ? ((AspGrn) grns[j]).getRemainingInGrn().toString()
                            : "");

        }

    }
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/vnd.ms-excel");
    externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"ASP POs.xlsx\"");

    try {
        workbook.write(externalContext.getResponseOutputStream());
        externalContext.getResponseOutputStream().close();
    } catch (IOException ex) {
        Logger.getLogger(ExportManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    facesContext.responseComplete();
    JsfUtil.addSuccessMessage("Customer PO Report is now exported");

}

From source file:com.vsquaresystem.safedeals.amenity.AmenityService.java

public Boolean exportExcel() throws IOException {

    List<Amenity> rs = amenityDAL.findAllAmenities();
    System.out.println("result set excel sop" + rs);
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Amenity Master");
    XSSFRow xssfrow = sheet.createRow((short) 0);
    xssfrow.createCell(1);/*from   w w w  . java2 s. c om*/
    xssfrow.createCell(2);
    xssfrow.createCell(3);
    xssfrow.getCell(1).setCellValue("Id");
    xssfrow.getCell(2).setCellValue("Amenity Name");
    xssfrow.getCell(3).setCellValue("Amenity Code Id");
    //        List<Book> listBook = excelWriter.getListBook();
    String fileName = "/AmenityMasterData.xls";
    String exportPath = attachmentUtils.getAmenityExportAttachmentRootDirectory() + fileName;

    System.out.println("exportPath" + exportPath);

    int rowCount = 0;

    for (Amenity aAmenity : rs) {
        Row row = sheet.createRow(++rowCount);
        writeBook(aAmenity, row);
    }

    try (FileOutputStream outputStream = new FileOutputStream(exportPath)) {
        workbook.write(outputStream);
    }

    return true;
}

From source file:com.vsquaresystem.safedeals.city.CityService.java

public Boolean exportExcel() throws IOException {
    logger.info("getExportExcel method is working");
    List<City> rs = cityDAL.findAllCities();
    System.out.println("result set excel sop" + rs);
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("City Master");
    XSSFRow xssfrow = sheet.createRow((short) 0);
    xssfrow.createCell(1);/*from  w ww . j a  v  a 2s  . c  om*/
    xssfrow.createCell(2);
    xssfrow.createCell(3);
    xssfrow.createCell(4);
    xssfrow.createCell(5);
    xssfrow.createCell(6);
    xssfrow.getCell(1).setCellValue("City Id");
    xssfrow.getCell(2).setCellValue("Name");
    xssfrow.getCell(3).setCellValue("Country Id");
    xssfrow.getCell(4).setCellValue("State Id");
    xssfrow.getCell(5).setCellValue("Latitude Id");
    xssfrow.getCell(6).setCellValue("Longitude Id");
    String fileName = "/CityMasterData.xls";
    String exportPath = attachmentUtils.getCityExportAttachmentRootDirectory() + fileName;
    System.out.println("exportPath" + exportPath);
    int rowCount = 0;

    for (City aLocation : rs) {
        Row row = sheet.createRow(++rowCount);
        writeBook(aLocation, row);
    }

    try (FileOutputStream outputStream = new FileOutputStream(exportPath)) {
        workbook.write(outputStream);
    }

    return true;
}

From source file:com.vsquaresystem.safedeals.location.LocationService.java

public Boolean exportExcel() throws IOException {
    logger.info("getExportExcel method is working");
    List<Location> rs = locationDAL.findAllLocations();
    System.out.println("result set excel sop" + rs);
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Location Master");
    XSSFRow xssfrow = sheet.createRow((short) 0);
    xssfrow.createCell(1);/*  w w w  .j  a v  a  2 s. co  m*/
    xssfrow.createCell(2);
    xssfrow.createCell(3);
    xssfrow.getCell(1).setCellValue("Location Id");
    xssfrow.getCell(2).setCellValue("Location Name");
    xssfrow.getCell(3).setCellValue("City Id");
    String fileName = "/LocationMasterData.xls";
    String exportPath = attachmentUtils.getLocationExportAttachmentRootDirectory() + fileName;
    System.out.println("exportPath" + exportPath);
    int rowCount = 0;
    for (Location aLocation : rs) {
        Row row = sheet.createRow(++rowCount);
        writeBook(aLocation, row);
    }
    try (FileOutputStream outputStream = new FileOutputStream(exportPath)) {
        workbook.write(outputStream);
    }
    return true;
}

From source file:com.vsquaresystem.safedeals.rawmarketprice.RawMarketPriceService.java

public Boolean exportExcel() throws IOException {
    logger.info("getExportExcel method is working for raw market price");
    List<RawMarketPrice> rmp = rawMarketPriceDAL.findAllRawMarketPrice();
    //        logger.info("result set excel sop" + rmp);
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Raw Market Price Master");
    XSSFRow xssfrow = sheet.createRow((short) 0);
    xssfrow.createCell(1);/*  ww  w.j av a 2 s.  c  o  m*/
    xssfrow.createCell(2);
    xssfrow.createCell(3);
    xssfrow.createCell(4);
    xssfrow.createCell(5);
    xssfrow.createCell(6);
    xssfrow.createCell(7);
    xssfrow.createCell(8);
    xssfrow.createCell(9);
    xssfrow.createCell(10);
    xssfrow.createCell(11);
    xssfrow.createCell(12);
    xssfrow.createCell(13);
    xssfrow.createCell(14);
    xssfrow.createCell(15);
    xssfrow.createCell(16);
    xssfrow.createCell(17);
    xssfrow.createCell(18);
    xssfrow.createCell(19);
    xssfrow.createCell(20);
    xssfrow.createCell(21);
    xssfrow.createCell(22);
    xssfrow.createCell(23);
    xssfrow.createCell(24);
    xssfrow.createCell(25);
    xssfrow.getCell(1).setCellValue("Id");
    xssfrow.getCell(2).setCellValue("City Name");
    xssfrow.getCell(3).setCellValue("Location Name");
    xssfrow.getCell(4).setCellValue("Year");
    xssfrow.getCell(5).setCellValue("Month");
    xssfrow.getCell(6).setCellValue("MP Agri Land Lowest");
    xssfrow.getCell(7).setCellValue("MP Agri Land Highest");
    xssfrow.getCell(8).setCellValue("MP Plot Lowest");
    xssfrow.getCell(9).setCellValue("MP Plot Highest");
    xssfrow.getCell(10).setCellValue("MP Residential Lowest");
    xssfrow.getCell(11).setCellValue("MP Residential Highest");
    xssfrow.getCell(12).setCellValue("MP Commercial Lowest");
    xssfrow.getCell(13).setCellValue("MP Commercial Highest");
    xssfrow.getCell(14).setCellValue("Safedeal Zone Id");
    xssfrow.getCell(15).setCellValue("Location Type Id");
    xssfrow.getCell(16).setCellValue("Location Categories");
    xssfrow.getCell(17).setCellValue("Description");
    xssfrow.getCell(18).setCellValue("Major Approach Road");
    xssfrow.getCell(19).setCellValue("Source of Water");
    xssfrow.getCell(20).setCellValue("Public Transport");
    xssfrow.getCell(21).setCellValue("Advantage");
    xssfrow.getCell(22).setCellValue("Disadvantage");
    xssfrow.getCell(23).setCellValue("Population");
    xssfrow.getCell(24).setCellValue("Migration Rate");
    xssfrow.getCell(25).setCellValue("Commercial Center");

    String fileName = "/RawMarketPriceMasterData.xls";
    String exportPath = attachmentUtils.getRawMarketPriceExportAttachmentRootDirectory() + fileName;

    logger.info("exportPath" + exportPath);

    int rowCount = 0;

    for (RawMarketPrice aRawMarketPrice : rmp) {
        Row row = sheet.createRow(++rowCount);
        writeBook(aRawMarketPrice, row);
    }

    try (FileOutputStream outputStream = new FileOutputStream(exportPath)) {
        workbook.write(outputStream);
    }

    return true;
}

From source file:com.xidu.framework.common.view.AbstractExcel2007View.java

License:Open Source License

/**
 * Renders the Excel view, given the specified model. {@inheritDoc}
 * overridden:/*from  ww  w. j a va 2 s. c  o  m*/
 * 
 * @Date : 2011-4-14
 * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map,
 *      javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 * 
 */
@Override
protected final void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    XSSFWorkbook workbook;
    if (this.url != null) {
        workbook = getTemplateSource(this.url, request);
    } else {
        workbook = new XSSFWorkbook();
        logger.debug("Created Excel Workbook from scratch");
    }

    buildExcelDocument(model, workbook, request, response);

    // Set the content type.
    response.setContentType(getContentType());

    // Should we set the content length here?
    // response.setContentLength(workbook.getBytes().length);

    // Flush byte array to servlet output stream.
    ServletOutputStream out = response.getOutputStream();
    workbook.write(out);
    out.flush();
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

/**
 * ?xlsxExcel// w w  w. j a  v a  2s . c  o  m
 * @param heads 
 * @param data ?
 * @param sheetName Excel?
 * @param out ?
 * @return ByteArrayOutputStream
 * @throws IOException
 */
public static void writeToXLSX(ExcelRow heads, ExcelData data, String sheetName, ByteArrayOutputStream out)
        throws IOException {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet1 = wb.createSheet(sheetName);
    XSSFRow row = sheet1.createRow(0);
    XSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    XSSFCell cell;
    for (int i = 0; i < heads.size(); i++) {
        cell = row.createCell(i);
        cell.setCellValue(heads.get(i));
        cell.setCellStyle(style);
    }

    if (data != null && data.size() != 0) {
        for (int n = 0; n < data.size(); n++) {
            row = sheet1.createRow(n + 1);
            ExcelRow datarow = data.get(n);
            for (int m = 0; m < datarow.size(); m++) {
                cell = row.createCell(m);
                cell.setCellValue(datarow.get(m));
                cell.setCellStyle(style);
            }
        }
    }
    wb.write(out);
}