有时候我们要把从数据库中取出的数据导入到excel中,使取到的数据看起来更加的直观和方便,在java中如何实现取到的数据导入到excel中呢?以下就是使用poi工具吧数据写入excel文件中的解决方法:
Excel表格扩展名有.xlsx和.xls两种格式
百度上对两种文件的介绍有很多就不一一列举,基本的不同总结下来有以下几点:
在java中读取和写入.xls格式使用maven导入jar包:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version> </dependency>
在java中读取和写入.xlsx格式使用maven导入jar包:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16</version> </dependency>
然后就可以使用jar包读取excel文件,并保存到本地指定的位置,首先把从数据库中取出的信息放到一个list中,然后从list中一一读取数据,写入到excel文件中,由于后面还有需求约定好使用.xlsx文件,这里生成的excel文件类型便是.xlsx文件,如果需求对文件类型没有要求,尽量生成.xls文件。
/** * * @param stuList 从数据库中查询需要导入excel文件的信息列表 * @return 返回生成的excel文件的路径 * @throws Exception */ public static String stuList2Excel(List<Student> stuList) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd hhmmss"); Workbook wb = new XSSFWorkbook(); //标题行抽出字段 String[] title = {"序号","学号", "姓名", "性别", "入学时间", "住址", "手机号", "其他信息"}; //设置sheet名称,并创建新的sheet对象 String sheetName = "学生信息一览"; Sheet stuSheet = wb.createSheet(sheetName); //获取表头行 Row titleRow = stuSheet.createRow(0); //创建单元格,设置style居中,字体,单元格大小等 CellStyle style = wb.createCellStyle(); Cell cell = null; //把已经写好的标题行写入excel文件中 for (int i = 0; i < title.length; i++) { cell = titleRow.createCell(i); cell.setCellValue(title[i]); cell.setCellStyle(style); } //把从数据库中取得的数据一一写入excel文件中 Row row = null; for (int i = 0; i < stuList.size(); i++) { //创建list.size()行数据 row = stuSheet.createRow(i + 1); //把值一一写进单元格里 //设置第一列为自动递增的序号 row.createCell(0).setCellValue(i + 1); row.createCell(1).setCellValue(stuList.get(i).getStuId()); row.createCell(2).setCellValue(stuList.get(i).getStuName()); row.createCell(3).setCellValue(stuList.get(i).getGender()); //把时间转换为指定格式的字符串再写入excel文件中 if (stuList.get(i).getEnterTime() != null) { row.createCell(4).setCellValue(sdf.format(stuList.get(i).getEnterTime())); } row.createCell(5).setCellValue(stuList.get(i).getAddress()); row.createCell(6).setCellValue(stuList.get(i).getPhone()); row.createCell(7).setCellValue(stuList.get(i).getOtherInfo()); } //设置单元格宽度自适应,在此基础上把宽度调至1.5倍 for (int i = 0; i < title.length; i++) { stuSheet.autoSizeColumn(i, true); stuSheet.setColumnWidth(i, stuSheet.getColumnWidth(i) * 15 / 10); } //获取配置文件中保存对应excel文件的路径,本地也可以直接写成F:excel/stuInfoExcel路径 String folderPath = ResourceBundle.getBundle("systemconfig").getString("downloadFolder") + File.separator + "stuInfoExcel"; //创建上传文件目录 File folder = new File(folderPath); //如果文件夹不存在创建对应的文件夹 if (!folder.exists()) { folder.mkdirs(); } //设置文件名 String fileName = sdf1.format(new Date()) + sheetName + ".xlsx"; String savePath = folderPath + File.separator + fileName; // System.out.println(savePath); OutputStream fileOut = new FileOutputStream(savePath); wb.write(fileOut); fileOut.close(); //返回文件保存全路径 return savePath; }
注意事项:
- 这里的数据使用的是数据库中的测试数据,生产环境数据字段会更多,数据会更复杂,要根据不同的数据进行处理。
- poi工具对生成的单元格宽度即使设置了自适应,有时宽度也无法显示全部数据,所以在此基础上,根据情况把单元格宽度再增加一些,这里是把单元格宽度再增加1.5倍。
- 文件保存在同一个文件夹中会导致重名,所以文件名中最好包含时间,这样的话在单人使用的时候回避免出现文件名重复的情况。
根据返回的路径名手动找到的文件:
得到的excel数据如下
以上就是从数据库中读取数据写入excel文件并保存到指定位置,后续还有前端请求下载excel文件,后端的处理方法;前端上传excel文件,读取其中的文件信息,并把对应的数据取出存入到数据库中......
未完待续。。。。。。