Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook write

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook write

Introduction

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

Prototype

private void write(POIFSFileSystem fs) throws IOException 

Source Link

Document

Writes the workbook out to a brand new, empty POIFS

Usage

From source file:com.bdx.rainbow.spsy.controller.syjg.LicenseController.java

@RequestMapping("/excelOut")
public void companyInfoExcelOut(HttpServletRequest request, HttpServletResponse response) {
    DubboEnterpriseLicense condition = new DubboEnterpriseLicense();
    condition.setEnterpriseName(request.getParameter("enterpriseName"));
    condition.setLicenseCode(request.getParameter("licenseCode"));
    condition.setOrganizationCode(request.getParameter("organizationCode"));
    if (StringUtils.isNotBlank(request.getParameter("validDateStart"))) {
        condition.setValidDateStart(DateUtil.getTimestamp(request.getParameter("validDateStart")));
    }/* w  w  w  .  j  ava 2  s.  c om*/
    if (StringUtils.isNotBlank(request.getParameter("validDateEnd"))) {
        condition.setValidDateEnd(DateUtil.getTimestamp(request.getParameter("validDateEnd")));
    }
    String type = request.getParameter("type") == null ? "" : request.getParameter("type");
    if ("0".equals(type)) {
        condition.setInvalidDateStart(DateUtil.getCurrent());
    } else if ("1".equals(type)) {
        condition.setInvalidDateStart(DateUtil.getCurrent());
        condition.setInvalidDateEnd(new Timestamp(DateUtil.addMonth(new Date(), 1).getTime()));
    } else if ("-1".equals(type)) {
        condition.setInvalidDateEnd(DateUtil.getCurrent());
    }
    Map<String, Object> resultMap = new HashMap<String, Object>();
    try {
        resultMap = licenseService.getLicenses(condition, -1, 0);
        String title = "???";
        String[] headers = { "???", "?", "", "??", "??",
                "???", "", "??" };
        HSSFWorkbook wb = licenseService.ExcelOut(title, headers,
                (List<DubboEnterpriseLicense>) resultMap.get("list"), null);
        response.setContentType("application/vnd.ms-excel");
        String fileName = "licenses.xls";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    } catch (Exception e) {
        log.debug(e.getMessage(), e);
    }
}

From source file:com.bean.UserBean.java

public void gerarExcel() {

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet firstSheet = workbook.createSheet("Aba1");

    HSSFRow row = firstSheet.createRow(0);
    HSSFCell cell = row.createCell(0);/*w w w.j ava 2s.  c o m*/
    cell.setCellValue("Primeiro valor");
    cell = row.createCell(1);
    cell.setCellValue("Segundo valor");

    row = firstSheet.createRow(1);
    cell = row.createCell(0);
    cell.setCellValue("Segunda linha");

    try {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.setResponseContentType("application/vnd.ms-excel");
        externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"my.xls\"");

        workbook.write(externalContext.getResponseOutputStream());
        facesContext.responseComplete();

    } catch (Exception e) {
        System.out.println(e);
    }

}

From source file:com.beetle.framework.web.controller.DocumentController.java

License:LGPL

private void createExcel(OutputStream out, IDocument doc, WebInput wi) throws IOException, ServletException {
    this.setContentType("application/vnd.ms-excel");
    HSSFWorkbook wb = new HSSFWorkbook();
    DocInfo di = new DocInfo(IDocument.TYPE_MS_EXCEL);
    try {//from   w  ww.  j a v  a2 s.c  o  m
        di.setExcelDocument(wb);
        doc.createContent(wi, di);
        wb.write(out);
    } catch (Exception e) {
        throw new ServletException(e);
    } finally {
        if (out != null) {
            out.flush();
            out.close();
            out = null;
        }
        di = null;
        doc = null;
        wb = null;
    }
}

From source file:com.bharath.dao.SelectCourseDao.java

