代码如下:
本文参考自:https://blog.csdn.net/m0_37681532/article/details/75252236
1 package kklazy.api.controller; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.OutputStream; 6 import java.util.ArrayList; 7 import java.util.List; 8 import org.apache.poi.hssf.usermodel.HSSFCell; 9 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 10 import org.apache.poi.hssf.usermodel.HSSFFont; 11 import org.apache.poi.hssf.usermodel.HSSFRow; 12 import org.apache.poi.hssf.usermodel.HSSFSheet; 13 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 14 15 import kklazy.security.model.User; 16 17 18 public class Excel { 19 20 public static void main(String[] args) { 21 exportExcel(); 22 } 23 24 public static void exportExcel(){ 25 FileOutputStream fos=null; 26 try { 27 //若文件地址在C盘:抛出异常:java.io.FileNotFoundException: C:bb.xls (拒绝访问。)————>可能原因:C盘权限问题 28 fos = new FileOutputStream("D:\bbb.xls"); 29 } catch (FileNotFoundException e) { 30 e.printStackTrace(); 31 } 32 HSSFWorkbook wb = new HSSFWorkbook(); 33 HSSFSheet sheet = wb.createSheet(); 34 wb.setSheetName(0, "我的工作簿1");//设置名字 35 HSSFRow row = sheet.createRow(0);//创建第一行 36 //设置样式 37 HSSFCellStyle style = wb.createCellStyle(); 38 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//对齐方式 39 //设置上下左右边框; 40 style.setBorderTop(HSSFCellStyle.BORDER_THIN); 41 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); 42 style.setBorderLeft(HSSFCellStyle.BORDER_THIN); 43 style.setBorderRight(HSSFCellStyle.BORDER_THIN); 44 //设置字体样式 45 HSSFFont font = wb.createFont(); 46 font.setFontName("宋体");//设置字体名字 47 font.setFontHeightInPoints((short)10);//设置字体大小 48 font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);////设置字体加粗-正常(不加粗)HSSFFont.BOLDWEIGHT_BOLD:加粗 49 style.setFont(font); 50 //创建单元格,设置样式并赋值 51 HSSFCell cell = row.createCell(0); 52 cell.setCellStyle(style); 53 cell.setCellValue("用户名"); 54 55 cell = row.createCell(1); 56 cell.setCellStyle(style); 57 cell.setCellValue("密码"); 58 List<User> list = new Excel().getUsers();//得到查询数据(模拟数据库查询) 59 for(int i=1;i<=list.size();i++){ 60 row = sheet.createRow(i); 61 User user = list.get(i-1); 62 cell = row.createCell(0);//创建单元格 先设置样式、编码,然后再置值。 63 cell.setCellValue(user.getUsername()); 64 65 cell = row.createCell(1); 66 cell.setCellValue(user.getPassword()); 67 68 } 69 70 try { 71 wb.write(fos); 72 fos.close(); 73 } catch (Exception e) { 74 e.printStackTrace(); 75 } 76 77 78 } 79 80 public List<User> getUsers(){ 81 List<User> users = new ArrayList<User>(); 82 User u = new User(); 83 u.setUsername("whh"); 84 u.setPassword("123456"); 85 User u1 = new User(); 86 u1.setUsername("lily"); 87 u1.setPassword("1234"); 88 users.add(u); 89 users.add(u1); 90 return users; 91 } 92 93 //若要在页面下载,输出流必须通过response获取,并传送到前台下载: 94 /* 95 * response.setContentType("application/octet-stream;charset=UTF-8"); 96 * response.setHeader("Content-Disposition", 97 "attachment; filename=" + java.net.URLEncoder.encode(reportName+ ".xls", "UTF-8")); 98 * OutputStream out = response.getOutputStream(); 99 * wb.write(out);//写出文件 100 * out.flush(); 101 * out.close(); 102 * */ 103 }
备注:若要在页面下载,输出流必须通过response获取,并传送到前台下载:
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition",
"attachment; filename=" + java.net.URLEncoder.encode(reportName+ ".xls", "UTF-8"));
OutputStream out = response.getOutputStream();
wb.write(out);//写出文件
out.flush();
out.close();