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.efficio.fieldbook.web.nursery.service.impl.ExcelExportStudyServiceImpl.java

License:Open Source License

@Override
public void export(Workbook workbook, String filename) {
    FileOutputStream fos = null;//from   w  w  w  .  j  ava  2 s .c o m

    try {
        HSSFWorkbook xlsBook = new HSSFWorkbook();

        writeDescriptionSheet(xlsBook, workbook);
        writeObservationSheet(xlsBook, workbook);

        fos = new FileOutputStream(new File(filename));
        xlsBook.write(fos);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.eryansky.core.excelTools.JsGridReportBase.java

License:Apache License

/**
 * Excel(?)//from   w w  w  . ja v a  2s.  com
 * 
 * @param title
 *            ??
 * @param creator
 *            
 * @param tableData
 *            ?
 * @return void <style name="dataset"> case SYSROWNUM%2==0?#row0:#row1;
 *         fontsize:9px; </style> <style name="row0"> import(parent);
 *         bgcolor:#FFFFFF; </style> <style name="row1"> import(parent);
 *         bgcolor:#CAEAFE; </style>
 */
public void exportToExcel(String title, String creator, TableData tableData) throws Exception {

    HSSFWorkbook wb = new HSSFWorkbook();// Excel 
    HashMap<String, HSSFCellStyle> styles = initStyles(wb);// ??

    wb = writeSheet(wb, title, styles, creator, tableData);//

    String sFileName = title + ".xls";
    WebUtils.setDownloadableHeader(request, response, sFileName);
    response.setHeader("Connection", "close");
    response.setHeader("Content-Type", WebUtils.EXCEL_TYPE);

    wb.write(response.getOutputStream());
}

From source file:com.eryansky.core.excelTools.JsGridReportBase.java

License:Apache License

/**
 * Excel()/*from  w  w w.j  a va 2 s  .co m*/
 * 
 * @param title
 *            ??
 * @param creator
 *            
 * @param tableDataLst
 *            ??(??tableData?sheet???)
 * @return void <style name="dataset"> case SYSROWNUM%2==0?#row0:#row1;
 *         fontsize:9px; </style> <style name="row0"> import(parent);
 *         bgcolor:#FFFFFF; </style> <style name="row1"> import(parent);
 *         bgcolor:#CAEAFE; </style>
 */
public void exportToExcel(String title, String creator, List<TableData> tableDataLst) throws Exception {

    HSSFWorkbook wb = new HSSFWorkbook();// Excel 
    HashMap<String, HSSFCellStyle> styles = initStyles(wb);// ??

    int i = 1;
    for (TableData tableData : tableDataLst) {
        String sheetTitle = tableData.getSheetTitle();
        sheetTitle = sheetTitle == null || sheetTitle.equals("") ? "sheet" + i : sheetTitle;
        wb = writeSheet(wb, tableData.getSheetTitle(), styles, creator, tableData);//
        i++;
    }

    String sFileName = title + ".xls";
    WebUtils.setDownloadableHeader(request, response, sFileName);
    response.setHeader("Connection", "close");
    response.setHeader("Content-Type", "application/vnd.ms-excel");

    wb.write(response.getOutputStream());
}

From source file:com.eryansky.modules.disk.utils.DiskUtils.java

License:Apache License

/**
 * ?// w w w. jav a 2 s. c  om
 *
 * @param folderCode
 *            ?
 * @param sessionInfo
 *            session? ??null
 *
 * @param workbook
 * @param fileName
 *
 * @return
 * @throws InvalidExtensionException
 * @throws FileUploadBase.FileSizeLimitExceededException
 * @throws FileNameLengthLimitExceededException
 * @throws IOException
 */
public static File saveExcelFile(String folderCode, SessionInfo sessionInfo, HSSFWorkbook workbook,
        String fileName) throws InvalidExtensionException, FileUploadBase.FileSizeLimitExceededException,
        FileNameLengthLimitExceededException, IOException {
    String userId = null;
    if (sessionInfo != null && sessionInfo.getUserId() != null) {
        userId = sessionInfo.getUserId();
    }

    String code = FileUploadUtils.encodingFilenamePrefix(userId + "", fileName);
    Folder folder = getSystemFolderByCode(folderCode, userId);
    String storeFilePath = iFileManager.getStorePath(folder, userId, fileName);
    File file = new File();
    file.setFolder(folder);
    file.setCode(code);
    file.setUserId(userId);
    file.setName(fileName);
    file.setFilePath(storeFilePath);
    file.setFileSize(Long.valueOf(workbook.getBytes().length));
    file.setFileSuffix(FilenameUtils.getExtension(fileName));
    FileOutputStream fileOut = new FileOutputStream(storeFilePath);
    workbook.write(fileOut);
    fileOut.close();
    //        iFileManager.saveFile(file.getFilePath(),multipartFile.getInputStream(), true);
    diskManager.saveFile(file);
    return file;
}

From source file:com.eryansky.modules.sys.web.BugController.java

License:Apache License

/**
 * Excel//w  ww .  j a v a 2 s.c  o  m
 */
@SuppressWarnings("unchecked")
@RequestMapping(value = { "exportExcel" })
public void exportExcel(HttpServletRequest request, HttpServletResponse response, HttpSession session)
        throws Exception {
    // ????
    final String fileName = "?.xls";
    OutputStream outStream = null;
    try {
        //
        response.setContentType(WebUtils.EXCEL_TYPE);
        //?
        WebUtils.setDownloadableHeader(request, response, fileName);
        //session??
        List<PropertyFilter> sessionFilters = (List<PropertyFilter>) session.getAttribute(SSSION_SEARCH);
        List<Bug> bugs = null;
        if (sessionFilters != null) {
            bugs = bugManager.find(sessionFilters, "orderNo", Page.ASC);
        } else {
            bugs = bugManager.getAll("id", Page.ASC);
        }
        //bugBug?Dictionary??
        for (Bug bug : bugs) {
            String dicStringName = "";
            if (StringUtils.isNotBlank(bug.getType())) {
                dicStringName = DictionaryUtils.getDictionaryNameByDV(DictionaryUtils.DIC_BUG, bug.getType(),
                        null);
                bug.setTypeName(dicStringName);
            }

        }
        HSSFWorkbook workbook = new ExportExcel<Bug>().exportExcel("?", Bug.class, bugs);
        outStream = response.getOutputStream();
        workbook.write(outStream);
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outStream.flush();
            outStream.close();
        } catch (IOException e) {

        }
    }
}

From source file:com.esd.cs.common.PoiCreateExcel.java

License:Open Source License

/**
 * ?//from ww w  . ja va  2s .  c  o m
 * 
 * @param FilePath
 * @param workerList
 * @return
 */
public static boolean createExcel(String FilePath, List<WorkerTemp> workerList) {
    // Excel Workbook,excel
    HSSFWorkbook wb = new HSSFWorkbook();
    // Excelsheet,exceltab
    HSSFSheet sheet = wb.createSheet("sheet1");
    // excel?
    sheet.setColumnWidth(0, 4000);
    sheet.setColumnWidth(1, 3500);

    // Excel?
    HSSFRow headRow = sheet.createRow(0);
    HSSFCell headell = headRow.createCell(0);
    // ???
    headell = headRow.createCell(0);
    headell.setCellValue("??");
    headell = headRow.createCell(1);
    headell.setCellValue("??");
    sheet.setColumnWidth(1, 8000);
    headell = headRow.createCell(2);
    headell.setCellValue("");
    sheet.setColumnWidth(2, 13000);

    for (int i = 1; i <= workerList.size(); i++) {
        WorkerTemp worker = workerList.get(i - 1);
        // Excel?
        HSSFRow row = sheet.createRow(i);
        HSSFCell cell = row.createCell(0);
        // ???
        cell = row.createCell(0);
        cell.setCellValue(worker.getWorkerName());
        cell = row.createCell(1);
        cell.setCellValue(worker.getWorkerHandicapCode());
        cell = row.createCell(2);
        cell.setCellValue(worker.getRemark());
    }
    try {
        FileOutputStream os = new FileOutputStream(FilePath);
        wb.write(os);
        os.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:com.esd.cs.common.PoiCreateExcel.java

License:Open Source License

/**
 * ???/*from   w  w w  .  j  a  va  2 s  .c o m*/
 * 
 * @param FilePath
 * @param companyList
 * @return
 */
public static boolean createComapnyExcel(String FilePath, List<Company> companyList) {
    // Excel Workbook,excel
    HSSFWorkbook wb = new HSSFWorkbook();
    // Excelsheet,exceltab
    HSSFSheet sheet = wb.createSheet("sheet1");
    // excel?
    sheet.setColumnWidth(0, 4000);
    sheet.setColumnWidth(1, 3500);

    // Excel?
    HSSFRow headRow = sheet.createRow(0);
    HSSFCell headell = headRow.createCell(0);
    // ???
    headell = headRow.createCell(0);
    headell.setCellValue("?");
    headell = headRow.createCell(1);
    headell.setCellValue("?");
    headell = headRow.createCell(2);
    headell.setCellValue("????");
    sheet.setColumnWidth(2, 12000); // 

    headell = headRow.createCell(3);
    headell.setCellValue("");
    headell = headRow.createCell(4);
    headell.setCellValue("?");

    headell = headRow.createCell(5);
    headell.setCellValue("???");

    headell = headRow.createCell(6);
    headell.setCellValue("??");

    headell = headRow.createCell(7);
    headell.setCellValue("");
    headell = headRow.createCell(8);
    headell.setCellValue("???");
    sheet.setColumnWidth(8, 12000);

    for (int i = 1; i <= companyList.size(); i++) {
        Company company = companyList.get(i - 1);
        // Excel?
        HSSFRow row = sheet.createRow(i);
        HSSFCell cell = row.createCell(0);
        // ???
        // ?
        cell = row.createCell(0);
        cell.setCellValue(company.getCompanyCode());
        // ?
        cell = row.createCell(1);
        cell.setCellValue(company.getCompanyTaxCode());
        // ???
        cell = row.createCell(2);
        cell.setCellValue(company.getCompanyName());
        // 
        cell = row.createCell(3);
        cell.setCellValue(company.getCompanyLegal());
        // ?
        cell = row.createCell(4);
        cell.setCellValue(company.getCompanyContactPerson());
        // ???
        cell = row.createCell(5);
        cell.setCellValue(company.getCompanyPhone());
        // ??
        cell = row.createCell(6);
        cell.setCellValue(company.getCompanyMobile());
        // 
        cell = row.createCell(7);
        cell.setCellValue(company.getCompanyZipCode());
        // ???
        cell = row.createCell(8);
        cell.setCellValue(company.getCompanyAddress());
    }
    try {
        FileOutputStream os = new FileOutputStream(FilePath);
        wb.write(os);
        os.flush();
        os.close();
        companyList.clear();
        companyList = null;
        os = null;
        wb = null;
        System.gc();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:com.esd.cs.common.PoiCreateExcel.java

License:Open Source License

/**
 * //from  w  w w. j  a  v a2s .co  m
 * 
 * @param FilePath
 * @param companyList
 * @return
 */
public static boolean createRepeaExcel(String FilePath, List<ReportViewModel> companyList, ReportModel model) {
    // Excel Workbook,excel
    HSSFWorkbook wb = new HSSFWorkbook();
    // Excelsheet,exceltab
    HSSFSheet sheet = wb.createSheet("sheet1");
    // excel?
    sheet.setColumnWidth(0, 4000);
    sheet.setColumnWidth(1, 3500);

    // Excel?
    HSSFRow headRow0 = sheet.createRow(0);
    HSSFCell headCell = headRow0.createCell(0);
    // ??
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 13));// ???
    headCell = headRow0.createCell(0);
    // 
    headCell.setCellValue(model.getTitle());
    // ?
    HSSFCellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(HSSFColor.GREEN.index);
    style.setAlignment(CellStyle.ALIGN_CENTER);// 
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 
    // 
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 12);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 
    style.setFont(font);
    headCell.setCellStyle(style);

    // ? ??
    HSSFRow RowTow = sheet.createRow(1);
    HSSFCell CellTow = headRow0.createCell(1);
    // ??
    sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 5));// ???
    CellTow = RowTow.createCell(0);
    // 
    CellTow.setCellValue(model.getCreateCompany());

    // ? 
    // ??
    sheet.addMergedRegion(new CellRangeAddress(1, 1, 6, 13));// ???
    CellTow = RowTow.createCell(6);
    HSSFCellStyle style1 = wb.createCellStyle();
    style1.setFillBackgroundColor(HSSFColor.GREEN.index);
    style1.setAlignment(CellStyle.ALIGN_RIGHT);// ?
    CellTow.setCellStyle(style1);
    // 
    CellTow.setCellValue(model.getCreateData());

    // ?
    HSSFRow headRow = sheet.createRow(2);
    HSSFCell headell = headRow.createCell(2);
    // ???
    headell = headRow.createCell(0);
    headell.setCellValue(model.getType());

    headell = headRow.createCell(1);
    headell.setCellValue("??");

    headell = headRow.createCell(2);
    headell.setCellValue("??");
    sheet.setColumnWidth(2, 3000); // 

    headell = headRow.createCell(3);
    headell.setCellValue("???");
    sheet.setColumnWidth(3, 3000); // 

    headell = headRow.createCell(4);
    headell.setCellValue("???");
    sheet.setColumnWidth(4, 4000); // 

    headell = headRow.createCell(5);
    headell.setCellValue("???");
    sheet.setColumnWidth(5, 4000); // 

    headell = headRow.createCell(6);
    headell.setCellValue("???");
    sheet.setColumnWidth(6, 4500); // 

    headell = headRow.createCell(7);
    headell.setCellValue("");
    sheet.setColumnWidth(8, 4000);

    headell = headRow.createCell(8);
    headell.setCellValue("");
    sheet.setColumnWidth(8, 4000);

    headell = headRow.createCell(9);
    headell.setCellValue("");
    sheet.setColumnWidth(9, 4000);

    headell = headRow.createCell(10);
    headell.setCellValue("?");
    sheet.setColumnWidth(10, 4000);

    headell = headRow.createCell(11);
    headell.setCellValue("???");

    headell = headRow.createCell(12);
    headell.setCellValue("?");

    headell = headRow.createCell(13);
    headell.setCellValue("?");

    for (int i = 0; i < companyList.size(); i++) {
        ReportViewModel company = companyList.get(i);
        // Excel?
        HSSFRow row = sheet.createRow(i + 3);
        HSSFCell cell = row.createCell(i + 3);
        // ???
        // ????
        cell = row.createCell(0);
        cell.setCellValue(company.getReportName());
        // ??
        cell = row.createCell(1);
        cell.setCellValue(company.getUnitNum());
        // ??
        cell = row.createCell(2);
        cell.setCellValue(company.getEmpTotal());

        // ???
        cell = row.createCell(3);
        cell.setCellValue(company.getUnAudit());

        // ?, ???
        cell = row.createCell(4);
        cell.setCellValue(company.getUnReAudit());

        // ?, ??
        cell = row.createCell(5);
        cell.setCellValue(company.getAuditOk());

        // ?, ??
        cell = row.createCell(6);
        cell.setCellValue(company.getUnauditOk());

        // 
        cell = row.createCell(7);
        cell.setCellValue(company.getShouldTotal().toString());

        // ?
        cell = row.createCell(8);
        cell.setCellValue(company.getAlreadyTotal().toString());

        // 
        cell = row.createCell(9);
        cell.setCellValue(company.getLessTotal().toString());
        // ?
        cell = row.createCell(10);
        cell.setCellValue(company.getAmountPayable().toString());
        // ???
        cell = row.createCell(11);
        cell.setCellValue(company.getReductionAmount().toString());
        // ?
        cell = row.createCell(12);
        cell.setCellValue(company.getActualAmount().toString());
        // ?
        cell = row.createCell(13);
        cell.setCellValue(company.getAlreadyAmount().toString());
    }

    // ? 
    HSSFRow row = sheet.createRow(companyList.size() + 3);
    HSSFCell cell = row.createCell(companyList.size() + 3);
    // ???
    // ????
    sheet.addMergedRegion(new CellRangeAddress(companyList.size() + 3, companyList.size() + 3, 0, 13));// ???
    cell = row.createCell(0);
    // ?
    HSSFCellStyle styleFoot = wb.createCellStyle();
    styleFoot.setAlignment(CellStyle.ALIGN_RIGHT);// ?
    cell.setCellStyle(styleFoot);
    // 
    cell.setCellValue(model.getCreatePeople());

    try {
        FileOutputStream os = new FileOutputStream(FilePath);
        wb.write(os);
        os.flush();
        os.close();
        companyList.clear();
        companyList = null;
        os = null;
        wb = null;
        System.gc();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:com.esd.ps.excel.PoiCreateExcel.java

License:Open Source License

/**
 * ?//from   ww w. java 2s  . co  m
 * 
 * @param FilePath
 * @param companyList
 * @return
 */
public static boolean createRegistrationExcel(String FilePath, List<Registration> list) {
    // Excel Workbook,excel
    HSSFWorkbook wb = new HSSFWorkbook();
    // Excelsheet,exceltab
    HSSFSheet sheet = wb.createSheet("sheet1");
    // excel?
    sheet.setColumnWidth(0, 4000);
    sheet.setColumnWidth(1, 3500);

    // Excel?
    HSSFRow headRow = sheet.createRow(0);
    HSSFCell headell = headRow.createCell(0);
    // ???
    headell = headRow.createCell(0);
    headell.setCellValue("??");
    headell = headRow.createCell(1);
    headell.setCellValue("??");
    headell = headRow.createCell(2);
    headell.setCellValue("??");
    // sheet.setColumnWidth(2, 12000); // 

    headell = headRow.createCell(3);
    headell.setCellValue("QQ");
    headell = headRow.createCell(4);
    headell.setCellValue("??");

    headell = headRow.createCell(5);
    headell.setCellValue("??");
    SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATETIME_FORMAT);
    for (int i = 1; i <= list.size(); i++) {
        Registration r = list.get(i - 1);
        // Excel?
        HSSFRow row = sheet.createRow(i);
        HSSFCell cell = row.createCell(0);
        // ???
        // ??
        cell = row.createCell(0);
        cell.setCellValue(r.getName());
        // ??
        cell = row.createCell(1);
        cell.setCellValue(r.getCard());
        // ?
        cell = row.createCell(2);
        cell.setCellValue(r.getPhone());
        // QQ
        cell = row.createCell(3);
        cell.setCellValue(r.getQq());
        // ??
        cell = row.createCell(4);
        cell.setCellValue(r.getAddress());
        // ??
        cell = row.createCell(5);
        cell.setCellValue(sdf.format(r.getCreateTime()));
    }
    try {
        FileOutputStream os = new FileOutputStream(FilePath);
        wb.write(os);
        os.flush();
        os.close();
        list.clear();
        list = null;
        os = null;
        wb = null;
        System.gc();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:com.fota.devMgt.controller.DevstatExcelDown.java

License:Open Source License

@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    String date = DateTimeUtil.getCurrentDate();
    String formNm = "?";
    String fileName = date + "_" + formNm;
    String sheetName = "sheet1";

    response.setContentType("application/vnd.ms-excel;charset=UTF-8");
    response.setHeader("Content-disposition",
            "attachent; filename=" + new String((fileName).getBytes("KSC5601"), "8859_1") + ".xls");

    @SuppressWarnings("unchecked")
    List<DevSearchVO> resultData = (List<DevSearchVO>) model.get("resultData");

    HSSFSheet sheet = workbook.createSheet(sheetName);

    //  ?/* w  w w .ja va2  s  .c  o  m*/
    HSSFRow header = sheet.createRow(0);
    header.createCell(0).setCellValue("??");
    header.createCell(1).setCellValue("");
    header.createCell(2).setCellValue("");
    header.createCell(3).setCellValue("?");
    header.createCell(4).setCellValue("??");
    header.createCell(5).setCellValue("?");
    header.createCell(6).setCellValue("IMEI");
    header.createCell(7).setCellValue("CTN");
    header.createCell(8).setCellValue("FOTA");
    header.createCell(9).setCellValue("?");
    header.createCell(10).setCellValue("?");
    header.createCell(11).setCellValue("???");
    header.createCell(12).setCellValue("??");

    int index = 0;
    for (int i = 0; i < resultData.size(); i++) {
        DevSearchVO fvo = resultData.get(i);
        HSSFRow row = sheet.createRow(++index);
        row.createCell(0).setCellValue(fvo.getDevId());
        row.createCell(1).setCellValue(fvo.getBizNm());
        row.createCell(2).setCellValue(fvo.getSvcNm());
        row.createCell(3).setCellValue(fvo.getMakerNm());
        row.createCell(4).setCellValue(fvo.getDevModelNm());
        row.createCell(5).setCellValue(fvo.getClientNm());
        row.createCell(6).setCellValue("");
        row.createCell(7).setCellValue("");
        row.createCell(8).setCellValue(fvo.getFotaYn());
        row.createCell(9).setCellValue(fvo.getProcessStatCd());
        row.createCell(10).setCellValue(fvo.getLastAccDt());
        row.createCell(11).setCellValue(fvo.getDevAccTerm());
        row.createCell(12).setCellValue(fvo.getCretDt());
    }

    workbook.write(response.getOutputStream());
    response.getOutputStream().close();
}