栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

excel表中的数据写入写出

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

excel表中的数据写入写出

public class ExcelDemo {
    public static void main(String[] args) throws IOException {
        ArrayList list = new ArrayList<>();
        list.add(new CheckgroupCheckitem(15,86));
        list.add(new CheckgroupCheckitem(88,74));
        list.add(new CheckgroupCheckitem(22,11));
        list.add(new CheckgroupCheckitem(44,35));

        //创建一个Excel文件对象  空的
        File file = new File("D:/path/springboot/dd.xlsx");
        //在内存中创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建工作表
        XSSFSheet sheet = workbook.createSheet("表格");
        //创建行
        XSSFRow row = sheet.createRow(0);
        //创建这一行的单元单
        row.createCell(0).setCellValue("组号");
        row.createCell(1).setCellValue("项目号");
        int index = 1;
        for (CheckgroupCheckitem checkgroupCheckitem : list) {
            XSSFRow row1 = sheet.createRow(index);
            row1.createCell(0).setCellValue(checkgroupCheckitem.getCheckgroupId());
            row1.createCell(1).setCellValue(checkgroupCheckitem.getCheckitemId());
            index++;
        }
        //把内存的数据写到Excel
        workbook.write(new FileOutputStream(file));
        System.out.println("yes");

        workbook.close();
    }
}

将excel表中数据取出

public class ExcelDemo2 {
    public static void main(String[] args) throws IOException, InvalidFormatException {
        //创建文件对象
        File file = new File("D:\path\springboot2\template.xlsx");

        XSSFWorkbook workbook = new XSSFWorkbook(file);

        XSSFSheet sheet = workbook.getSheetAt(0);
        int rows = sheet.getPhysicalNumberOfRows();
        System.out.println(rows);
        for (int i = 1; i < rows; i++) {
            XSSFRow row = sheet.getRow(i);
            Date v1 = row.getCell(0).getDateCellValue();
            double v2 = row.getCell(1).getNumericCellValue();
            System.out.println(v1+"--"+v2);
        }
        workbook.close();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490579.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号