Java-POI 4.1.2 表格导入导出2020.2.17官网发布的库


Java-POI表格导入导出

Java-POI表格导入导出,给予POI4.1.2,poi-bin-4.1.2-20200217 版本,2020.2.17官网发布的库

 

 

 .

 

 

 

 1 一、读取表格  2 
 3 
 4 
 5 <%@ page import="codedna616.demo.commom.POIExcelReader" %>
 6 <%@ page import="codedna616.demo.commom.Student" %>
 7 <%@ page import="java.util.List" %>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
 10 <head>
 11     <title>读取表格数据</title>
 12 
 13     <style>
 14  table td{border:1px solid #F00}  15     </style>
 16 </head>
 17 <body>
 18 <%
 19     //"想办法找到要提供下载的文件的物理路径+文件名";
 20     String localFilePath = request.getServletContext().getRealPath("") + "\\本地导出的文件.xls";  21 %>
 22 <h3>读取表格实例 <%=localFilePath%></h3>
 23 
 24 
 25 <%
 26     // 读取Excel文件内容
 27     List<Student> readResult = POIExcelReader.readExcel(localFilePath);  28     out.println("<table style=\"border:1px\">");  29     out.println("<tr><td>编号</td><td>姓名</td><td>学号</td> <br>");  30     for (Student item : readResult) {  31         out.println("<tr><td>"+item.getId() + "</td><td>" + item.getName() + "</td><td>" + item.getStudentNo() + "</td></tr>"+"");  32  }  33     out.println("</table>");  34 %>
 35 </body>
 36 </html>
 37 
 38 
 39 
 40 
 41 二、导出本地表格  42 
 43 <%@ page import="java.io.FileInputStream" %>
 44 <%@ page import="java.net.URLEncoder" %>
 45 <%@ page import="codedna616.demo.commom.Utils" %>
 46 <%@ page import="codedna616.demo.commom.Student" %>
 47 <%@ page import="codedna616.demo.commom.PoiExportExcelUtil" %>
 48 <%@ page import="java.io.FileOutputStream" %>
 49 <%@ page import="java.util.List" %>
 50 <%@ page import="java.util.ArrayList" %><%--
 51  Created by IntelliJ IDEA.  52  User: Administrator  53   Date: 2020/7/16
 54   Time: 9:27
 55   To change this template use File | Settings | File Templates.  56 --%>
 57 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 58 <html>
 59 <head>
 60     <title>下载表格</title>
 61 </head>
 62 <body>
 63 <%
 64     if (request.getParameter("type") == "2003") {  65 
 66     } else {  67 
 68  }  69 
 70     String[] columnNames = {"ID", "姓名", "学号"};  71 
 72     List<Student> list = new ArrayList<>();  73     for (int i = 1; i <= 100; i++) {  74         Student model = new Student();  75         model.Id = Integer.toString(i);  76         model.Name = "学生" + i;  77         model.StudentNo = "序号000" + i;  78  list.add(model);  79  }  80 
 81     //方式1:直接导出表格测试【保存到本地】
 82     PoiExportExcelUtil<Student> util = new PoiExportExcelUtil<Student>();  83 
 84     String localFilePath = request.getServletContext().getRealPath("") + "\\本地导出的文件.xls";  85     util.exportExcel("用户导出", columnNames, list, new FileOutputStream(localFilePath), PoiExportExcelUtil.EXCEL_FILE_2003);  86 
 87     out.print("写入完成"+localFilePath);  88 %>
 89 </body>
 90 </html>
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 三、下载表格  99 
100 
101 
102 <%@ page import="java.net.URLEncoder" %>
103 <%@ page import="codedna616.demo.commom.Utils" %>
104 <%@ page import="java.io.FileInputStream" %>
105 
106 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
107 <html>
108 <head>
109     <title>下载web表格</title>
110 </head>
111 <body>
112 <%
113 
114     //方式2:通过流下载文件(适合web下载) 115     //关于文件下载时采用文件流输出的方式处理: 116     //加上response.reset(),并且所有的%>后面不要换行,包括最后一个;
117     response.reset();//可以加也可以不加
118     response.setContentType("application/x-download"); 119     //application.getRealPath("/main/mvplayer/CapSetup.msi");获取的物理路径 120 
121     //"想办法找到要提供下载的文件的物理路径+文件名";
122     String  localFilePath=request.getServletContext().getRealPath("")+"\\本地导出的文件.xls"; 123 
124     String filedisplay ="web下载的文件"+ Utils.getDateTimeNow()+".xls" ;//"给用户提供的下载文件名";
125     filedisplay = URLEncoder.encode(filedisplay,"UTF-8"); 126     response.addHeader("Content-Disposition","attachment;filename=" + filedisplay); 127 
128     java.io.OutputStream outp = null; 129     java.io.FileInputStream in = null; 130     try { 131         outp = response.getOutputStream(); 132         in = new FileInputStream(localFilePath); 133         byte[] b = new byte[1024]; 134         int i = 0; 135         while((i = in.read(b)) > 0) { 136             outp.write(b, 0, i); 137  } 138  outp.flush(); 139         //要加以下两句话,否则会报错 140         //java.lang.IllegalStateException: getOutputStream() has already been called for //this response
141 
142  out.clear(); 143         out = pageContext.pushBody(); 144 
145 
146     }  catch(Exception e){ 147         System.out.println("Error!"); 148  e.printStackTrace(); 149     }finally{ 150         if(in != null){ 151  in.close(); 152             in = null; 153  } 154         //这里不能关闭 155         //if(outp != null) { 156         //outp.close(); 157         //outp = null; 158         //}
159  } 160 
161 %>
162 </body>
163 </html>
164 
165 
166 核心方法如下,建议下载完整代码查看 167 
168 
169 
170 ==》》》读取表格 171 
172 POIExcelReader.java 173 
174 
175 
176 package codedna616.demo.commom; 177 
178 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 179 import org.apache.poi.ss.usermodel.Cell; 180 import org.apache.poi.ss.usermodel.Row; 181 import org.apache.poi.ss.usermodel.Sheet; 182 import org.apache.poi.ss.usermodel.Workbook; 183 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 184 //import org.springframework.web.multipart.MultipartFile;
185 
186 import java.io.File; 187 import java.io.FileInputStream; 188 import java.io.IOException; 189 import java.io.InputStream; 190 import java.text.DecimalFormat; 191 import java.util.*; 192 import java.util.logging.Logger; 193 
194 /**
195  * Author: Dreamer-1 196  * Date: 2019-03-01 197  * Time: 10:21 198  * Description: 读取Excel内容 199  */
200 public class POIExcelReader { 201 
202     private static Logger logger = Logger.getLogger(POIExcelReader.class.getName()); // 日志打印类
203 
204     private static final String XLS = "xls"; 205     private static final String XLSX = "xlsx"; 206 
207     /**
208  * 根据文件后缀名类型获取对应的工作簿对象 209  * @param inputStream 读取文件的输入流 210  * @param fileType 文件后缀名类型(xls或xlsx) 211  * @return 包含文件数据的工作簿对象 212  * @throws IOException 213      */
214     public static Workbook getWorkbook(InputStream inputStream, String fileType) throws IOException { 215         Workbook workbook = null; 216         if (fileType.equalsIgnoreCase(XLS)) { 217             workbook = new HSSFWorkbook(inputStream); 218         } else if (fileType.equalsIgnoreCase(XLSX)) { 219             workbook = new XSSFWorkbook(inputStream); 220  } 221         return workbook; 222  } 223 
224     /**
225  * 读取Excel文件内容 226  * @param fileName 要读取的Excel文件所在路径 227  * @return 读取结果列表,读取失败时返回null 228      */
229     public static List<Student> readExcel(String fileName) { 230 
231         Workbook workbook = null; 232         FileInputStream inputStream = null; 233 
234         try { 235             // 获取Excel后缀名
236             String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); 237             // 获取Excel文件
238             File excelFile = new File(fileName); 239             if (!excelFile.exists()) { 240                 logger.warning("指定的Excel文件不存在!"); 241                 return null; 242  } 243 
244             // 获取Excel工作簿
245             inputStream = new FileInputStream(excelFile); 246             workbook = getWorkbook(inputStream, fileType); 247 
248             // 读取excel中的数据
249             List<Student> resultDataList = parseExcel(workbook); 250 
251             return resultDataList; 252         } catch (Exception e) { 253             logger.warning("解析Excel失败,文件名:" + fileName + " 错误信息:" + e.getMessage()); 254             return null; 255         } finally { 256             try { 257                 if (null != workbook) { 258  workbook.close(); 259  } 260                 if (null != inputStream) { 261  inputStream.close(); 262  } 263             } catch (Exception e) { 264                 logger.warning("关闭数据流出错!错误信息:" + e.getMessage()); 265                 return null; 266  } 267  } 268  } 269 
270     /**
271  * 读取Excel文件内容 272  * @param file 上传的Excel文件 273  * @return 读取结果列表,读取失败时返回null 274      */
275 // public static List<Student> readExcel(MultipartFile file) { 276 //
277 // Workbook workbook = null; 278 //
279 // try { 280 //            // 获取Excel后缀名 281 // String fileName = file.getOriginalFilename(); 282 // if (fileName == null || fileName.isEmpty() || fileName.lastIndexOf(".") < 0) { 283 // logger.warning("解析Excel失败,因为获取到的Excel文件名非法!"); 284 // return null; 285 // } 286 // String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); 287 //
288 //            // 获取Excel工作簿 289 // workbook = getWorkbook(file.getInputStream(), fileType); 290 //
291 //            // 读取excel中的数据 292 // List<Student> resultDataList = parseExcel(workbook); 293 //
294 // return resultDataList; 295 // } catch (Exception e) { 296 // logger.warning("解析Excel失败,文件名:" + file.getOriginalFilename() + " 错误信息:" + e.getMessage()); 297 // return null; 298 // } finally { 299 // try { 300 // if (null != workbook) { 301 // workbook.close(); 302 // } 303 // } catch (Exception e) { 304 // logger.warning("关闭数据流出错!错误信息:" + e.getMessage()); 305 // return null; 306 // } 307 // } 308 // }
309 
310 
311     /**
312  * 解析Excel数据 313  * @param workbook Excel工作簿对象 314  * @return 解析结果 315      */
316     private static List<Student> parseExcel(Workbook workbook) { 317        List<Student> resultDataList = new ArrayList<>(); 318         // 解析sheet
319         for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) { 320             Sheet sheet = workbook.getSheetAt(sheetNum); 321 
322             // 校验sheet是否合法
323             if (sheet == null) { 324                 continue; 325  } 326 
327             // 获取第一行数据
328             int firstRowNum = sheet.getFirstRowNum(); 329             Row firstRow = sheet.getRow(firstRowNum); 330             if (null == firstRow) { 331                 logger.warning("解析Excel失败,在第一行没有读取到任何数据!"); 332  } 333 
334             // 解析每一行的数据,构造数据对象
335             int rowStart = firstRowNum + 1; 336             int rowEnd = sheet.getPhysicalNumberOfRows(); 337             for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) { 338                 Row row = sheet.getRow(rowNum); 339 
340                 if (null == row) { 341                     continue; 342  } 343 
344                 Student resultData = convertRowToData(row); 345                 if (null == resultData) { 346                     logger.warning("第 " + row.getRowNum() + "行数据不合法,已忽略!"); 347                     continue; 348  } 349  resultDataList.add(resultData); 350  } 351  } 352 
353         return resultDataList; 354  } 355 
356     /**
357  * 将单元格内容转换为字符串 358  * @param cell 359  * @return
360      */
361     private static String convertCellValueToString(Cell cell) { 362         if(cell==null){ 363             return null; 364  } 365         String returnValue = null; 366         switch (cell.getCellType()) { 367             case NUMERIC:   //数字
368                 Double doubleValue = cell.getNumericCellValue(); 369 
370                 // 格式化科学计数法,取一位整数
371                 DecimalFormat df = new DecimalFormat("0"); 372                 returnValue = df.format(doubleValue); 373                 break; 374             case STRING:    //字符串
375                 returnValue = cell.getStringCellValue(); 376                 break; 377             case BOOLEAN:   //布尔
378                 Boolean booleanValue = cell.getBooleanCellValue(); 379                 returnValue = booleanValue.toString(); 380                 break; 381             case BLANK:     // 空值
382                 break; 383             case FORMULA:   // 公式
384                 returnValue = cell.getCellFormula(); 385                 break; 386             case ERROR:     // 故障
387                 break; 388             default: 389                 break; 390  } 391         return returnValue; 392  } 393 
394     /**
395  * 提取每一行中需要的数据,构造成为一个结果数据对象 396  * 397  * 当该行中有单元格的数据为空或不合法时,忽略该行的数据 398  * 399  * @param row 行数据 400  * @return 解析后的行数据对象,行数据错误时返回null 401      */
402     private static Student convertRowToData(Row row) { 403         Student resultData = new Student(); 404 
405  Cell cell; 406         int cellNum = 0; 407         // 获取第一列
408         cell = row.getCell(0); 409         String id = convertCellValueToString(cell); 410  resultData.setId(id); 411         // 第二列
412         cell = row.getCell(1); 413         String name = convertCellValueToString(cell); 414  resultData.setName(name); 415         // 第三列
416         cell = row.getCell(2); 417         String sutdnetNo = convertCellValueToString(cell); 418  resultData.setStudentNo(sutdnetNo); 419 
420         return resultData; 421  } 422 
423 } 424 
425 
426 
427 
428 ==》》》》导出表格 429 
430 PoiExportExcelUtil.java文件 431 package codedna616.demo.commom; 432 
433 import java.io.IOException; 434 import java.io.OutputStream; 435 import java.lang.reflect.Field; 436 import java.lang.reflect.InvocationTargetException; 437 import java.lang.reflect.Method; 438 import java.text.SimpleDateFormat; 439 import java.util.Collection; 440 import java.util.Date; 441 import java.util.Iterator; 442 import java.util.regex.Matcher; 443 import java.util.regex.Pattern; 444 
445 //import org.apache.commons.lang3.StringUtils;
446 import org.apache.poi.hssf.usermodel.HSSFCell; 447 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 448 import org.apache.poi.hssf.usermodel.HSSFFont; 449 import org.apache.poi.hssf.usermodel.HSSFRichTextString; 450 import org.apache.poi.hssf.usermodel.HSSFRow; 451 import org.apache.poi.hssf.usermodel.HSSFSheet; 452 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 453 import org.apache.poi.hssf.util.HSSFColor; 454 import org.apache.poi.xssf.usermodel.XSSFCell; 455 import org.apache.poi.xssf.usermodel.XSSFCellStyle; 456 import org.apache.poi.xssf.usermodel.XSSFColor; 457 import org.apache.poi.xssf.usermodel.XSSFFont; 458 import org.apache.poi.xssf.usermodel.XSSFRichTextString; 459 import org.apache.poi.xssf.usermodel.XSSFRow; 460 import org.apache.poi.xssf.usermodel.XSSFSheet; 461 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 462 
463 
464 /**
465  * 导出Excel 466  * 467  * @param <T> 468  * @author 
469  */
470 public class PoiExportExcelUtil<T> { 471 
472     // 2007 版本以上 最大支持1048576行
473     public final static String EXCEl_FILE_2007 = "2007"; 474     // 2003 版本 最大支持65536 行
475     public final static String EXCEL_FILE_2003 = "2003"; 476 
477     /**
478  * <p> 479  * 导出无头部标题行Excel <br> 480  * 时间格式默认:yyyy-MM-dd hh:mm:ss <br> 481  * </p> 482  * 483  * @param title 表格标题 484  * @param dataset 数据集合 485  * @param out 输出流 486  * @param version 2003 或者 2007,不传时默认生成2003版本 487      */
488     public void exportExcel(String title, Collection<T> dataset, OutputStream out, String version) { 489         if (Utils.isNullOrEmpty(version) || EXCEL_FILE_2003.equals(version.trim())) { 490             exportExcel2003(title, null, dataset, out, "yyyy-MM-dd HH:mm:ss"); 491         } else { 492             exportExcel2007(title, null, dataset, out, "yyyy-MM-dd HH:mm:ss"); 493  } 494  } 495 
496     /**
497  * <p> 498  * 导出带有头部标题行的Excel <br> 499  * 时间格式默认:yyyy-MM-dd hh:mm:ss <br> 500  * </p> 501  * 502  * @param title 表格标题 503  * @param headers 头部标题集合 504  * @param dataset 数据集合 505  * @param out 输出流 506  * @param version 2003 或者 2007,不传时默认生成2003版本 507      */
508     public void exportExcel(String title, String[] headers, Collection<T> dataset, OutputStream out, String version) { 509         if (Utils.isNullOrEmpty(version) || EXCEL_FILE_2003.equals(version.trim())) { 510             exportExcel2003(title, headers, dataset, out, "yyyy-MM-dd HH:mm:ss"); 511         } else { 512             exportExcel2007(title, headers, dataset, out, "yyyy-MM-dd HH:mm:ss"); 513  } 514  } 515 
516     /**
517  * <p> 518  * 通用Excel导出方法,利用反射机制遍历对象的所有字段,将数据写入Excel文件中 <br> 519  * 此版本生成2007以上版本的文件 (文件后缀:xlsx) 520  * </p> 521  * 522  * @param title 表格标题名 523  * @param headers 表格头部标题集合 524  * @param dataset 需要显示的数据集合,集合中一定要放置符合JavaBean风格的类的对象。此方法支持的 525  * JavaBean属性的数据类型有基本数据类型及String,Date 526  * @param out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中 527  * @param pattern 如果有时间数据,设定输出格式。默认为"yyyy-MM-dd hh:mm:ss" 528      */
529     @SuppressWarnings({"unchecked", "rawtypes"}) 530     public void exportExcel2007(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) { 531         // 声明一个工作薄
532         XSSFWorkbook workbook = new XSSFWorkbook(); 533         // 生成一个表格
534         XSSFSheet sheet = workbook.createSheet(title); 535         // 设置表格默认列宽度为15个字节
536         sheet.setDefaultColumnWidth(20); 537         // 生成一个样式
538         XSSFCellStyle style = workbook.createCellStyle(); 539         // 设置这些样式
540         style.setFillForegroundColor(new XSSFColor(java.awt.Color.gray)); 541 // style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); 542 //
543 // style.setBorderBottom(XSSFCellStyle.BORDER_THIN); 544 // style.setBorderLeft(XSSFCellStyle.BORDER_THIN); 545 // style.setBorderRight(XSSFCellStyle.BORDER_THIN); 546 // style.setBorderTop(XSSFCellStyle.BORDER_THIN); 547 // style.setAlignment(XSSFCellStyle.ALIGN_CENTER); 548         // 生成一个字体
549         XSSFFont font = workbook.createFont(); 550         //font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
551         font.setFontName("宋体"); 552         font.setColor(new XSSFColor(java.awt.Color.BLACK)); 553         font.setFontHeightInPoints((short) 11); 554         // 把字体应用到当前的样式
555  style.setFont(font); 556         // 生成并设置另一个样式
557         XSSFCellStyle style2 = workbook.createCellStyle(); 558         style2.setFillForegroundColor(new XSSFColor(java.awt.Color.WHITE)); 559 // style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); 560 // style2.setBorderBottom(XSSFCellStyle.BORDER_THIN); 561 // style2.setBorderLeft(XSSFCellStyle.BORDER_THIN); 562 // style2.setBorderRight(XSSFCellStyle.BORDER_THIN); 563 // style2.setBorderTop(XSSFCellStyle.BORDER_THIN); 564 // style2.setAlignment(XSSFCellStyle.ALIGN_CENTER); 565 // style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); 566         // 生成另一个字体
567         XSSFFont font2 = workbook.createFont(); 568        //font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); 569         // 把字体应用到当前的样式
570  style2.setFont(font2); 571 
572         // 产生表格标题行
573         XSSFRow row = sheet.createRow(0); 574  XSSFCell cellHeader; 575         for (int i = 0; i < headers.length; i++) { 576             cellHeader = row.createCell(i); 577  cellHeader.setCellStyle(style); 578             cellHeader.setCellValue(new XSSFRichTextString(headers[i])); 579  } 580 
581         // 遍历集合数据,产生数据行
582         Iterator<T> it = dataset.iterator(); 583         int index = 0; 584  T t; 585  Field[] fields; 586  Field field; 587  XSSFRichTextString richString; 588         Pattern p = Pattern.compile("^//d+(//.//d+)?$"); 589  Matcher matcher; 590  String fieldName; 591  String getMethodName; 592  XSSFCell cell; 593  Class tCls; 594  Method getMethod; 595  Object value; 596  String textValue; 597         SimpleDateFormat sdf = new SimpleDateFormat(pattern); 598         while (it.hasNext()) { 599             index++; 600             row = sheet.createRow(index); 601             t = (T) it.next(); 602             // 利用反射,根据JavaBean属性的先后顺序,动态调用getXxx()方法得到属性值
603             fields = t.getClass().getDeclaredFields(); 604             for (int i = 0; i < fields.length; i++) { 605                 cell = row.createCell(i); 606  cell.setCellStyle(style2); 607                 field = fields[i]; 608                 fieldName = field.getName(); 609                 getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() 610                         + fieldName.substring(1); 611                 try { 612                     tCls = t.getClass(); 613                     getMethod = tCls.getMethod(getMethodName, new Class[]{}); 614                     value = getMethod.invoke(t, new Object[]{}); 615                     // 判断值的类型后进行强制类型转换
616                     textValue = null; 617                     if (value instanceof Integer) { 618  cell.setCellValue((Integer) value); 619                     } else if (value instanceof Float) { 620                         textValue = String.valueOf((Float) value); 621  cell.setCellValue(textValue); 622                     } else if (value instanceof Double) { 623                         textValue = String.valueOf((Double) value); 624  cell.setCellValue(textValue); 625                     } else if (value instanceof Long) { 626  cell.setCellValue((Long) value); 627  } 628                     if (value instanceof Boolean) { 629                         textValue = "是"; 630                         if (!(Boolean) value) { 631                             textValue = "否"; 632  } 633                     } else if (value instanceof Date) { 634                         textValue = sdf.format((Date) value); 635                     } else { 636                         // 其它数据类型都当作字符串简单处理
637                         if (value != null) { 638                             textValue = value.toString(); 639  } 640  } 641                     if (textValue != null) { 642                         matcher = p.matcher(textValue); 643                         if (matcher.matches()) { 644                             // 是数字当作double处理
645  cell.setCellValue(Double.parseDouble(textValue)); 646                         } else { 647                             richString = new XSSFRichTextString(textValue); 648  cell.setCellValue(richString); 649  } 650  } 651                 } catch (SecurityException e) { 652  e.printStackTrace(); 653                 } catch (NoSuchMethodException e) { 654  e.printStackTrace(); 655                 } catch (IllegalArgumentException e) { 656  e.printStackTrace(); 657                 } catch (IllegalAccessException e) { 658  e.printStackTrace(); 659                 } catch (InvocationTargetException e) { 660  e.printStackTrace(); 661                 } finally { 662                     // 清理资源
663  } 664  } 665  } 666         try { 667  workbook.write(out); 668         } catch (IOException e) { 669  e.printStackTrace(); 670  } 671  } 672 
673 
674     /**
675  * <p> 676  * 通用Excel导出方法,利用反射机制遍历对象的所有字段,将数据写入Excel文件中 <br> 677  * 此方法生成2003版本的excel,文件名后缀:xls <br> 678  * </p> 679  * 680  * @param title 表格标题名 681  * @param headers 表格头部标题集合 682  * @param dataset 需要显示的数据集合,集合中一定要放置符合JavaBean风格的类的对象。此方法支持的 683  * JavaBean属性的数据类型有基本数据类型及String,Date 684  * @param out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中 685  * @param pattern 如果有时间数据,设定输出格式。默认为"yyyy-MM-dd hh:mm:ss" 686      */
687     @SuppressWarnings({"unchecked", "rawtypes"}) 688     public void exportExcel2003(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) { 689         // 声明一个工作薄
690         HSSFWorkbook workbook = new HSSFWorkbook(); 691         // 生成一个表格
692         HSSFSheet sheet = workbook.createSheet(title); 693         // 设置表格默认列宽度为15个字节
694         sheet.setDefaultColumnWidth(20); 695         // 生成一个样式
696         HSSFCellStyle style = workbook.createCellStyle(); 697         // 设置这些样式 698 // style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index); 699 // style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 700 // style.setBorderBottom(HSSFCellStyle.BORDER_THIN); 701 // style.setBorderLeft(HSSFCellStyle.BORDER_THIN); 702 // style.setBorderRight(HSSFCellStyle.BORDER_THIN); 703 // style.setBorderTop(HSSFCellStyle.BORDER_THIN); 704 // style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 705         // 生成一个字体
706         HSSFFont font = workbook.createFont(); 707      // font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
708         font.setFontName("宋体"); 709        // font.setColor(HSSFColor.WHITE.index);
710         font.setFontHeightInPoints((short) 11); 711         // 把字体应用到当前的样式
712  style.setFont(font); 713         // 生成并设置另一个样式
714         HSSFCellStyle style2 = workbook.createCellStyle(); 715 // style2.setFillForegroundColor(HSSFColor.WHITE.index); 716 // style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 717 // style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); 718 // style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); 719 // style2.setBorderRight(HSSFCellStyle.BORDER_THIN); 720 // style2.setBorderTop(HSSFCellStyle.BORDER_THIN); 721 // style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); 722 // style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 723         // 生成另一个字体
724         HSSFFont font2 = workbook.createFont(); 725        // font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); 726         // 把字体应用到当前的样式
727  style2.setFont(font2); 728 
729         // 产生表格标题行
730         HSSFRow row = sheet.createRow(0); 731  HSSFCell cellHeader; 732         for (int i = 0; i < headers.length; i++) { 733             cellHeader = row.createCell(i); 734  cellHeader.setCellStyle(style); 735             cellHeader.setCellValue(new HSSFRichTextString(headers[i])); 736  } 737 
738         // 遍历集合数据,产生数据行
739         Iterator<T> it = dataset.iterator(); 740         int index = 0; 741  T t; 742  Field[] fields; 743  Field field; 744  HSSFRichTextString richString; 745         Pattern p = Pattern.compile("^//d+(//.//d+)?$"); 746  Matcher matcher; 747  String fieldName; 748  String getMethodName; 749  HSSFCell cell; 750  Class tCls; 751  Method getMethod; 752  Object value; 753  String textValue; 754         SimpleDateFormat sdf = new SimpleDateFormat(pattern); 755         while (it.hasNext()) { 756             index++; 757             row = sheet.createRow(index); 758             t = (T) it.next(); 759             // 利用反射,根据JavaBean属性的先后顺序,动态调用getXxx()方法得到属性值
760             fields = t.getClass().getDeclaredFields(); 761             for (int i = 0; i < fields.length; i++) { 762                 cell = row.createCell(i); 763  cell.setCellStyle(style2); 764                 field = fields[i]; 765                 fieldName = field.getName(); 766                 getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() 767                         + fieldName.substring(1); 768                 try { 769                     tCls = t.getClass(); 770                     getMethod = tCls.getMethod(getMethodName, new Class[]{}); 771                     value = getMethod.invoke(t, new Object[]{}); 772                     // 判断值的类型后进行强制类型转换
773                     textValue = null; 774                     if (value instanceof Integer) { 775  cell.setCellValue((Integer) value); 776                     } else if (value instanceof Float) { 777                         textValue = String.valueOf((Float) value); 778  cell.setCellValue(textValue); 779                     } else if (value instanceof Double) { 780                         textValue = String.valueOf((Double) value); 781  cell.setCellValue(textValue); 782                     } else if (value instanceof Long) { 783  cell.setCellValue((Long) value); 784  } 785                     if (value instanceof Boolean) { 786                         textValue = "是"; 787                         if (!(Boolean) value) { 788                             textValue = "否"; 789  } 790                     } else if (value instanceof Date) { 791                         textValue = sdf.format((Date) value); 792                     } else { 793                         // 其它数据类型都当作字符串简单处理
794                         if (value != null) { 795                             textValue = value.toString(); 796  } 797  } 798                     if (textValue != null) { 799                         matcher = p.matcher(textValue); 800                         if (matcher.matches()) { 801                             // 是数字当作double处理
802  cell.setCellValue(Double.parseDouble(textValue)); 803                         } else { 804                             richString = new HSSFRichTextString(textValue); 805  cell.setCellValue(richString); 806  } 807  } 808                 } catch (SecurityException e) { 809  e.printStackTrace(); 810                 } catch (NoSuchMethodException e) { 811  e.printStackTrace(); 812                 } catch (IllegalArgumentException e) { 813  e.printStackTrace(); 814                 } catch (IllegalAccessException e) { 815  e.printStackTrace(); 816                 } catch (InvocationTargetException e) { 817  e.printStackTrace(); 818                 } finally { 819                     // 清理资源
820  } 821  } 822  } 823         try { 824  workbook.write(out); 825         } catch (IOException e) { 826  e.printStackTrace(); 827         }catch (Exception e) { 828  e.printStackTrace(); 829  } 830  } 831 }

 

 

 

