记:使用POI导入导出Excel表格

记:使用POI导入导出Excel表格

导入依赖

	 <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.16</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>ooxml-schemas</artifactId>
        <version>1.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.16</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.6.0</version>
    </dependency>

导入导出excel

@Controller
@RequestMapping("/excel")
public class ImportExportController {

    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 导入excel文件
     */

    @RequestMapping("/import")
    public String importExcel(MultipartFile file){
        XSSFWorkbook workbook = null;
        try {

            //读取excel文件,读取excel的工作区间sheet。可以通过sheet的名字获得sheet,也可以通过sheet的index来获取sheet
            workbook = new XSSFWorkbook(file.getInputStream());
            XSSFSheet sheet = workbook.getSheetAt(0);
            //获取单元格中的内容,通过sheet.getLastRowNum()来获取数据的最后一行,再循环来读取单元格中的数据
            for (int i = 1; i <=sheet.getLastRowNum() ; i++) {

                XSSFRow row = sheet.getRow(i);

                XSSFCell name = row.getCell(1);
                XSSFCell status = row.getCell(2);

                double status1 = Double.parseDouble(status.toString());
                int productTypeStatus = (int)status1;

                ProductType productType = new ProductType();
                productType.setName(name.toString());
                productType.setStatus(productTypeStatus);

                productTypeService.addProductType(productType);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "redirect:/productType/findAll";

    }


    /**
     * 导出excel表格
     */
    @RequestMapping("export")
    public void exportExcel(HttpServletResponse response){
        List<ProductType> list = productTypeService.findAll();
        XSSFWorkbook workBook = new XSSFWorkbook();
        XSSFSheet sheet = workBook.createSheet("产品类型表");
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("产品类型列表"); //表头

        //合并单元格 参数:(firstRow,lastRow,firstColumn,lastColumn)
        CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 2);
        sheet.addMergedRegion(cellRangeAddress);

        XSSFRow row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("产品id");
        row1.createCell(1).setCellValue("产品名称");
        row1.createCell(2).setCellValue("产品状态");

        for (int i = 0; i <list.size()  ; i++) {
            XSSFRow row2 = sheet.createRow(i + 2);
            row2.createCell(0).setCellValue(list.get(i).getId());
            row2.createCell(1).setCellValue(list.get(i).getName());
            row2.createCell(2).setCellValue(list.get(i).getStatus()==0?"启用":"禁用");
        }
        // response.setContentType();
        try {
            ServletOutputStream out = response.getOutputStream();
            response.setHeader("Content-disposition","attachment;filename=productType.xls");
            workBook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();       
        }
    }
}

总结:

关于Ajax或者是表单发送导出excel表格的请求:
导出excel算是文件下载了,后台需要向前台(浏览器)写文件流,而ajax请求获取的数据的都是字符串(此点当时在调试页面的时候,Response响应中满是乱码的字符串文本),它没法解决后台返回的文件流,但是浏览器可以。

1.将Excel通过流写在D盘上(这种方式是可以用ajax发送请求的,)
FileOutputStream out = new FileOutputStream(“D:\xxxx.xls”);
workbook.write(out);
out.flush();
out.close();

2、注意:如果是通过浏览器下载的话,得加这个:
// String filename = “订餐情况汇总”+".xls";
// response.setContentType(“application/vnd.ms-excel”);
// response.setHeader(“Content-disposition”, “attachment;filename=” + URLEncoder.encode(filename,“UTF-8”));

前台页面这样传值:
得加上location.href=url;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值