public class ExcelDemo { public static void main(String[] args) throws IOException { ArrayList<CheckgroupCheckitem> 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(); } }