Java 实现导出excel表
需要的Maven仓库依赖如下:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
1.直接上代码
1 import java.io.FileOutputStream; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.apache.poi.hssf.usermodel.HSSFCell; 7 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 8 import org.apache.poi.hssf.usermodel.HSSFRow; 9 import org.apache.poi.hssf.usermodel.HSSFSheet; 10 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 11 12 public class CreateSimpleExcelToDisk 13 { 14 /** 15 * @功能:手工构建一个简单格式的Excel 16 */ 17 private static List<Student> getStudent() throws Exception 18 { 19 List list = new ArrayList(); 20 SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd"); 21 22 Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12")); 23 Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12")); 24 Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12")); 25 list.add(user1); 26 list.add(user2); 27 list.add(user3); 28 29 return list; 30 } 31 32 public static void main(String[] args) throws Exception 33 { 34 // 第一步,创建一个webbook,对应一个Excel文件 35 HSSFWorkbook wb = new HSSFWorkbook(); 36 // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet 37 HSSFSheet sheet = wb.createSheet("学生表一"); 38 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short 39 HSSFRow row = sheet.createRow((int) 0); 40 // 第四步,创建单元格,并设置值表头 设置表头居中 41 HSSFCellStyle style = wb.createCellStyle(); 42 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 43 44 HSSFCell cell = row.createCell((short) 0); 45 cell.setCellValue("学号"); 46 cell.setCellStyle(style); 47 cell = row.createCell((short) 1); 48 cell.setCellValue("姓名"); 49 cell.setCellStyle(style); 50 cell = row.createCell((short) 2); 51 cell.setCellValue("年龄"); 52 cell.setCellStyle(style); 53 cell = row.createCell((short) 3); 54 cell.setCellValue("生日"); 55 cell.setCellStyle(style); 56 57 // 第五步,写入实体数据 实际应用中这些数据从数据库得到, 58 List list = CreateSimpleExcelToDisk.getStudent(); 59 60 for (int i = 0; i < list.size(); i++) 61 { 62 row = sheet.createRow((int) i + 1); 63 Student stu = (Student) list.get(i); 64 // 第四步,创建单元格,并设置值 65 row.createCell((short) 0).setCellValue((double) stu.getId()); 66 row.createCell((short) 1).setCellValue(stu.getName()); 67 row.createCell((short) 2).setCellValue((double) stu.getAge()); 68 cell = row.createCell((short) 3); 69 cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu 70 .getBirth())); 71 } 72 // 第六步,将文件存到指定位置 73 try 74 { 75 FileOutputStream fout = new FileOutputStream("E:/students.xls"); 76 wb.write(fout); 77 fout.close(); 78 } 79 catch (Exception e) 80 { 81 e.printStackTrace(); 82 } 83 } 84 }
2 另外补充一点,还可以设置行和列自适应的
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 设置列宽 sheet.setColumnWidth( 0 , 18 * 256 ); sheet.setColumnWidth( 2 , 18 * 256 ); sheet.setColumnWidth( 3 , 18 * 256 ); sheet.setColumnWidth( 4 , 18 * 256 ); //设置行高 HSSFRow row = sheet.createRow(( int ) 0 ); row.setHeight(st); //设置第一行和第二行格式为合并单元行,分别对应起始行,结束行,起始列,结束列 sheet.addMergedRegion( new CellRangeAddress( 0 , 0 , 0 , 2 )); sheet.addMergedRegion( new CellRangeAddress( 1 , 1 , 0 , 2 )); |
还有就是乱码问题
//这是一个工具类,IE浏览器这边做了修改,不然一直这一直有问题,现在是可以的 public static void exportExcel(String name, HSSFWorkbook workbook, HttpServletResponse response, HttpServletRequest request) throws IOException { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // 使用时间戳作为表格名称 String fileName = name + df.format(new Date()) + ".xls"; // String downloadFileName = new String(strObj.getBytes("utf-8"), "iso8859-1"); String finalFileName = null; final String userAgent = request.getHeader("USER-AGENT"); // 设置http头 if(null != userAgent && -1 != userAgent.indexOf("MSIE") || null != userAgent && -1 != userAgent.indexOf("Trident")){//IE浏览器 finalFileName = java.net.URLEncoder.encode(fileName,"UTF8"); }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器 finalFileName = new String(fileName.getBytes(), "ISO8859-1"); }else{ finalFileName = URLEncoder.encode(fileName,"UTF8");//其他浏览器 } response.setCharacterEncoding("utf-8"); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;fileName=" + finalFileName); // out出文件流 OutputStream os = response.getOutputStream(); // 输出excel workbook.write(os); // 关闭io流 os.close(); }
不积跬步,无以至千里;不积小流,无以成江海
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!