[Java] POI Excel读取

 

 

  1. 作者:王卫东
  2. 来源:CSDN
  3. 原文:https://blog.csdn.net/wwd0501/article/details/78780646

 

pom

POI版本3.15

pom文件如下:

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>

excle文件

Java代码实现

  1 /**
  2 * 
  3 */
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.text.SimpleDateFormat;
  9 
 10 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 11 import org.apache.poi.ss.usermodel.Cell;
 12 import org.apache.poi.ss.usermodel.Row;
 13 import org.apache.poi.ss.usermodel.Sheet;
 14 import org.apache.poi.ss.usermodel.Workbook;
 15 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 16 
 17 /**
 18 *
 19 * Description: Excel操作
 20 * 
 21 * CreateTime: 2017年12月11日 下午3:08:09
 22 *
 23 * Change History:
 24 *
 25 * Date CR Number Name Description of change
 26 *
 27 */
 28 public class ExcelUtil {
 29 
 30 private static final String EXCEL_XLS = "xls"; 
 31 private static final String EXCEL_XLSX = "xlsx"; 
 32 
 33 /** 
 34 * 判断Excel的版本,获取Workbook 
 35 * @param in 
 36 * @param filename 
 37 * @return 
 38 * @throws IOException 
 39 */ 
 40 public static Workbook getWorkbok(InputStream in,File file) throws IOException{ 
 41 Workbook wb = null; 
 42 if(file.getName().endsWith(EXCEL_XLS)){ //Excel 2003 
 43 wb = new HSSFWorkbook(in); 
 44 }else if(file.getName().endsWith(EXCEL_XLSX)){ // Excel 2007/2010 
 45 wb = new XSSFWorkbook(in); 
 46 } 
 47 return wb; 
 48 } 
 49 
 50 /** 
 51 * 判断文件是否是excel 
 52 * @throws Exception 
 53 */ 
 54 public static void checkExcelVaild(File file) throws Exception{ 
 55 if(!file.exists()){ 
 56 throw new Exception("文件不存在"); 
 57 } 
 58 if(!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))){ 
 59 throw new Exception("文件不是Excel"); 
 60 } 
 61 } 
 62 
 63 /** 
 64 * 读取Excel测试,兼容 Excel 2003/2007/2010 
 65 * @throws Exception 
 66 */ 
 67 public static void main(String[] args) throws Exception { 
 68 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); 
 69 try { 
 70 // 同时支持Excel 2003、2007 
 71 File excelFile = new File("d:/product.xlsx"); // 创建文件对象 
 72 FileInputStream in = new FileInputStream(excelFile); // 文件流 
 73 checkExcelVaild(excelFile); 
 74 Workbook workbook = getWorkbok(in,excelFile); 
 75 //Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel2003/2007/2010都是可以处理的 
 76 
 77 int sheetCount = workbook.getNumberOfSheets(); // Sheet的数量 
 78 /** 
 79 * 设置当前excel中sheet的下标:0开始 
 80 */ 
 81 // Sheet sheet = workbook.getSheetAt(0); // 遍历第一个Sheet 
 82 Sheet sheet = workbook.getSheetAt(2); // 遍历第三个Sheet 
 83 
 84 //获取总行数
 85 // System.out.println(sheet.getLastRowNum());
 86 
 87 // 为跳过第一行目录设置count 
 88 int count = 0;
 89 for (Row row : sheet) {
 90 try {
 91 // 跳过第一和第二行的目录 
 92 if(count < 2 ) {
 93 count++; 
 94 continue; 
 95 }
 96 
 97 //如果当前行没有数据,跳出循环 
 98 if(row.getCell(0).toString().equals("")){ 
 99 return;
100 }
101 
102 //获取总列数(空格的不计算)
103 int columnTotalNum = row.getPhysicalNumberOfCells();
104 System.out.println("总列数:" + columnTotalNum);
105 
106 System.out.println("最大列数:" + row.getLastCellNum());
107 
108 //for循环的,不扫描空格的列
109 // for (Cell cell : row) { 
110 // System.out.println(cell);
111 // }
112 int end = row.getLastCellNum();
113 for (int i = 0; i < end; i++) {
114 Cell cell = row.getCell(i);
115 if(cell == null) {
116 System.out.print("null" + "\t");
117 continue;
118 }
119 
120 Object obj = getValue(cell);
121 System.out.print(obj + "\t");
122 }
123 } catch (Exception e) {
124 e.printStackTrace();
125 }
126 } 
127 } catch (Exception e) { 
128 e.printStackTrace(); 
129 }
130 }
131 
132 private static Object getValue(Cell cell) {
133 Object obj = null;
134 switch (cell.getCellTypeEnum()) {
135 case BOOLEAN:
136 obj = cell.getBooleanCellValue(); 
137 break;
138 case ERROR:
139 obj = cell.getErrorCellValue(); 
140 break;
141 case NUMERIC:
142 obj = cell.getNumericCellValue(); 
143 break;
144 case STRING:
145 obj = cell.getStringCellValue(); 
146 break;
147 default:
148 break;
149 }
150 return obj;
151 }
152 }

 

posted @ 2019-04-13 16:21  一朵盛开的腊梅花  阅读(187)  评论(0编辑  收藏  举报