一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - java常用工具類之Excel操作類及依賴包下載

java常用工具類之Excel操作類及依賴包下載

2019-11-24 15:23junjie JAVA教程

這篇文章主要介紹了java常用工具類Excel操作類及依賴包下載,需要的朋友可以參考下

Excel工具類ExcelUtil.java源碼:

  1. package com.itjh.javaUtil; 
  2.   
  3. import java.io.File; 
  4. import java.io.FileInputStream; 
  5. import java.io.FileNotFoundException; 
  6. import java.io.FileOutputStream; 
  7. import java.io.IOException; 
  8. import java.io.OutputStream; 
  9. import java.text.DecimalFormat; 
  10. import java.util.LinkedList; 
  11. import java.util.List; 
  12.   
  13. import javax.servlet.http.HttpServletResponse; 
  14.   
  15. import org.apache.poi.hssf.usermodel.HSSFCell; 
  16. import org.apache.poi.hssf.usermodel.HSSFRichTextString; 
  17. import org.apache.poi.hssf.usermodel.HSSFRow; 
  18. import org.apache.poi.hssf.usermodel.HSSFSheet; 
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
  20. import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 
  21. import org.apache.poi.ss.usermodel.Cell; 
  22. import org.apache.poi.ss.usermodel.DateUtil; 
  23. import org.apache.poi.ss.usermodel.Row; 
  24. import org.apache.poi.ss.usermodel.Sheet; 
  25. import org.apache.poi.ss.usermodel.Workbook; 
  26. import org.apache.poi.ss.usermodel.WorkbookFactory; 
  27.   
  28. /** 
  29.  * 封裝對(duì)excel的操作,包括本地讀寫excel和流中輸出excel,支持office 2007。<br/> 
  30.  * 依賴于poi-3.9-20121203.jar,poi-ooxml-3.9-20121203.jar,poi-ooxml-schemas-3.9- 
  31.  * 20121203.jar,dom4j-1.6.1.jar<br/> 
  32.  * 有參構(gòu)造函數(shù)參數(shù)為excel的全路徑<br/> 
  33.  *  
  34.  * @author 宋立君 
  35.  * @date 2014年07月03日 
  36.  */ 
  37. public class ExcelUtil { 
  38.   
  39.     // excel文件路徑 
  40.     private String path = ""
  41.   
  42.     // 寫入excel時(shí),是否自動(dòng)擴(kuò)展列寬度來符合內(nèi)容。 
  43.     private boolean autoColumnWidth = false
  44.   
  45.     /** 
  46.      * 無參構(gòu)造函數(shù) 默認(rèn) 
  47.      */ 
  48.     public ExcelUtil() { 
  49.     } 
  50.   
  51.     /** 
  52.      * 有參構(gòu)造函數(shù) 
  53.      *  
  54.      * @param path 
  55.      *   excel路徑 
  56.      */ 
  57.     public ExcelUtil(String path) { 
  58.         this.path = path; 
  59.     } 
  60.   
  61.     /** 
  62.      * 讀取某個(gè)工作簿上的所有單元格的值。 
  63.      *  
  64.      * @param sheetOrder 
  65.      *   工作簿序號(hào),從0開始。 
  66.      * @return List<Object[]> 所有單元格的值。 
  67.      * @throws IOException 
  68.      *    加載excel文件IO異常。 
  69.      * @throws FileNotFoundException 
  70.      *    excel文件沒有找到異常。 
  71.      * @throws InvalidFormatException 
  72.      * @author 宋立君 
  73.      * @date 2014年07月03日 
  74.      */ 
  75.     public List<Object[]> read(int sheetOrder) throws FileNotFoundException, 
  76.             IOException, InvalidFormatException { 
  77.         FileInputStream fis = new FileInputStream(path); 
  78.         Workbook workbook = WorkbookFactory.create(fis); 
  79.         if (fis != null) { 
  80.             fis.close(); 
  81.         } 
  82.         Sheet sheet = workbook.getSheetAt(sheetOrder); 
  83.         // 用來記錄excel值 
  84.         List<Object[]> valueList = new LinkedList<Object[]>(); 
  85.         // 循環(huán)遍歷每一行、每一列。 
  86.         for (Row row : sheet) { 
  87.             // 每一行 
  88.             Object[] rowObject = null
  89.             for (Cell cell : row) { 
  90.                 // cell.getCellType是獲得cell里面保存的值的type 
  91.                 switch (cell.getCellType()) { 
  92.                 case Cell.CELL_TYPE_BOOLEAN: 
  93.                     // 得到Boolean對(duì)象的方法 
  94.                     rowObject = CollectionUtil.addObjectToArray(rowObject, 
  95.                             cell.getBooleanCellValue()); 
  96.                     break
  97.                 case Cell.CELL_TYPE_NUMERIC: 
  98.                     // 先看是否是日期格式 
  99.                     if (DateUtil.isCellDateFormatted(cell)) { 
  100.                         // 讀取日期格式 
  101.                         rowObject = CollectionUtil.addObjectToArray(rowObject, 
  102.                                 cell.getDateCellValue()); 
  103.                     } else { 
  104.                         DecimalFormat df = new DecimalFormat(); 
  105.                         // 單元格的值,替換掉, 
  106.                         String value = df.format(cell.getNumericCellValue()) 
  107.                                 .replace(","""); 
  108.                         // 讀取數(shù)字 
  109.                         rowObject = CollectionUtil.addObjectToArray(rowObject, 
  110.                                 value); 
  111.                     } 
  112.                     break
  113.                 case Cell.CELL_TYPE_FORMULA: 
  114.                     // 讀取公式 
  115.                     rowObject = CollectionUtil.addObjectToArray(rowObject, 
  116.                             cell.getCellFormula()); 
  117.                     break
  118.                 case Cell.CELL_TYPE_STRING: 
  119.                     // 讀取String 
  120.                     rowObject = CollectionUtil.addObjectToArray(rowObject, cell 
  121.                             .getRichStringCellValue().toString()); 
  122.                     break
  123.                 } 
  124.             } 
  125.             // 將這行添加到list。 
  126.             valueList.add(rowObject); 
  127.         } 
  128.         return valueList; 
  129.     } 
  130.   
  131.     /** 
  132.      * 讀取某個(gè)工作簿上的某個(gè)單元格的值。 
  133.      *  
  134.      * @param sheetOrder 
  135.      *   工作簿序號(hào),從0開始。 
  136.      * @param colum 
  137.      *   列數(shù) 從1開始 
  138.      * @param row 
  139.      *   行數(shù) 從1開始 
  140.      * @return 單元格的值。 
  141.      * @throws Exception 
  142.      *    加載excel異常。 
  143.      * @author 宋立君 
  144.      * @date 2014年07月03日 
  145.      */ 
  146.     public String read(int sheetOrder, int colum, int row) throws Exception { 
  147.         FileInputStream fis = new FileInputStream(path); 
  148.         Workbook workbook = WorkbookFactory.create(fis); 
  149.         if (fis != null) { 
  150.             fis.close(); 
  151.         } 
  152.         Sheet sheet = workbook.getSheetAt(sheetOrder); 
  153.         Row rows = sheet.getRow(row - 1); 
  154.         Cell cell = rows.getCell(colum - 1); 
  155.         String content = cell.getStringCellValue(); 
  156.         return content; 
  157.     } 
  158.   
  159.     /** 
  160.      * 在指定的工作簿、行、列書寫值。 
  161.      *  
  162.      * @param sheetOrder 
  163.      *   工作簿序號(hào),基于0. 
  164.      * @param colum 
  165.      *   列 基于1 
  166.      * @param row 
  167.      *   行 基于1 
  168.      * @param content 
  169.      *   將要被書寫的內(nèi)容。 
  170.      * @throws Exception 
  171.      *    書寫后保存異常。 
  172.      * @author 宋立君 
  173.      * @date 2014年07月03日 
  174.      */ 
  175.     public void write(int sheetOrder, int colum, int row, String content) 
  176.             throws Exception { 
  177.         FileInputStream fis = new FileInputStream(path); 
  178.         Workbook workbook = WorkbookFactory.create(fis); 
  179.         if (fis != null) { 
  180.             fis.close(); 
  181.         } 
  182.         Sheet sheet = workbook.getSheetAt(sheetOrder); 
  183.         Row rows = sheet.createRow(row - 1); 
  184.         Cell cell = rows.createCell(colum - 1); 
  185.         cell.setCellValue(content); 
  186.         FileOutputStream fileOut = new FileOutputStream(path); 
  187.         workbook.write(fileOut); 
  188.         fileOut.close(); 
  189.   
  190.     } 
  191.   
  192.     /** 
  193.      * 得到一個(gè)工作區(qū)最后一條記錄的序號(hào),相當(dāng)于這個(gè)工作簿共多少行數(shù)據(jù)。 
  194.      *  
  195.      * @param sheetOrder 
  196.      *   工作區(qū)序號(hào) 
  197.      * @return int 序號(hào)。 
  198.      * @throws IOException 
  199.      *    根據(jù)excel路徑加載excel異常。 
  200.      * @throws InvalidFormatException 
  201.      * @author 宋立君 
  202.      * @date 2014年07月03日 
  203.      */ 
  204.     public int getSheetLastRowNum(int sheetOrder) throws IOException, 
  205.             InvalidFormatException { 
  206.         FileInputStream fis = new FileInputStream(path); 
  207.         Workbook workbook = WorkbookFactory.create(fis); 
  208.         if (fis != null) { 
  209.             fis.close(); 
  210.         } 
  211.         Sheet sheet = workbook.getSheetAt(sheetOrder); 
  212.         return sheet.getLastRowNum(); 
  213.     } 
  214.   
  215.     /** 
  216.      * 在磁盤生成一個(gè)含有內(nèi)容的excel,路徑為path屬性 
  217.      *  
  218.      * @param sheetName 
  219.      *   導(dǎo)出的sheet名稱 
  220.      * @param fieldName 
  221.      *   列名數(shù)組 
  222.      * @param data 
  223.      *   數(shù)據(jù)組 
  224.      * @throws IOException 
  225.      * @author 宋立君 
  226.      * @date 2014年07月03日 
  227.      */ 
  228.     public void makeExcel(String sheetName, String[] fieldName, 
  229.             List<Object[]> data) throws IOException { 
  230.         // 在內(nèi)存中生成工作薄 
  231.         HSSFWorkbook workbook = makeWorkBook(sheetName, fieldName, data); 
  232.         // 截取文件夾路徑 
  233.         String filePath = path.substring(0, path.lastIndexOf("\\")); 
  234.         // 如果路徑不存在,創(chuàng)建路徑 
  235.         File file = new File(filePath); 
  236.         // System.out.println(path+"-----------"+file.exists()); 
  237.         if (!file.exists()) 
  238.             file.mkdirs(); 
  239.         FileOutputStream fileOut = new FileOutputStream(path); 
  240.         workbook.write(fileOut); 
  241.         fileOut.close(); 
  242.     } 
  243.   
  244.     /** 
  245.      * 在輸出流中導(dǎo)出excel。 
  246.      *  
  247.      * @param excelName 
  248.      *   導(dǎo)出的excel名稱 包括擴(kuò)展名 
  249.      * @param sheetName 
  250.      *   導(dǎo)出的sheet名稱 
  251.      * @param fieldName 
  252.      *   列名數(shù)組 
  253.      * @param data 
  254.      *   數(shù)據(jù)組 
  255.      * @param response 
  256.      *   response 
  257.      * @throws IOException 
  258.      *    轉(zhuǎn)換流時(shí)IO錯(cuò)誤 
  259.      * @author 宋立君 
  260.      * @date 2014年07月03日 
  261.      */ 
  262.     public void makeStreamExcel(String excelName, String sheetName, 
  263.             String[] fieldName, List<Object[]> data, 
  264.             HttpServletResponse response) throws IOException { 
  265.         OutputStream os = null
  266.         response.reset(); // 清空輸出流 
  267.         os = response.getOutputStream(); // 取得輸出流 
  268.         response.setHeader("Content-disposition""attachment; filename=" 
  269.                 + new String(excelName.getBytes(), "ISO-8859-1")); // 設(shè)定輸出文件頭 
  270.         response.setContentType("application/msexcel"); // 定義輸出類型 
  271.         // 在內(nèi)存中生成工作薄 
  272.         HSSFWorkbook workbook = makeWorkBook(sheetName, fieldName, data); 
  273.         os.flush(); 
  274.         workbook.write(os); 
  275.     } 
  276.   
  277.     /** 
  278.      * 根據(jù)條件,生成工作薄對(duì)象到內(nèi)存。 
  279.      *  
  280.      * @param sheetName 
  281.      *   工作表對(duì)象名稱 
  282.      * @param fieldName 
  283.      *   首列列名稱 
  284.      * @param data 
  285.      *   數(shù)據(jù) 
  286.      * @return HSSFWorkbook 
  287.      * @author 宋立君 
  288.      * @date 2014年07月03日 
  289.      */ 
  290.     private HSSFWorkbook makeWorkBook(String sheetName, String[] fieldName, 
  291.             List<Object[]> data) { 
  292.         // 用來記錄最大列寬,自動(dòng)調(diào)整列寬。 
  293.         Integer collength[] = new Integer[fieldName.length]; 
  294.   
  295.         // 產(chǎn)生工作薄對(duì)象 
  296.         HSSFWorkbook workbook = new HSSFWorkbook(); 
  297.         // 產(chǎn)生工作表對(duì)象 
  298.         HSSFSheet sheet = workbook.createSheet(); 
  299.         // 為了工作表能支持中文,設(shè)置字符集為UTF_16 
  300.         workbook.setSheetName(0, sheetName); 
  301.         // 產(chǎn)生一行 
  302.         HSSFRow row = sheet.createRow(0); 
  303.         // 產(chǎn)生單元格 
  304.         HSSFCell cell; 
  305.         // 寫入各個(gè)字段的名稱 
  306.         for (int i = 0; i < fieldName.length; i++) { 
  307.             // 創(chuàng)建第一行各個(gè)字段名稱的單元格 
  308.             cell = row.createCell((short) i); 
  309.             // 設(shè)置單元格內(nèi)容為字符串型 
  310.             cell.setCellType(HSSFCell.CELL_TYPE_STRING); 
  311.             // 為了能在單元格中輸入中文,設(shè)置字符集為UTF_16 
  312.             // cell.setEncoding(HSSFCell.ENCODING_UTF_16); 
  313.             // 給單元格內(nèi)容賦值 
  314.             cell.setCellValue(new HSSFRichTextString(fieldName[i])); 
  315.             // 初始化列寬 
  316.             collength[i] = fieldName[i].getBytes().length; 
  317.         } 
  318.         // 臨時(shí)單元格內(nèi)容 
  319.         String tempCellContent = ""
  320.         // 寫入各條記錄,每條記錄對(duì)應(yīng)excel表中的一行 
  321.         for (int i = 0; i < data.size(); i++) { 
  322.             Object[] tmp = data.get(i); 
  323.             // 生成一行 
  324.             row = sheet.createRow(i + 1); 
  325.             for (int j = 0; j < tmp.length; j++) { 
  326.                 cell = row.createCell((short) j); 
  327.                 // 設(shè)置單元格字符類型為String 
  328.                 cell.setCellType(HSSFCell.CELL_TYPE_STRING); 
  329.                 tempCellContent = (tmp[j] == null) ? "" : tmp[j].toString(); 
  330.                 cell.setCellValue(new HSSFRichTextString(tempCellContent)); 
  331.   
  332.                 // 如果自動(dòng)調(diào)整列寬度。 
  333.                 if (autoColumnWidth) { 
  334.                     if (j >= collength.length) { // 標(biāo)題列數(shù)小于數(shù)據(jù)列數(shù)時(shí)。 
  335.                         collength = CollectionUtil.addObjectToArray(collength, 
  336.                                 tempCellContent.getBytes().length); 
  337.                     } else { 
  338.                         // 如果這個(gè)內(nèi)容的寬度大于之前最大的,就按照這個(gè)設(shè)置寬度。 
  339.                         if (collength[j] < tempCellContent.getBytes().length) { 
  340.                             collength[j] = tempCellContent.getBytes().length; 
  341.                         } 
  342.                     } 
  343.                 } 
  344.             } 
  345.         } 
  346.   
  347.         // 自動(dòng)調(diào)整列寬度。 
  348.         if (autoColumnWidth) { 
  349.             // 調(diào)整列為這列文字對(duì)應(yīng)的最大寬度。 
  350.             for (int i = 0; i < fieldName.length; i++) { 
  351.                 sheet.setColumnWidth(i, collength[i] * 2 * 256); 
  352.             } 
  353.         } 
  354.         return workbook; 
  355.     } 
  356.   
  357.     /** 
  358.      * 功能:設(shè)置寫入excel時(shí),是否自動(dòng)擴(kuò)展列寬度來符合內(nèi)容,默認(rèn)為false。 
  359.      *  
  360.      * @author 宋立君 
  361.      * @date 2014年07月03日 
  362.      * @param autoColumnWidth 
  363.      *   true或者false 
  364.      */ 
  365.     public void setAutoColumnWidth(boolean autoColumnWidth) { 
  366.         this.autoColumnWidth = autoColumnWidth; 
  367.     } 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: aigao视频| 天天干天天操天天碰 | 国产一区二区三区在线观看视频 | 久久精品动漫99精品动漫 | 国产成人盗摄精品 | 激情三级做爰在线观看激情 | 女子监狱第二季在线观看免费完整版 | 亚洲欧美日韩天堂在线观看 | 国产精品网站在线观看 | 4444kk在线看片| 大学生按摩黄a级中文片 | 欧美亚洲国产成人不卡 | 四虎影音在线 | 亚州人成网在线播放 | 欧美性理论片在线观看片免费 | 日韩毛片免费在线观看 | 99久久免费视频 | 黑人操日本妞 | 精品综合在线 | 久久免费黄色 | 国产精品永久免费视频 | 国产亚洲精aa在线观看香蕉 | 福利片中文 | 富士av105| 欧美kkk4444在线观看 | 黄 色 大 片 网站 | 日本黄色录像视频 | 久久免费看少妇高潮A片特爽 | 逼里逼里香 | 久久亚洲精品专区蓝色区 | 我在厨房摸岳的乳HD在线观看 | beeg最新| 国产99久久久国产精品成人 | 天堂久久久久va久久久久 | 丰满肥臀风间由美357在线 | 亚洲 欧美 在线观看 | 亚洲狠狠网站色噜噜 | 美女脱了内裤打开腿让人羞羞软件 | 40分钟在线观看免费 | 美女被上漫画 | 小sao货水好多真紧h的视频 |