public String getStudentslisttoExcel(String curl) {
    ArrayList<String> arr = new ArrayList<String>();
    ArrayList<String> arr1 = new ArrayList<String>();
    ArrayList<String> arr2 = new ArrayList<String>();
    ArrayList<String> arr3 = new ArrayList<String>();

    try {//from  ww  w  .  ja  v  a2s.c o  m
        UserAgent userAgent = new UserAgent();
        userAgent.openContent(curl);
        //userAgent.visit(curl);

        Table table = userAgent.doc.getTable(
                "<table class=\"datadisplaytable\" summary=\"This table displays a list of students registered for the course; summary information about each student is provided.\" width=\"100%\">");
        System.out.println(table.toString());
        Elements elements = table.getCol(0);
        for (Element element : elements) {
            arr.add(element.innerText());
            //System.out.println(element.innerText());
        }

        Elements elements1 = table.getCol(2);
        for (Element element : elements1) {
            arr1.add(element.innerText());
            //System.out.println(element.innerText());
        }
        Elements elements2 = table.getCol(3);
        for (Element element : elements2) {
            arr2.add(element.innerText());
            //System.out.println(element.innerText());
        }
        Elements elements3 = table.getCol(4);
        for (Element element : elements3) {
            arr3.add(element.innerText());
            // System.out.println(element.innerText());
        }

        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet spreadSheet = workBook.createSheet("StudentDetails");
        HSSFRow row;
        HSSFCell cell;
        //int colnum = 0;

        int rowcount = 0;
        //for (int i = 0; i < arr.size(); i++) {
        for (String str : arr) {

            row = spreadSheet.createRow((short) rowcount);
            // System.out.println(arr.get(i));
            cell = row.createCell(0);
            cell.setCellValue(str.trim());
            rowcount++;
            //  colcount++;
        }
        rowcount = 0;
        for (String str : arr1) {

            row = spreadSheet.getRow((short) rowcount);
            // System.out.println(arr.get(i));
            cell = row.createCell(1);
            cell.setCellValue(str.trim());
            rowcount++;
            //  colcount++;
        }
        rowcount = 0;
        for (String str : arr2) {

            row = spreadSheet.getRow((short) rowcount);
            // System.out.println(arr.get(i));
            cell = row.createCell(2);
            cell.setCellValue(str.trim());
            rowcount++;
            //  colcount++;
        }
        rowcount = 0;
        for (String str : arr3) {

            row = spreadSheet.getRow((short) rowcount);
            // System.out.println(arr.get(i));
            cell = row.createCell(3);
            cell.setCellValue(str.trim());
            rowcount++;
            //  colcount++;
        }

        ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
        workBook.write(outByteStream);
        outArray = outByteStream.toByteArray();
        System.out.println(outArray.length);
        //fos = new FileOutputStream("student.xls");
        System.out.println("Done");
        //workBook.write(fos);

    } catch (Exception e) {
        System.out.println(e);
    }
    return "Done";

}

From source file:com.binlist.binlistreader.BinlistReader.java

public static void main(String[] args) {
    String result[] = null;// w w  w .java  2s . co  m

    String folder = "/opt/";
    String sourceFileName = "binlist.xls";
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(folder + sourceFileName);

        HSSFWorkbook workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        Iterator<Cell> cellIterator = null;
        Row row = null;
        Cell cell = null;
        int cellNo = 0;
        String binno = "";
        String longUrl = "";
        String shortUrl = "";
        int rownum = 0;
        while (rowIterator.hasNext()) {
            rownum++;
            if (rownum == 1)
                continue;

            row = rowIterator.next();
            cellIterator = row.cellIterator();
            cellNo = 0;
            binno = "";
            longUrl = "";
            shortUrl = "";

            cell = row.getCell(4);
            if (cell != null && cell.getStringCellValue() != null) {
                binno = cell.getStringCellValue();
            }

            if (binno != null && binno.length() > 5) {
                result = postRequestV3(binno);
                System.out.println("rownum..:" + rownum + " binno..:" + binno + " result..:" + result.length);
                if (result != null) {
                    row.getCell(5).setCellValue(checkNull(result[0], ""));
                    row.getCell(6).setCellValue(checkNull(result[1], ""));
                    row.getCell(7).setCellValue(checkNull(result[2], ""));
                }
            }

        }

        System.out.println("rownum..:" + rownum);
        fis.close();
        FileOutputStream out = new FileOutputStream(folder + "newfile/" + sourceFileName);
        workbook.write(out);
        out.close();

        workbook.close();
        workbook = null;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(BinlistReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        fis = null;
    }

}

