• poi读取excell表格


    原文链接:http://blog.csdn.net/qq_37936542/article/details/79024847

    最近项目需要实现一个将excell中的数据导入数据库,在网上找到这篇文章,原文地址:点击打开链接


    一:导入maven依赖或相关jar包

    poi-3.17.jar  下载地址:点击打开链接
    poi-ooxml-3.17.jar  下载地址:点击打开链接
    poi-ooxml-schemas-3.17.jar  下载地址:点击打开链接


    二:编写ImportExcellUtils

    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. }  


    三:测试

    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. }  

    文末福利:

    福利一:前端,Java,产品经理,微信小程序,Python等8G资源合集大放送:https://www.jianshu.com/p/e8197d4d9880

    福利二:微信小程序入门与实战全套详细视频教程

    领取方式:
    如果需要学习视频,欢迎关注 【编程微刊】微信公众号,回复【领取资源】一键领取以下所有干货资源,获取更多有用技术干货、文档资料。所有文档会持续更新,欢迎关注一起成长!



  • 相关阅读:
    android调用google地图
    AndroidManifest.xml中android:configChanges的简介
    android消息推送机制
    用j4lChartAndroid实现3D图饼
    读《重构改善既有代码的设计》笔记一
    怎样删除data下的多余文件
    Mac OSX 10.8 下 配置 mysql+python+thrift开发环境
    新的征程
    练习1 四则运算的程序
    作业二四则运算
  • 原文地址:https://www.cnblogs.com/wangting888/p/9701639.html
Copyright © 2020-2023  润新知