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