From source file:com.byd.test.actions.OrderAction.java

@RequestMapping("xopera")
public ModelAndView xopera(HttpServletRequest request, HttpServletResponse response) {
    try {/*  w  w  w .j a v  a2 s. c  o  m*/
        OutputStream out = response.getOutputStream();
        response.reset();
        response.setHeader("content-disposition", "attachment;filename=" + "fileName.xls");
        response.setContentType("application/x-octetstream;charset=gb2312");
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = null;
        String sheetName = "first sheet";
        sheet = workbook.createSheet(sheetName);
        System.out.println("*-*-----------------------");
        HSSFCell cell = sheet.createRow(1).createCell((short) 2);
        cell.setCellValue(323);
        workbook.write(out);
        out.flush();
        out.close();

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

    String s = (String) request.getParameter("plantCode");//String a = request.getQueryString("");
    System.out.println("plantCode:  " + s);
    return new ModelAndView("myname");
}

From source file:com.carfinance.module.common.controller.DocumentDownloadController.java

/**
 * ????Excel//from   w ww.  ja v  a 2  s.co  m
 * @param model
 * @param request
 * @param response
 */
@RequestMapping(value = "/cashierstatement", method = RequestMethod.GET)
public void cashierStatement(Model model, HttpServletRequest request, HttpServletResponse response) {
    String contrace_id_str = request.getParameter("contrace_id");

    String contrace_no = "";
    String customer_name = "";
    String customer_no = "";
    String daily_available_km = "";
    String contrace_type = "";

    VehicleContraceInfo vehicleContraceInfo = this.vehicleServiceManageService
            .getVehicleContraceInfoById(Long.valueOf(contrace_id_str));
    if (vehicleContraceInfo != null) {
        contrace_no = vehicleContraceInfo.getContrace_no();
        customer_name = vehicleContraceInfo.getCustomer_name();
        daily_available_km = vehicleContraceInfo.getDaily_available_km() + "";
    } else {
        PropertyContraceInfo propertyContraceInfo = this.vehicleServiceManageService
                .getPropertyContraceInfoById(Long.valueOf(contrace_id_str));
        if (propertyContraceInfo != null) {
            contrace_no = propertyContraceInfo.getContrace_no();
            customer_name = propertyContraceInfo.getCustomer_name();
            contrace_type = "?";
        }
    }

    List<VehicleContraceVehsInfo> vehicleContraceVehsInfoList = this.vehicleServiceManageService
            .getVehicleContraceVehsListByContraceId(Long.valueOf(contrace_id_str));

    //1.ContentType
    response.setContentType("multipart/form-data");
    //2.????(??a.pdf)
    response.setHeader("Content-Disposition", "attachment;fileName=" + contrace_no + ".xls");

    // webbookExcel
    HSSFWorkbook wb = new HSSFWorkbook();

    for (VehicleContraceVehsInfo v : vehicleContraceVehsInfoList) {
        // webbooksheet,Excelsheet
        HSSFSheet sheet = wb.createSheet(v.getLicense_plate());
        // sheet0,??poiExcel?short
        HSSFRow row = sheet.createRow((int) 0);
        // ? 
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?

        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("");
        cell.setCellStyle(style);

        row = sheet.createRow(1);
        row.createCell((short) 0).setCellValue(customer_name);
        row.createCell((short) 1).setCellValue(customer_no);
        row.createCell((short) 2).setCellValue(contrace_type);
        row.createCell((short) 3).setCellValue("");
        row.createCell((short) 4).setCellValue("");

        row = sheet.createRow(2);
        HSSFCell cell2 = row.createCell((short) 0);
        cell2.setCellValue("?");
        cell2.setCellStyle(style);
        cell2 = row.createCell((short) 1);
        cell2.setCellValue("??");
        cell2.setCellStyle(style);
        cell2 = row.createCell((short) 2);
        cell2.setCellValue("");
        cell2.setCellStyle(style);
        cell2 = row.createCell((short) 3);
        cell2.setCellValue("");
        cell2.setCellStyle(style);
        cell2 = row.createCell((short) 4);
        cell2.setCellValue("??");
        cell2.setCellStyle(style);

        row = sheet.createRow(3);
        row.createCell((short) 0).setCellValue(v.getKm());
        row.createCell((short) 1).setCellValue(v.getOil_percent());
        long over_km = (v.getReturn_km() - v.getKm()) > 0 ? (v.getReturn_km() - v.getKm()) : 0;
        row.createCell((short) 2).setCellValue(over_km);
        row.createCell((short) 3).setCellValue("");
        row.createCell((short) 4).setCellValue("");

        row = sheet.createRow(4);
        HSSFCell cell4 = row.createCell((short) 0);
        cell4.setCellValue("");
        cell4.setCellStyle(style);
        cell4 = row.createCell((short) 1);
        cell4.setCellValue("?");
        cell4.setCellStyle(style);
        cell4 = row.createCell((short) 2);
        cell4.setCellValue("");
        cell4.setCellStyle(style);
        cell4 = row.createCell((short) 3);
        cell4.setCellValue("");
        cell4.setCellStyle(style);
        cell4 = row.createCell((short) 4);
        cell4.setCellValue("");
        cell4.setCellStyle(style);

        row = sheet.createRow(5);
        row.createCell((short) 0).setCellValue(v.getReturn_km());
        row.createCell((short) 1).setCellValue(v.getRevert_oil_percent());
        long over_oil = (v.getOil_percent() - v.getRevert_oil_percent()) > 0
                ? (v.getOil_percent() - v.getRevert_oil_percent())
                : 0;
        row.createCell((short) 2).setCellValue(over_oil);
        row.createCell((short) 3).setCellValue("");
        row.createCell((short) 4).setCellValue(v.getActually_price());
    }

    // ?
    try {

        String path = appProps.get("contrace.over.download.path") + contrace_no + ".xls";
        FileOutputStream fout = new FileOutputStream(path);
        wb.write(fout);
        fout.close();

        ServletOutputStream out;
        //File(?download.pdf)
        File file = new File(path);

        try {
            FileInputStream inputStream = new FileInputStream(file);
            //3.response?ServletOutputStream(out)
            out = response.getOutputStream();
            int b = 0;
            byte[] buffer = new byte[512];
            while (b != -1) {
                b = inputStream.read(buffer);
                //4.?(out)
                out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.cimmyt.reports.impl.ServiceReportCustomQueryImpl.java

License:Apache License

@Override
public byte[] getBytesCustomQuery(CustomQueryReport bean) {
    HSSFWorkbook objWB = createBookExcel(bean);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from  w w w  .  j  av  a  2  s.co  m*/
        objWB.write(baos);
        baos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    InputStream is = bais;
    byte[] b = null;
    try {
        b = this.inputStreamToBytes(is);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return b;
}

From source file:com.cimmyt.reports.impl.ServiceReportKBioImpl.java

License:Apache License

/**
 * Method that return a HSSFWorkbook to byte []
 * @param objWB//  w ww .j av  a2s  .co  m
 * @return
 */
private byte[] getArryByte(HSSFWorkbook objWB) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        objWB.write(baos);
        baos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    InputStream is = bais;
    byte[] b = null;
    try {
        b = this.inputStreamToBytes(is);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return b;
}

From source file:com.cimmyt.reports.impl.ServiceReportLaboratoryImpl.java

License:Apache License

private byte[] getArryByte(HSSFWorkbook objWB) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {//from   w w w .  ja v  a 2s.  com
        objWB.write(baos);
        baos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    InputStream is = bais;
    byte[] b = null;
    try {
        b = this.inputStreamToBytes(is);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return b;
}