下载链接:http://video-course-upyun.51minicloud.com/java/POIDemo_codedna618.zip

    http://edu.51minicloud.com/Course/Detail?vid=1137

 

备用下载链接:

一、读取表格

 

<%@ page import="codedna616.demo.commom.POIExcelReader" %>
<%@ page import="codedna616.demo.commom.Student" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>读取表格数据</title>

<style>
table td{border:1px solid #F00}
</style>
</head>
<body>
<%
   //"想办法找到要提供下载的文件的物理路径+文件名";
   String localFilePath = request.getServletContext().getRealPath("") + "\\本地导出的文件.xls";
%>
<h3>读取表格实例 <%=localFilePath%></h3>



<%
   // 读取Excel文件内容
   List<Student> readResult = POIExcelReader.readExcel(localFilePath);
   out.println("<table style=\"border:1px\">");
   out.println("<tr><td>编号</td><td>姓名</td><td>学号</td> <br>");
   for (Student item : readResult) {
       out.println("<tr><td>"+item.getId() + "</td><td>" + item.getName() + "</td><td>" + item.getStudentNo() + "</td></tr>"+"");
   }
   out.println("</table>");
%>
</body>

</html>

 

 

二、导出本地表格

<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="codedna616.demo.commom.Utils" %>
<%@ page import="codedna616.demo.commom.Student" %>
<%@ page import="codedna616.demo.commom.PoiExportExcelUtil" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %><%--
 Created by IntelliJ IDEA.
 User: Administrator
 Date: 2020/7/16
 Time: 9:27
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
<title>下载表格</title>
</head>
<body>
<%
   if (request.getParameter("type") == "2003") {

   } else {

   }

   String[] columnNames = {"ID", "姓名", "学号"};

   List<Student> list = new ArrayList<>();
   for (int i = 1; i <= 100; i++) {
       Student model = new Student();
       model.Id = Integer.toString(i);
       model.Name = "学生" + i;
       model.StudentNo = "序号000" + i;
       list.add(model);
   }

   //方式1:直接导出表格测试【保存到本地】
   PoiExportExcelUtil<Student> util = new PoiExportExcelUtil<Student>();

   String localFilePath = request.getServletContext().getRealPath("") + "\\本地导出的文件.xls";
   util.exportExcel("用户导出", columnNames, list, new FileOutputStream(localFilePath), PoiExportExcelUtil.EXCEL_FILE_2003);

   out.print("写入完成"+localFilePath);
%>
</body>

</html>

 

 

 

三、下载表格

 

<%@ page import="java.net.URLEncoder" %>
<%@ page import="codedna616.demo.commom.Utils" %>
<%@ page import="java.io.FileInputStream" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>下载web表格</title>
</head>
<body>
<%

   //方式2:通过流下载文件(适合web下载)
   //关于文件下载时采用文件流输出的方式处理:
   //加上response.reset(),并且所有的%>后面不要换行,包括最后一个;
   response.reset();//可以加也可以不加
   response.setContentType("application/x-download");
   //application.getRealPath("/main/mvplayer/CapSetup.msi");获取的物理路径

   //"想办法找到要提供下载的文件的物理路径+文件名";
   String  localFilePath=request.getServletContext().getRealPath("")+"\\本地导出的文件.xls";

   String filedisplay ="web下载的文件"+ Utils.getDateTimeNow()+".xls" ;//"给用户提供的下载文件名";
   filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
   response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

   java.io.OutputStream outp = null;
   java.io.FileInputStream in = null;
   try {
       outp = response.getOutputStream();
       in = new FileInputStream(localFilePath);
       byte[] b = new byte[1024];
       int i = 0;
       while((i = in.read(b)) > 0) {
           outp.write(b, 0, i);
       }
       outp.flush();
       //要加以下两句话,否则会报错
       //java.lang.IllegalStateException: getOutputStream() has already been called for //this response

       out.clear();
       out = pageContext.pushBody();


   }  catch(Exception e){
       System.out.println("Error!");
       e.printStackTrace();
   }finally{
       if(in != null){
           in.close();
           in = null;
       }
       //这里不能关闭
       //if(outp != null)  {
       //outp.close();
       //outp = null;
       //}
   }

%>
</body>

</html>

 

核心方法如下,建议下载完整代码查看

 

==》》》读取表格

POIExcelReader.java

 

 

package codedna616.demo.commom;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//import org.springframework.web.multipart.MultipartFile;

import java.io.File;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.*;
import java.util.logging.Logger;

/**
* Author: Dreamer-1
* Date: 2019-03-01
* Time: 10:21
* Description: 读取Excel内容
*/
public class POIExcelReader {


private static Logger logger = Logger.getLogger(POIExcelReader.class.getName()); // 日志打印类

   private static final String XLS = "xls";

private static final String XLSX = "xlsx";

/**
    * 根据文件后缀名类型获取对应的工作簿对象
    * @param inputStream 读取文件的输入流
    * @param fileType 文件后缀名类型(xlsxlsx
    * @return 包含文件数据的工作簿对象
    * @throws IOException
    */
   public static Workbook getWorkbook(InputStream inputStream, String fileType) throws IOException {

Workbook workbook = null;
if (fileType.equalsIgnoreCase(XLS)) {
workbook = new HSSFWorkbook(inputStream);
} else if (fileType.equalsIgnoreCase(XLSX)) {
workbook = new XSSFWorkbook(inputStream);
}
return workbook;
}

/**
    * 读取Excel文件内容
    * @param fileName 要读取的Excel文件所在路径
    * @return 读取结果列表,读取失败时返回null
    */
   public static List<Student> readExcel(String fileName) {


Workbook workbook = null;
FileInputStream inputStream = null;

try {
// 获取Excel后缀名
           String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

// 获取Excel文件
           File excelFile = new File(fileName);

if (!excelFile.exists()) {
logger.warning("指定的Excel文件不存在!");
return null;
}

// 获取Excel工作簿
           inputStream = new FileInputStream(excelFile);

workbook = getWorkbook(inputStream, fileType);

// 读取excel中的数据
           List<Student> resultDataList = parseExcel(workbook);


return resultDataList;
} catch (Exception e) {
logger.warning("解析Excel失败,文件名:" + fileName + " 错误信息:" + e.getMessage());
return null;
} finally {
try {
if (null != workbook) {
workbook.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (Exception e) {
logger.warning("关闭数据流出错!错误信息:" + e.getMessage());
return null;
}
}
}

/**
    * 读取Excel文件内容
    * @param file 上传的Excel文件
    * @return 读取结果列表,读取失败时返回null
    */
//    public static List<Student> readExcel(MultipartFile file) {
//
//        Workbook workbook = null;
//
//        try {
//            // 获取Excel后缀名
//            String fileName = file.getOriginalFilename();
//            if (fileName == null || fileName.isEmpty() || fileName.lastIndexOf(".") < 0) {
//                logger.warning("解析Excel失败,因为获取到的Excel文件名非法!");
//                return null;
//            }
//            String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
//
//            // 获取Excel工作簿
//            workbook = getWorkbook(file.getInputStream(), fileType);
//
//            // 读取excel中的数据
//            List<Student> resultDataList = parseExcel(workbook);
//
//            return resultDataList;
//        } catch (Exception e) {
//            logger.warning("解析Excel失败,文件名:" + file.getOriginalFilename() + " 错误信息:" + e.getMessage());
//            return null;
//        } finally {
//            try {
//                if (null != workbook) {
//                    workbook.close();
//                }
//            } catch (Exception e) {
//                logger.warning("关闭数据流出错!错误信息:" + e.getMessage());
//                return null;
//            }
//        }
//    }


   /**
    * 解析Excel数据
    * @param workbook Excel工作簿对象
    * @return 解析结果
    */
   private static List<Student> parseExcel(Workbook workbook) {

List<Student> resultDataList = new ArrayList<>();
// 解析sheet
       for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {

Sheet sheet = workbook.getSheetAt(sheetNum);

// 校验sheet是否合法
           if (sheet == null) {

continue;
}

// 获取第一行数据
           int firstRowNum = sheet.getFirstRowNum();

Row firstRow = sheet.getRow(firstRowNum);
if (null == firstRow) {
logger.warning("解析Excel失败,在第一行没有读取到任何数据!");
}

// 解析每一行的数据,构造数据对象
           int rowStart = firstRowNum + 1;

int rowEnd = sheet.getPhysicalNumberOfRows();
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row row = sheet.getRow(rowNum);

if (null == row) {
continue;
}

Student resultData = convertRowToData(row);
if (null == resultData) {
logger.warning(" " + row.getRowNum() + "行数据不合法,已忽略!");
continue;
}
resultDataList.add(resultData);
}
}

return resultDataList;
}

/**
    * 将单元格内容转换为字符串
    * @param cell
    * @return
    */
   private static String convertCellValueToString(Cell cell) {

if(cell==null){
return null;
}
String returnValue = null;
switch (cell.getCellType()) {
case NUMERIC: //数字
               Double doubleValue = cell.getNumericCellValue();


// 格式化科学计数法,取一位整数
               DecimalFormat df = new DecimalFormat("0");

returnValue = df.format(doubleValue);
break;
case STRING: //字符串
               returnValue = cell.getStringCellValue();

break;
case BOOLEAN: //布尔
               Boolean booleanValue = cell.getBooleanCellValue();

returnValue = booleanValue.toString();
break;
case BLANK: // 空值
               break;

case FORMULA: // 公式
               returnValue = cell.getCellFormula();

break;
case ERROR: // 故障
               break;

default:
break;
}
return returnValue;
}

/**
    * 提取每一行中需要的数据,构造成为一个结果数据对象
    *
    * 当该行中有单元格的数据为空或不合法时,忽略该行的数据
    *
    * @param row 行数据
    * @return 解析后的行数据对象,行数据错误时返回null
    */
   private static Student convertRowToData(Row row) {

Student resultData = new Student();

Cell cell;
int cellNum = 0;
// 获取第一列
       cell = row.getCell(0);

String id = convertCellValueToString(cell);
resultData.setId(id);
// 第二列
       cell = row.getCell(1);

String name = convertCellValueToString(cell);
resultData.setName(name);
// 第三列
       cell = row.getCell(2);

String sutdnetNo = convertCellValueToString(cell);
resultData.setStudentNo(sutdnetNo);

return resultData;
}

}

 

 

==》》》》导出表格

PoiExportExcelUtil.java文件

package codedna616.demo.commom;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


/**
* 导出Excel
*
* @param <T>
* @author
*/
public class PoiExportExcelUtil<T> {

// 2007 版本以上 最大支持1048576
   public final static String EXCEl_FILE_2007 = "2007";
// 2003 版本 最大支持65536
   public final static String EXCEL_FILE_2003 = "2003";

/**
    * <p>
    * 导出无头部标题行Excel <br>
    * 时间格式默认:yyyy-MM-dd hh:mm:ss <br>
    * </p>
    *
    * @param title   表格标题
    * @param dataset 数据集合
    * @param out     输出流
    * @param version 2003 或者 2007,不传时默认生成2003版本
    */
   public void exportExcel(String title, Collection<T> dataset, OutputStream out, String version) {
if (Utils.isNullOrEmpty(version) || EXCEL_FILE_2003.equals(version.trim())) {
exportExcel2003(title, null, dataset, out, "yyyy-MM-dd HH:mm:ss");
} else {
exportExcel2007(title, null, dataset, out, "yyyy-MM-dd HH:mm:ss");
}
}

/**
    * <p>
    * 导出带有头部标题行的Excel <br>
    * 时间格式默认:yyyy-MM-dd hh:mm:ss <br>
    * </p>
    *
    * @param title   表格标题
    * @param headers 头部标题集合
    * @param dataset 数据集合
    * @param out     输出流
    * @param version 2003 或者 2007,不传时默认生成2003版本
    */
   public void exportExcel(String title, String[] headers, Collection<T> dataset, OutputStream out, String version) {
if (Utils.isNullOrEmpty(version) || EXCEL_FILE_2003.equals(version.trim())) {
exportExcel2003(title, headers, dataset, out, "yyyy-MM-dd HH:mm:ss");
} else {
exportExcel2007(title, headers, dataset, out, "yyyy-MM-dd HH:mm:ss");
}
}

/**
    * <p>
    * 通用Excel导出方法,利用反射机制遍历对象的所有字段,将数据写入Excel文件中 <br>
    * 此版本生成2007以上版本的文件 (文件后缀:xlsx)
    * </p>
    *
    * @param title   表格标题名
    * @param headers 表格头部标题集合
    * @param dataset 需要显示的数据集合,集合中一定要放置符合JavaBean风格的类的对象。此方法支持的
    *                JavaBean属性的数据类型有基本数据类型及String,Date
    * @param out     与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中
    * @param pattern 如果有时间数据,设定输出格式。默认为"yyyy-MM-dd hh:mm:ss"
    */
   @SuppressWarnings({"unchecked", "rawtypes"})
public void exportExcel2007(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) {
// 声明一个工作薄
       XSSFWorkbook workbook = new XSSFWorkbook();
// 生成一个表格
       XSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
       sheet.setDefaultColumnWidth(20);
// 生成一个样式
       XSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
       style.setFillForegroundColor(new XSSFColor(java.awt.Color.gray));
//        style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
//
//        style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
//        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
//        style.setBorderRight(XSSFCellStyle.BORDER_THIN);
//        style.setBorderTop(XSSFCellStyle.BORDER_THIN);
//        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
       // 生成一个字体
       XSSFFont font = workbook.createFont();
//font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
       font.setFontName("宋体");
font.setColor(new XSSFColor(java.awt.Color.BLACK));
font.setFontHeightInPoints((short) 11);
// 把字体应用到当前的样式
       style.setFont(font);
// 生成并设置另一个样式
       XSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(new XSSFColor(java.awt.Color.WHITE));
//        style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
//        style2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
//        style2.setBorderLeft(XSSFCellStyle.BORDER_THIN);
//        style2.setBorderRight(XSSFCellStyle.BORDER_THIN);
//        style2.setBorderTop(XSSFCellStyle.BORDER_THIN);
//        style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
//        style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
       // 生成另一个字体
       XSSFFont font2 = workbook.createFont();
//font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
       // 把字体应用到当前的样式
       style2.setFont(font2);

// 产生表格标题行
       XSSFRow row = sheet.createRow(0);
XSSFCell cellHeader;
for (int i = 0; i < headers.length; i++) {
cellHeader = row.createCell(i);
cellHeader.setCellStyle(style);
cellHeader.setCellValue(new XSSFRichTextString(headers[i]));
}

// 遍历集合数据,产生数据行
       Iterator<T> it = dataset.iterator();
int index = 0;
T t;
Field[] fields;
Field field;
XSSFRichTextString richString;
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher;
String fieldName;
String getMethodName;
XSSFCell cell;
Class tCls;
Method getMethod;
Object value;
String textValue;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
t = (T) it.next();
// 利用反射,根据JavaBean属性的先后顺序,动态调用getXxx()方法得到属性值
           fields = t.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
cell = row.createCell(i);
cell.setCellStyle(style2);
field = fields[i];
fieldName = field.getName();
getMethodName = "get" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try {
tCls = t.getClass();
getMethod = tCls.getMethod(getMethodName, new Class[]{});
value = getMethod.invoke(t, new Object[]{});
// 判断值的类型后进行强制类型转换
                   textValue = null;
if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Float) {
textValue = String.valueOf((Float) value);
cell.setCellValue(textValue);
} else if (value instanceof Double) {
textValue = String.valueOf((Double) value);
cell.setCellValue(textValue);
} else if (value instanceof Long) {
cell.setCellValue((Long) value);
}
if (value instanceof Boolean) {
textValue = "";
if (!(Boolean) value) {
textValue = "";
}
} else if (value instanceof Date) {
textValue = sdf.format((Date) value);
} else {
// 其它数据类型都当作字符串简单处理
                       if (value != null) {
textValue = value.toString();
}
}
if (textValue != null) {
matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
                           cell.setCellValue(Double.parseDouble(textValue));
} else {
richString = new XSSFRichTextString(textValue);
cell.setCellValue(richString);
}
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} finally {
// 清理资源
               }
}
}
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}


