apache poi处理excel表格

本文介绍如何利用Apache POI库在Java中实现Excel文件的上传、读取和保存到数据库,同时展示了如何从数据库中读取数据并导出到Excel文件。文章通过具体的代码示例,详细讲解了处理Excel表格数据的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入 apache poi jar包

a773585ea77f08781e1e39d905aecea617b.jpg

导入

excel文件

e4fd1e2d2a82ff91c91ed1a258a687d017c.jpg

jsp上传excel文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.ocupload-1.1.2.js"></script>
    <title>Title</title>

    <script type="text/javascript">
        $(function () {
            $("#button-import").upload({
                action: "${pageContext.request.contextPath}/regionAction_importExcel",
                name: "excelFile"
            })
        });
    </script>

</head>
<body>

<input type="button" id="uploadbtn" value="一键上传">

</body>
</html>

读取保存数据库

public class RegionAction extends BaseAction<Region> {

    @Autowired
    private RegionService regionService;

    private File excelFile;

    public RegionAction() throws IllegalAccessException, InstantiationException {
    }

    public void setExcelFile(File excelFile) {
        this.excelFile = excelFile;
    }

    public String importExcel() throws IOException {

        //System.out.println(excelFile.getAbsolutePath());

        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelFile));

        HSSFSheet sheet = workbook.getSheetAt(0);
        List<Region> regions = new ArrayList<>();

        for (Row row : sheet) {

            String id = row.getCell(0).getStringCellValue();
            String province = row.getCell(1).getStringCellValue();
            String city = row.getCell(2).getStringCellValue();
            String district = row.getCell(3).getStringCellValue();
            String postcode = row.getCell(4).getStringCellValue();

            Region region = new Region(id, province, city, district, postcode);
            regions.add(region);
        }

        regions.remove(0);
        //保存数据库
        regionService.saveAll(regions);
        return SUCCESS;
    }
}

导出

    public void exportExcel() throws IOException {

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sheet = workbook.createSheet("用户表1");

        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("id");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("密码");
        row.createCell(3).setCellValue("电话");

        List<User> users = userService.getAll();
        for (User user : users) {
            row = sheet.createRow(sheet.getLastRowNum() + 1);
            row.createCell(0).setCellValue(user.getId());
            row.createCell(1).setCellValue(user.getUsername());
            row.createCell(2).setCellValue(user.getPassword());
            row.createCell(3).setCellValue(user.getTel());
        }

        String fileName = URLEncoder.encode("全部用户.xls", "utf-8");

        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        String contentType = ServletActionContext.getServletContext().getMimeType(fileName);
        //response.setContentType("application/octet-stream");
        response.setContentType(contentType);

        OutputStream outputStream = response.getOutputStream();

        workbook.write(outputStream);
    }

 

转载于:https://my.oschina.net/gwlCode/blog/3066897

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值