修改excel单元格数据

1、需要引入的maven依赖

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

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

2、代码示例

public class UpdateExcelUtil {

    public static void updateExcelUtil(List<Map> mapList) throws Exception {

        String path = new File("").getCanonicalPath();

        File file = new File(path+"/src/test/resources/电子卡密批量导入模板.xlsx");
        FileInputStream fileInputStream = new FileInputStream(file);
        XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
        XSSFSheet sheet = sheets.getSheetAt(0);

        //获取最后一行
        int lastRowNum = sheet.getLastRowNum();

        //删除数据
        if(lastRowNum>1){
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);
            sheet.removeRow(row);
        }}

        for (int rowId = 0; rowId < mapList.size(); rowId++) {
            Map<String, String> map = mapList.get(rowId);
            XSSFRow newRow = sheet.createRow(rowId + 1);
            String code = map.get("商品编码");
            String cardid = map.get("卡号");
            String psd = map.get("密码");
            XSSFCell cell0 = newRow.createCell(0);
            XSSFCell cell1 = newRow.createCell(1);
            XSSFCell cell2 = newRow.createCell(2);
            cell0.setCellValue(code);
            cell1.setCellValue(cardid);
            cell2.setCellValue(psd);

        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        sheets.write(fileOutputStream);
        fileInputStream.close();
        fileOutputStream.close();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值