/**
    * <p>
    * 通用Excel导出方法,利用反射机制遍历对象的所有字段,将数据写入Excel文件中 <br>
    * 此方法生成2003版本的excel,文件名后缀:xls <br>
    * </p>
    *
    * @param title   表格标题名
    * @param headers 表格头部标题集合
    * @param dataset 需要显示的数据集合,集合中一定要放置符合JavaBean风格的类的对象。此方法支持的
    *                JavaBean属性的数据类型有基本数据类型及String,Date
    * @param out     与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中
    * @param pattern 如果有时间数据,设定输出格式。默认为"yyyy-MM-dd hh:mm:ss"
    */
   @SuppressWarnings({"unchecked", "rawtypes"})
public void exportExcel2003(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) {
// 声明一个工作薄
       HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
       HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
       sheet.setDefaultColumnWidth(20);
// 生成一个样式
       HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
//        style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
//        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
       // 生成一个字体
       HSSFFont font = workbook.createFont();
//   font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
       font.setFontName("宋体");
// font.setColor(HSSFColor.WHITE.index);
       font.setFontHeightInPoints((short) 11);
// 把字体应用到当前的样式
       style.setFont(font);
// 生成并设置另一个样式
       HSSFCellStyle style2 = workbook.createCellStyle();
//        style2.setFillForegroundColor(HSSFColor.WHITE.index);
//        style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//        style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//        style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//        style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
//        style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
//        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//        style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
       // 生成另一个字体
       HSSFFont font2 = workbook.createFont();
// font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
       // 把字体应用到当前的样式
       style2.setFont(font2);

// 产生表格标题行
       HSSFRow row = sheet.createRow(0);
HSSFCell cellHeader;
for (int i = 0; i < headers.length; i++) {
cellHeader = row.createCell(i);
cellHeader.setCellStyle(style);
cellHeader.setCellValue(new HSSFRichTextString(headers[i]));
}

// 遍历集合数据,产生数据行
       Iterator<T> it = dataset.iterator();
int index = 0;
T t;
Field[] fields;
Field field;
HSSFRichTextString richString;
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher;
String fieldName;
String getMethodName;
HSSFCell cell;
Class tCls;
Method getMethod;
Object value;
String textValue;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
t = (T) it.next();
// 利用反射,根据JavaBean属性的先后顺序,动态调用getXxx()方法得到属性值
           fields = t.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
cell = row.createCell(i);
cell.setCellStyle(style2);
field = fields[i];
fieldName = field.getName();
getMethodName = "get" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try {
tCls = t.getClass();
getMethod = tCls.getMethod(getMethodName, new Class[]{});
value = getMethod.invoke(t, new Object[]{});
// 判断值的类型后进行强制类型转换
                   textValue = null;
if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Float) {
textValue = String.valueOf((Float) value);
cell.setCellValue(textValue);
} else if (value instanceof Double) {
textValue = String.valueOf((Double) value);
cell.setCellValue(textValue);
} else if (value instanceof Long) {
cell.setCellValue((Long) value);
}
if (value instanceof Boolean) {
textValue = "";
if (!(Boolean) value) {
textValue = "";
}
} else if (value instanceof Date) {
textValue = sdf.format((Date) value);
} else {
// 其它数据类型都当作字符串简单处理
                       if (value != null) {
textValue = value.toString();
}
}
if (textValue != null) {
matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
                           cell.setCellValue(Double.parseDouble(textValue));
} else {
richString = new HSSFRichTextString(textValue);
cell.setCellValue(richString);
}
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} finally {
// 清理资源
               }
}
}
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}

jar文件和代码下载地址,使用ideaj ,java-jdk1.8 导入即可:


下载地址

 http://edu.51minicloud.com/ThirdPartDown/Down?id=3034906a3884487e8d93f0a546daea4a&title=cnblog


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM