您的位置:首页 > 其它

excel文件导出

2016-07-27 16:36 309 查看
  import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    /**

     * 供应商导出

     *

     * @param request

     * @param response

     * @return

     * @throws Exception

     */

    @SecurityMapping(title = "supplier_export.htm供应商导出", value = "/erp/supplier_export.htm*", rtype = "seller", rname = "supplier供应商导出", rcode = "supplier_user", display = false, rgroup = "supplier管理")

    @RequestMapping("/erp/supplier_export.htm")

    @ResponseBody

    public void supplier_export(HttpServletRequest request, HttpServletResponse response, String ids) throws Exception {

        Long userId = SecurityUserHolder.getConditionId();

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet JobSheet = workbook.createSheet("供应商数据");

        this.supplierService.produceJobSheet(workbook, JobSheet, userId, ids);// 有测试数据提交小心

        Date date = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");

        String fileName = "供应商数据" + sdf.format(date) + ".xls";

        // 执行文件输出

        this.runExcelFileExport(response, workbook, fileName);

    }

  /**

     * 创建excel2003

     *

     * @param response

     * @param wb

     * @param fileName

     *            文件名

     */

    public void runExcelFileExport(HttpServletResponse response, HSSFWorkbook wb, String fileName) {

        try {

            ServletOutputStream sos = response.getOutputStream();

            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            wb.write(buffer);

            response.setContentType("application/vnd.ms-excel");

            response.setContentLength(buffer.size());

            response.setHeader("Content-Disposition",

                    "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));

            response.setHeader("Pragma", "public");

            response.setHeader("Cache-Control", "max-age=0");

            sos.write(buffer.toByteArray());

            buffer.flush();

            sos.flush();

        } catch (IOException e) {

            e.printStackTrace();

        }
    }

    /**

     * 创建excel

     *

     * @param workbook

     * @param sheet

     * @throws Exception

     */

    @Override

    public void produceJobSheet(HSSFWorkbook workbook, HSSFSheet sheet, Long userId, String ids) throws Exception {

        HSSFRow row1 = sheet.createRow(0);// 创建行1

        sheet.setColumnWidth(0, 6000);// 设置列宽

        sheet.setColumnWidth(1, 6000);// 设置列宽

        sheet.setColumnWidth(2, 6000);// 设置列宽

        sheet.setColumnWidth(3, 6000);// 设置列宽

        sheet.setColumnWidth(4, 6000);// 设置列宽

        sheet.setColumnWidth(5, 6000);// 设置列宽

        HSSFCell code1 = row1.createCell(0);

        code1.setCellValue("供应商编号");

        HSSFCell sequence1 = row1.createCell(1);

        sequence1.setCellValue("供应商序号");

        HSSFCell name1 = row1.createCell(2);

        name1.setCellValue("供应商名称");

        HSSFCell contacts1 = row1.createCell(3);

        contacts1.setCellValue("联系人");

        HSSFCell phoneNum1 = row1.createCell(4);

        phoneNum1.setCellValue("联系电话");

        HSSFCell address1 = row1.createCell(5);

        address1.setCellValue("地址信息");

        List<Supplier> supplier = this.supplierDao.produceJobSheet(userId, ids);

        int i = 1;

        for (Supplier su : supplier) {

            HSSFRow row = sheet.createRow(i);// 创建行1

            sheet.setColumnWidth(0, 6000);// 设置列宽

            sheet.setColumnWidth(1, 6000);// 设置列宽

            sheet.setColumnWidth(2, 6000);// 设置列宽

            sheet.setColumnWidth(3, 6000);// 设置列宽

            sheet.setColumnWidth(4, 6000);// 设置列宽

            sheet.setColumnWidth(5, 6000);// 设置列宽

            HSSFCell code = row.createCell(0);

            code.setCellValue(su.getCode());

            HSSFCell sequence = row.createCell(1);

            sequence.setCellValue(su.getSequence());

            HSSFCell name = row.createCell(2);

            name.setCellValue(su.getName());

            HSSFCell contacts = row.createCell(3);

            contacts.setCellValue(su.getContacts());

            HSSFCell phoneNum = row.createCell(4);

            phoneNum.setCellValue(su.getPhoneNum());

            HSSFCell address = row.createCell(5);

            address.setCellValue(su.getAddress());

            i++;

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: