poi讀取excell表格

 

最近項目須要實現一個將excell中的數據導入數據庫,在網上找到這篇文章,html

 

一:導入maven依賴或相關jar包java

poi-3.17.jar  下載地址:點擊打開連接
poi-ooxml-3.17.jar  下載地址:點擊打開連接
poi-ooxml-schemas-3.17.jar  下載地址:點擊打開連接數據庫

 

二:編寫ImportExcellUtilsapache

[html] view plain copy
  1. package com.debo.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.text.DecimalFormat;  
  8. import java.text.SimpleDateFormat;  
  9. import java.util.LinkedList;  
  10. import java.util.List;  
  11.   
  12. import org.apache.poi.hssf.usermodel.HSSFCell;  
  13. import org.apache.poi.hssf.usermodel.HSSFDateUtil;  
  14. import org.apache.poi.hssf.usermodel.HSSFRow;  
  15. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  16. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  17. import org.apache.poi.xssf.usermodel.XSSFCell;  
  18. import org.apache.poi.xssf.usermodel.XSSFRow;  
  19. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  20. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  21.   
  22. public class ImportExcellUtils {  
  23.     /**  
  24.      * 對外提供讀取excel 的方法  
  25.      * */  
  26.     public static List<List<Object>> readExcel(File file) throws IOException {  
  27.         String fileName = file.getName();  
  28.         String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName  
  29.                 .substring(fileName.lastIndexOf(".") + 1);  
  30.         if ("xls".equals(extension)) {  
  31.             return read2003Excel(file);  
  32.         } else if ("xlsx".equals(extension)) {  
  33.             return read2007Excel(file);  
  34.         } else {  
  35.             throw new IOException("不支持的文件類型");  
  36.         }  
  37.     }  
  38.   
  39.     /**  
  40.      * 讀取 office 2003 excel  
  41.      *   
  42.      * @throws IOException  
  43.      * @throws FileNotFoundException  
  44.      */  
  45.     private static List<List<Object>> read2003Excel(File file)  
  46.             throws IOException {  
  47.         List<List<Object>> list = new LinkedList<List<Object>>();  
  48.         HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));  
  49.         HSSFSheet sheet = hwb.getSheetAt(0);  
  50.         Object value = null;  
  51.         HSSFRow row = null;  
  52.         HSSFCell cell = null;  
  53.         int counter = 0;  
  54.         for (int i = sheet.getFirstRowNum(); counter < sheet  
  55.                 .getPhysicalNumberOfRows(); i++) {  
  56.             row = sheet.getRow(i);  
  57.             if (row == null) {  
  58.                 continue;  
  59.             } else {  
  60.                 counter++;  
  61.             }  
  62.             List<Object> linked = new LinkedList<Object>();  
  63.             for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {  
  64.                 cell = row.getCell(j);  
  65.                 if (cell == null) {  
  66.                     continue;  
  67.                 }  
  68.                 DecimalFormat df = new DecimalFormat("0");// 格式化 number String  
  69.                                                             // 字符  
  70.                 SimpleDateFormat sdf = new SimpleDateFormat(  
  71.                         "yyyy-MM-dd HH:mm:ss");// 格式化日期字符串  
  72.                 DecimalFormat nf = new DecimalFormat("0.00");// 格式化數字  
  73.                 switch (cell.getCellType()) {  
  74.                 case XSSFCell.CELL_TYPE_STRING:  
  75.                     System.out.println(i + "行" + j + " 列 is String type");  
  76.                     value = cell.getStringCellValue();  
  77.                     break;  
  78.                 case XSSFCell.CELL_TYPE_NUMERIC:  
  79.                     System.out.println(i + "行" + j  
  80.                             + " 列 is Number type ; DateFormt:"  
  81.                             + cell.getCellStyle().getDataFormatString());  
  82.                     if ("@".equals(cell.getCellStyle().getDataFormatString())) {  
  83.                         value = df.format(cell.getNumericCellValue());  
  84.                     } else if ("General".equals(cell.getCellStyle()  
  85.                             .getDataFormatString())) {  
  86.                         value = nf.format(cell.getNumericCellValue());  
  87.                     } else {  
  88.                         value = sdf.format(HSSFDateUtil.getJavaDate(cell  
  89.                                 .getNumericCellValue()));  
  90.                     }  
  91.                     break;  
  92.                 case XSSFCell.CELL_TYPE_BOOLEAN:  
  93.                     System.out.println(i + "行" + j + " 列 is Boolean type");  
  94.                     value = cell.getBooleanCellValue();  
  95.                     break;  
  96.                 case XSSFCell.CELL_TYPE_BLANK:  
  97.                     System.out.println(i + "行" + j + " 列 is Blank type");  
  98.                     value = "";  
  99.                     break;  
  100.                 default:  
  101.                     System.out.println(i + "行" + j + " 列 is default type");  
  102.                     value = cell.toString();  
  103.                 }  
  104.                 if (value == null || "".equals(value)) {  
  105.                     continue;  
  106.                 }  
  107.                 linked.add(value);  
  108.             }  
  109.             list.add(linked);  
  110.         }  
  111.         return list;  
  112.     }  
  113.   
  114.     /**  
  115.      * 讀取Office 2007 excel  
  116.      * */  
  117.     private static List<List<Object>> read2007Excel(File file)  
  118.             throws IOException {  
  119.         List<List<Object>> list = new LinkedList<List<Object>>();  
  120.         // 構造 XSSFWorkbook 對象,strPath 傳入文件路徑  
  121.         XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));  
  122.         // 讀取第一章表格內容  
  123.         XSSFSheet sheet = xwb.getSheetAt(0);  
  124.         Object value = null;  
  125.         XSSFRow row = null;  
  126.         XSSFCell cell = null;  
  127.         int counter = 0;  
  128.         for (int i = sheet.getFirstRowNum(); counter < sheet  
  129.                 .getPhysicalNumberOfRows(); i++) {  
  130.             row = sheet.getRow(i);  
  131.             if (row == null) {  
  132.                 continue;  
  133.             } else {  
  134.                 counter++;  
  135.             }  
  136.             List<Object> linked = new LinkedList<Object>();  
  137.             for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {  
  138.                 cell = row.getCell(j);  
  139.                 if (cell == null) {  
  140.                     continue;  
  141.                 }  
  142.                 DecimalFormat df = new DecimalFormat("0");// 格式化 number String  
  143.                                                             // 字符  
  144.                 SimpleDateFormat sdf = new SimpleDateFormat(  
  145.                         "yyyy-MM-dd HH:mm:ss");// 格式化日期字符串  
  146.                 DecimalFormat nf = new DecimalFormat("0.00");// 格式化數字  
  147.                 switch (cell.getCellType()) {  
  148.                 case XSSFCell.CELL_TYPE_STRING:  
  149.                     System.out.println(i + "行" + j + " 列 is String type");  
  150.                     value = cell.getStringCellValue();  
  151.                     break;  
  152.                 case XSSFCell.CELL_TYPE_NUMERIC:  
  153.                     System.out.println(i + "行" + j  
  154.                             + " 列 is Number type ; DateFormt:"  
  155.                             + cell.getCellStyle().getDataFormatString());  
  156.                     if ("@".equals(cell.getCellStyle().getDataFormatString())) {  
  157.                         value = df.format(cell.getNumericCellValue());  
  158.                     } else if ("General".equals(cell.getCellStyle()  
  159.                             .getDataFormatString())) {  
  160.                         value = nf.format(cell.getNumericCellValue());  
  161.                     } else {  
  162.                         value = sdf.format(HSSFDateUtil.getJavaDate(cell  
  163.                                 .getNumericCellValue()));  
  164.                     }  
  165.                     break;  
  166.                 case XSSFCell.CELL_TYPE_BOOLEAN:  
  167.                     System.out.println(i + "行" + j + " 列 is Boolean type");  
  168.                     value = cell.getBooleanCellValue();  
  169.                     break;  
  170.                 case XSSFCell.CELL_TYPE_BLANK:  
  171.                     System.out.println(i + "行" + j + " 列 is Blank type");  
  172.                     value = "";  
  173.                     break;  
  174.                 default:  
  175.                     System.out.println(i + "行" + j + " 列 is default type");  
  176.                     value = cell.toString();  
  177.                 }  
  178.                 if (value == null || "".equals(value)) {  
  179.                     continue;  
  180.                 }  
  181.                 linked.add(value);  
  182.             }  
  183.             list.add(linked);  
  184.         }  
  185.         return list;  
  186.     }  
  187.   
  188. }  



三:測試xss

[html] view plain copy
  1. package com.debo.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.List;  
  6.   
  7. public class Test {  
  8.       
  9.     public static void main(String[] args) throws IOException {  
  10.         File file = new File("D:/2.xlsx");  
  11.         List<List<Object>> readExcel = ImportExcellUtils.readExcel(file);  
  12.         for (int i = 0; i < readExcel.size(); i++) {  
  13.             System.out.println(readExcel.get(i));  
  14.         }  
  15.     }  
  16.   
  17. }  
相關文章
相關標籤/搜索