Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
HSSF - 提供读写Microsoft Excel格式档案的功能。
XSSF - 提供读写Microsoft OOXML格式档案的功能。
以下是项目工程结构图:
使用POI解析EXCEL文件需要用到POI相关的jar包,这些jar包可以在apache官网上去下载:http://poi.apache.org/download.html;
这里我使用的jar包版本为poi-3.14-beta1-20151223.jar
相关代码如下:
Excel文件解析接口 IExcelParse.java
1 /* 2 * IExcelParse.java 3 * 4 * 2016-1-6 下午4:45:53 5 * 6 * RecluseKapoor 7 * 8 * Copyright © 2016, RecluseKapoor. All rights reserved. 9 * 10 */ 11 package com.rk.pub.poi.excel; 12 13 /** 14 * @Title: recluse-Excel文件解析接口 15 * 16 * @Description:Excel文件解析接口,所有版本的Excel解析类都要实现该接口 17 * 18 * @Company: 卡普工作室 19 * 20 * @Website: http://www.cnblogs.com/reclusekapoor/ 21 * 22 * @author: RecluseKapoor 23 * 24 * @CreateDate:2016-1-6 下午9:42:08 25 * 26 * @version: 1.0 27 * 28 * @lastModify: 29 * 30 */ 31 public interface IExcelParse { 32 public void loadExcel(String path) throws Exception; 33 34 public String getSheetName(int sheetNo); 35 36 public int getSheetCount() throws Exception; 37 38 public int getRowCount(int sheetNo); 39 40 public int getRealRowCount(int sheetNo); 41 42 public String readExcelByRowAndCell(int sheetNo, int rowNo, int cellNo) 43 throws Exception; 44 45 public String[] readExcelByRow(int sheetNo, int rowNo) throws Exception; 46 47 public String[] readExcelByCell(int sheetNo, int cellNo) throws Exception; 48 49 public void close(); 50 }
1 /* 2 * ExcelParse2003.java 3 * 4 * 2016-1-6 下午4:45:53 5 * 6 * RecluseKapoor 7 * 8 * Copyright © 2016, RecluseKapoor. All rights reserved. 9 * 10 */ 11 package com.rk.pub.poi.excel; 12 13 import java.io.FileInputStream; 14 import java.io.FileNotFoundException; 15 import java.io.IOException; 16 import java.sql.Timestamp; 17 import java.text.DecimalFormat; 18 import java.util.Date; 19 20 import org.apache.poi.hssf.usermodel.HSSFCell; 21 import org.apache.poi.hssf.usermodel.HSSFDateUtil; 22 import org.apache.poi.hssf.usermodel.HSSFRow; 23 import org.apache.poi.hssf.usermodel.HSSFSheet; 24 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 25 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 26 27 /** 28 * @Title: recluse--2003版Excel文件解析工具 29 * 30 * @Description: 解析2003版Excel文件具体实现类 31 * 32 * @Company: 卡普工作室 33 * 34 * @Website: http://www.cnblogs.com/reclusekapoor/ 35 * 36 * @author: RecluseKapoor 37 * 38 * @CreateDate:2016-1-6 下午9:59:51 39 * 40 * @version: 1.0 41 * 42 * @lastModify: 43 * 44 */ 45 public class ExcelParse2003 implements IExcelParse { 46 // Excel工作区 47 private HSSFWorkbook wb = null; 48 49 /** 50 * 加载excel文件,获取excel工作区 51 * 52 * @param filePathAndName 53 * @throws FileNotFoundException 54 * @throws IOException 55 */ 56 @Override 57 public void loadExcel(String filePathAndName) throws FileNotFoundException, 58 IOException { 59 FileInputStream fis = null; 60 POIFSFileSystem fs = null; 61 try { 62 fis = new FileInputStream(filePathAndName); 63 fs = new POIFSFileSystem(fis); 64 wb = new HSSFWorkbook(fs); 65 } catch (FileNotFoundException e) { 66 e.printStackTrace(); 67 throw new FileNotFoundException("加载Excel文件失败:" + e.getMessage()); 68 } catch (IOException e) { 69 e.printStackTrace(); 70 throw new IOException("加载Excel文件失败:" + e.getMessage()); 71 } finally { 72 if (fis != null) { 73 fis.close(); 74 fis = null; 75 } 76 if (fs != null) { 77 fs.close(); 78 } 79 } 80 } 81 82 /** 83 * 获取sheet页名称 84 * 85 * @param sheetNo 86 * @return 87 */ 88 public String getSheetName(int sheetNo) { 89 return wb.getSheetName(sheetNo - 1); 90 } 91 92 /** 93 * 获取sheet页数 94 * 95 * @return int 96 */ 97 public int getSheetCount() throws Exception { 98 int sheetCount = wb.getNumberOfSheets(); 99 if (sheetCount == 0) { 100 throw new Exception("Excel中没有SHEET页"); 101 } 102 return sheetCount; 103 } 104 105 /** 106 * 获取sheetNo页行数 107 * 108 * @param sheetNo 109 * @return 110 */ 111 public int getRowCount(int sheetNo) { 112 int rowCount = 0; 113 HSSFSheet sheet = wb.getSheetAt(sheetNo - 1); 114 rowCount = sheet.getLastRowNum(); 115 return rowCount; 116 } 117 118 /** 119 * 获取sheetNo页行数(含有操作或者内容的真实行数) 120 * 121 * @param sheetNo 122 * @return 123 */ 124 public int getRealRowCount(int sheetNo) { 125 int rowCount = 0; 126 int rowNum = 0; 127 HSSFSheet sheet = wb.getSheetAt(sheetNo - 1); 128 rowCount = sheet.getLastRowNum(); 129 if (rowCount == 0) { 130 return rowCount; 131 } 132 HSSFRow row = null; 133 HSSFCell cell = null; 134 rowNum = rowCount; 135 for (int i = 0; i < rowCount; i++) { 136 row = sheet.getRow(rowNum); 137 rowNum--; 138 if (row == null) { 139 continue; 140 } 141 short firstCellNum = row.getFirstCellNum(); 142 short lastCellNum = row.getLastCellNum(); 143 for (int j = firstCellNum; j < lastCellNum; j++) { 144 cell = row.getCell(j); 145 if (cell == null) { 146 continue; 147 } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) { 148 continue; 149 } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { 150 String value = cell.getStringCellValue(); 151 if (value == null || value.equals("")) { 152 continue; 153 } else { 154 value = value.trim(); 155 if (value.isEmpty() || value.equals("") 156 || value.length() == 0) { 157 continue; 158 } 159 } 160 } 161 rowCount = rowNum + 1; 162 return rowCount; 163 } 164 } 165 rowCount = rowNum; 166 return rowCount; 167 } 168 169 /** 170 * 读取第sheetNo个sheet页中第rowNo行第cellNo列的数据 171 * 172 * @param sheetNo 173 * sheet页编号 174 * @param rowNo 175 * 行号 176 * @param cellNo 177 * 列号 178 * @return 返回相应的excel单元格内容 179 * @throws Exception 180 */ 181 public String readExcelByRowAndCell(int sheetNo, int rowNo, int cellNo) 182 throws Exception { 183 String rowCellData = ""; 184 sheetNo = sheetNo - 1; 185 HSSFSheet sheet = wb.getSheetAt(sheetNo); 186 String sheetName = wb.getSheetName(sheetNo); 187 try { 188 HSSFRow row = sheet.getRow(rowNo - 1); 189 if (row == null) { 190 return "NoData"; 191 } 192 HSSFCell cell = row.getCell((cellNo - 1)); 193 if (cell == null) { 194 return "NoData"; 195 } 196 int cellType = cell.getCellType(); 197 if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {// 数值(包括excel中数值、货币、日期、时间、会计专用等单元格格式) 198 //判断数值是否为日期或时间;但是该判断方法存在漏洞,只能识别一种日期格式。 199 if (HSSFDateUtil.isCellDateFormatted(cell)) {//日期、时间 200 double d = cell.getNumericCellValue(); 201 Date date = HSSFDateUtil.getJavaDate(d); 202 Timestamp timestamp = new Timestamp(date.getTime()); 203 String temp = timestamp.toString(); 204 if (temp.endsWith("00:00:00.0")) { 205 rowCellData = temp.substring(0, 206 temp.lastIndexOf("00:00:00.0")); 207 } else if (temp.endsWith(".0")) { 208 rowCellData = temp.substring(0, temp.lastIndexOf(".0")); 209 } else { 210 rowCellData = timestamp.toString(); 211 } 212 } else {//数值、货币、会计专用、百分比、分数、科学记数 单元格式 213 rowCellData = new DecimalFormat("0.########").format(cell 214 .getNumericCellValue()); 215 } 216 } else if (cellType == HSSFCell.CELL_TYPE_STRING) {// 字符串 217 rowCellData = cell.getStringCellValue(); 218 } else if (cellType == HSSFCell.CELL_TYPE_FORMULA) {// 公式 219 double d = cell.getNumericCellValue(); 220 rowCellData = String.valueOf(d); 221 } else if (cellType == HSSFCell.CELL_TYPE_BLANK) {// 空值 222 rowCellData = ""; 223 } else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {// boolean值 224 rowCellData = ""; 225 } else if (cellType == HSSFCell.CELL_TYPE_ERROR) {// 异常 226 rowCellData = ""; 227 } else { 228 229 } 230 } catch (Exception e) { 231 e.printStackTrace(); 232 throw new Exception(sheetName + "sheet页中" + "第" + rowNo + "行" + "第" 233 + cellNo + "列" + "数据不符合要求,请检查sheet页"); 234 } 235 return rowCellData; 236 } 237 238 /** 239 * 读取第sheetNo个sheet页中第rowNo行的数据 240 * 241 * @param sheetNo 242 * 指定sheetNo页 243 * @param rowNo 244 * 指定rowNo行 245 * @return 返回第rowNo行的数据 246 * @throws Exception 247 */ 248 public String[] readExcelByRow(int sheetNo, int rowNo) throws Exception { 249 String[] rowData = null; 250 HSSFSheet sheet = wb.getSheetAt(sheetNo - 1); 251 HSSFRow row = sheet.getRow(rowNo - 1); 252 int cellCount = row.getLastCellNum(); 253 rowData = new String[cellCount]; 254 for (int k = 1; k <= cellCount; k++) { 255 rowData[k - 1] = readExcelByRowAndCell(sheetNo, rowNo, k); 256 } 257 return rowData; 258 } 259 260 /** 261 * 读取第sheetNo个sheet页中第cellNo列的数据 262 * 263 * @param sheetNo 264 * 指定sheetNo页 265 * @param cellNo 266 * 指定cellNo列号 267 * @return 返回第cellNo列的数据 268 * @throws Exception 269 */ 270 public String[] readExcelByCell(int sheetNo, int cellNo) throws Exception { 271 String[] cellData = null; 272 HSSFSheet sheet = wb.getSheetAt(sheetNo - 1); 273 int rowCount = sheet.getLastRowNum(); 274 cellData = new String[rowCount + 1]; 275 for (int i = 0; i <= rowCount; i++) { 276 cellData[i] = readExcelByRowAndCell(sheetNo - 1, i, cellNo - 1); 277 } 278 return cellData; 279 } 280 281 /** 282 * 关闭excel工作区,释放资源 283 * 284 * @throws Exception 285 */ 286 @Override 287 public void close() { 288 if (wb != null) { 289 try { 290 wb.close(); 291 wb = null; 292 } catch (IOException e) { 293 e.printStackTrace(); 294 } 295 } 296 } 297 }
1 /* 2 * ExcelParse2007.java 3 * 4 * 2016-1-6 下午4:45:53 5 * 6 * RecluseKapoor 7 * 8 * Copyright © 2016, RecluseKapoor. All rights reserved. 9 * 10 */ 11 package com.rk.pub.poi.excel; 12 13 import java.io.FileInputStream; 14 import java.io.FileNotFoundException; 15 import java.io.IOException; 16 import java.sql.Timestamp; 17 import java.text.DecimalFormat; 18 import java.util.Date; 19 20 import org.apache.poi.ss.usermodel.DateUtil; 21 import org.apache.poi.xssf.usermodel.XSSFCell; 22 import org.apache.poi.xssf.usermodel.XSSFRow; 23 import org.apache.poi.xssf.usermodel.XSSFSheet; 24 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 25 26 /** 27 * @Title: recluse--2007版Excel文件解析工具 28 * 29 * @Description: 解析2007版Excel文件具体实现类 30 * 31 * @Company: 卡普工作室 32 * 33 * @Website: http://www.cnblogs.com/reclusekapoor/ 34 * 35 * @author: RecluseKapoor 36 * 37 * @CreateDate:2016-1-6 下午9:51:15 38 * 39 * @version: 1.0 40 * 41 * @lastModify: 42 * 43 */ 44 public class ExcelParse2007 implements IExcelParse { 45 // Excel工作区 46 private XSSFWorkbook wb = null; 47 48 /** 49 * 加载excel文件,获取excel工作区 50 * 51 * @param filePathAndName 52 * @throws FileNotFoundException 53 * @throws IOException 54 */ 55 public void loadExcel(String filePathAndName) throws FileNotFoundException, 56 IOException { 57 FileInputStream fis = null; 58 try { 59 fis = new FileInputStream(filePathAndName); 60 wb = new XSSFWorkbook(fis); 61 } catch (FileNotFoundException e) { 62 e.printStackTrace(); 63 throw new FileNotFoundException("加载Excel文件失败:" + e.getMessage()); 64 } catch (IOException e) { 65 e.printStackTrace(); 66 throw new IOException("加载Excel文件失败:" + e.getMessage()); 67 } finally { 68 if (fis != null) { 69 fis.close(); 70 fis = null; 71 } 72 } 73 } 74 75 /** 76 * 获取sheet页名称 77 * 78 * @param sheetNo 79 * @return 80 */ 81 public String getSheetName(int sheetNo) { 82 return wb.getSheetName(sheetNo - 1); 83 } 84 85 /** 86 * 获取sheet页数 87 * 88 * @return int 89 */ 90 public int getSheetCount() throws Exception { 91 int sheetCount = wb.getNumberOfSheets(); 92 if (sheetCount == 0) { 93 throw new Exception("Excel中没有SHEET页"); 94 } 95 return sheetCount; 96 } 97 98 /** 99 * 获取sheetNo页行数 100 * 101 * @param sheetNo 102 * @return 103 */ 104 public int getRowCount(int sheetNo) { 105 int rowCount = 0; 106 XSSFSheet sheet = wb.getSheetAt(sheetNo - 1); 107 rowCount = sheet.getLastRowNum(); 108 return rowCount; 109 } 110 111 /** 112 * 获取sheetNo页行数(含有操作或者内容的真实行数) 113 * 114 * @param sheetNo 115 * @return 116 */ 117 public int getRealRowCount(int sheetNo) { 118 int rowCount = 0; 119 int rowNum = 0; 120 XSSFSheet sheet = wb.getSheetAt(sheetNo - 1); 121 rowCount = sheet.getLastRowNum(); 122 if (rowCount == 0) { 123 return rowCount; 124 } 125 XSSFRow row = null; 126 XSSFCell cell = null; 127 rowNum = rowCount; 128 for (int i = 0; i < rowCount; i++) { 129 row = sheet.getRow(rowNum); 130 rowNum--; 131 if (row == null) { 132 continue; 133 } 134 short firstCellNum = row.getFirstCellNum(); 135 short lastCellNum = row.getLastCellNum(); 136 for (int j = firstCellNum; j < lastCellNum; j++) { 137 cell = row.getCell(j); 138 if (cell == null) { 139 continue; 140 } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK) { 141 continue; 142 } else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { 143 String value = cell.getStringCellValue(); 144 if (value == null || value.equals("")) { 145 continue; 146 } else { 147 value = value.trim(); 148 if (value.isEmpty() || value.equals("") 149 || value.length() == 0) { 150 continue; 151 } 152 } 153 } 154 rowCount = rowNum + 1; 155 return rowCount; 156 } 157 } 158 159 rowCount = rowNum; 160 return rowCount; 161 } 162 163 /** 164 * 读取第sheetNo个sheet页中第rowNo行第cellNo列的数据(通过) 165 * 166 * @param sheetNo 167 * sheet页编号 168 * @param rowNo 169 * 行号 170 * @param cellNo 171 * 列号 172 * @return 返回相应的excel单元格内容 173 * @throws Exception 174 */ 175 public String readExcelByRowAndCell(int sheetNo, int rowNo, int cellNo) 176 throws Exception { 177 String rowCellData = ""; 178 XSSFSheet sheet = wb.getSheetAt(sheetNo - 1); 179 String sheetName = wb.getSheetName(sheetNo - 1); 180 try { 181 XSSFRow row = sheet.getRow(rowNo - 1); 182 if (row == null) { 183 return "NoData"; 184 } 185 XSSFCell cell = row.getCell((short) (cellNo - 1)); 186 if (cell == null) { 187 return "NoData"; 188 } 189 int cellType = cell.getCellType(); 190 String df = cell.getCellStyle().getDataFormatString(); 191 if (cellType == XSSFCell.CELL_TYPE_NUMERIC) {// 数值(包括excel中数值、货币、日期、时间、会计专用等单元格格式) 192 double d = cell.getNumericCellValue(); 193 // 判断数值是否是日期,该方法只能识别部分日期格式,故加入第二个判断条件对不能识别的日期再次进行识别 194 if (DateUtil.isCellDateFormatted(cell) 195 || df.contains("yyyy\"年\"m\"月\"d\"日\"")) {// 日期、时间单元格格式 196 Date date = DateUtil.getJavaDate(d); 197 Timestamp timestamp = new Timestamp(date.getTime()); 198 String temp = timestamp.toString(); 199 if (temp.endsWith("00:00:00.0")) {// yyyy-MM-dd 格式 200 rowCellData = temp.substring(0, 201 temp.lastIndexOf("00:00:00.0")); 202 } else if (temp.endsWith(".0")) {// yyyy-MM-dd hh:mm:ss 格式 203 rowCellData = temp.substring(0, temp.lastIndexOf(".0")); 204 } else { 205 rowCellData = timestamp.toString(); 206 } 207 } else {// 数值、货币、会计专用、百分比、分数、科学记数 单元格式 208 rowCellData = new DecimalFormat("0.########").format(d); 209 } 210 } else if (cellType == XSSFCell.CELL_TYPE_STRING) {// 文本 211 rowCellData = cell.getStringCellValue(); 212 } else if (cellType == XSSFCell.CELL_TYPE_FORMULA) {// 公式 213 double d = cell.getNumericCellValue(); 214 rowCellData = String.valueOf(d); 215 } else if (cellType == XSSFCell.CELL_TYPE_BLANK) {// 空 216 rowCellData = ""; 217 } else if (cellType == XSSFCell.CELL_TYPE_BOOLEAN) {// 布尔值 218 rowCellData = ""; 219 } else if (cellType == XSSFCell.CELL_TYPE_ERROR) {// 异常 220 rowCellData = ""; 221 } else { 222 throw new Exception(sheetName + " sheet页中" + "第" + rowNo + "行" 223 + "第" + cellNo + "列,单元格格式无法识别,请检查sheet页"); 224 } 225 } catch (Exception e) { 226 e.printStackTrace(); 227 throw new Exception(sheetName + "sheet页中" + "第" + rowNo + "行" + "第" 228 + cellNo + "列" + "数据不符合要求,请检查sheet页"); 229 } 230 return rowCellData; 231 } 232 233 /** 234 * 读取第sheetNo个sheet页中第rowNo行的数据 235 * 236 * @param sheetNo 237 * 指定sheetNo页 238 * @param rowNo 239 * 指定rowNo行 240 * @return 241 * @throws Exception 242 */ 243 public String[] readExcelByRow(int sheetNo, int rowNo) throws Exception { 244 String[] rowData = null; 245 XSSFSheet sheet = wb.getSheetAt(sheetNo - 1); 246 XSSFRow row = sheet.getRow(rowNo - 1); 247 int cellCount = row.getLastCellNum(); 248 rowData = new String[cellCount]; 249 for (int k = 1; k <= cellCount; k++) { 250 rowData[k - 1] = readExcelByRowAndCell(sheetNo, rowNo, k); 251 } 252 return rowData; 253 } 254 255 /** 256 * 读取第sheetNo个sheet页中第cellNo列的数据 257 * 258 * @param sheetNo 259 * 指定sheetNo页 260 * @param cellNo 261 * 指定cellNo列号 262 * @return 263 * @throws Exception 264 */ 265 public String[] readExcelByCell(int sheetNo, int cellNo) throws Exception { 266 String[] cellData = null; 267 XSSFSheet sheet = wb.getSheetAt(sheetNo - 1); 268 int rowCount = sheet.getLastRowNum(); 269 cellData = new String[rowCount + 1]; 270 for (int i = 0; i <= rowCount; i++) { 271 cellData[i] = readExcelByRowAndCell(sheetNo - 1, i, cellNo - 1); 272 } 273 return cellData; 274 } 275 276 /** 277 * 关闭excel工作区,释放资源 278 * 279 * @throws Exception 280 */ 281 @Override 282 public void close() { 283 if (wb != null) { 284 try { 285 wb.close(); 286 } catch (IOException e) { 287 e.printStackTrace(); 288 } finally { 289 wb = null; 290 } 291 } 292 } 293 }
1 /* 2 * ExcelParse.java 3 * 4 * 2016-1-6 下午4:45:53 5 * 6 * RecluseKapoor 7 * 8 * Copyright © 2016, RecluseKapoor. All rights reserved. 9 * 10 */ 11 package com.rk.pub.poi; 12 13 import com.rk.pub.poi.excel.ExcelParse2003; 14 import com.rk.pub.poi.excel.ExcelParse2007; 15 import com.rk.pub.poi.excel.IExcelParse; 16 17 /** 18 * @Title:recluse-Excel文件解析工具类(兼容2003和2007版本Excel) 19 * 20 * @Description: 该工具类用于解析Excel文件,同时兼容2003版和2007版Excel文件的解析,且随时可以进行新版本的扩展, 21 * <p> 22 * 若要支持新版本Excel格式的解析,只需要在excle包下新增一个实现了IExcelParse接口的实现类, 23 * <p> 24 * 在新增的实现类中实现新对版本Excel格式的解析的功能代码即可 ; 该扩展方法可以最大程度的实现解耦 。 25 * <p> 26 * 27 * @Company: 卡普工作室 28 * 29 * @Website: http://www.cnblogs.com/reclusekapoor/ 30 * 31 * @author: RecluseKapoor 32 * 33 * @CreateDate:2016-1-6 下午9:43:56 34 * 35 * @version: 1.0 36 * 37 * @lastModify: 38 * 39 */ 40 public class ExcelParse { 41 42 private IExcelParse excelParse = null; 43 44 /** 45 * 加载实例,根据不同版本的Excel文件,加载不同的具体实现实例 46 * 47 * @param path 48 * @return 49 */ 50 private boolean getInstance(String path) throws Exception { 51 path = path.toLowerCase(); 52 if (path.endsWith(".xls")) { 53 excelParse = new ExcelParse2003(); 54 } else if (path.endsWith(".xlsx")) { 55 excelParse = new ExcelParse2007(); 56 } else { 57 throw new Exception("对不起,目前系统不支持对该版本Excel文件的解析。"); 58 } 59 return true; 60 } 61 62 /** 63 * 获取excel工作区 64 * 65 * @param path 66 * @throws Exception 67 */ 68 public void loadExcel(String filePathAndName) throws Exception { 69 getInstance(filePathAndName); 70 excelParse.loadExcel(filePathAndName); 71 } 72 73 /** 74 * 获取sheet页名称 75 * 76 * @param sheetNo 77 * @return 78 */ 79 public String getSheetName(int sheetNo) { 80 return excelParse.getSheetName(sheetNo); 81 } 82 83 /** 84 * 获取sheet页数 85 * 86 * @return 87 * @throws Exception 88 */ 89 public int getSheetCount() throws Exception { 90 return excelParse.getSheetCount(); 91 } 92 93 /** 94 * 获取sheetNo页行数 95 * 96 * @param sheetNo 97 * @return 98 * @throws Exception 99 */ 100 public int getRowCount(int sheetNo) { 101 return excelParse.getRowCount(sheetNo); 102 } 103 104 /** 105 * 获取sheetNo页行数(含有操作或者内容的真实行数) 106 * 107 * @param sheetNo 108 * @return 109 * @throws Exception 110 */ 111 public int getRealRowCount(int sheetNo) { 112 return excelParse.getRealRowCount(sheetNo); 113 } 114 115 /** 116 * 读取第sheetNo个sheet页中第rowNo行第cellNo列的数据 117 * 118 * @param sheetNo 119 * sheet页编号 120 * @param rowNo 121 * 行号 122 * @param cellNo 123 * 列号 124 * @return 返回相应的excel单元格内容 125 * @throws Exception 126 */ 127 public String readExcelByRowAndCell(int sheetNo, int rowNo, int cellNo) 128 throws Exception { 129 return excelParse.readExcelByRowAndCell(sheetNo, rowNo, cellNo); 130 } 131 132 /** 133 * 读取指定SHEET页指定行的Excel内容 134 * 135 * @param sheetNo 136 * 指定SHEET页 137 * @param lineNo 138 * 指定行 139 * @return 140 * @throws Exception 141 */ 142 public String[] readExcelByRow(int sheetNo, int rowNo) throws Exception { 143 return excelParse.readExcelByRow(sheetNo, rowNo); 144 } 145 146 /** 147 * 读取指定SHEET页指定列中的数据 148 * 149 * @param sheetNo 150 * 指定SHEET页 151 * @param cellNo 152 * 指定列号 153 * @return 154 * @throws Exception 155 */ 156 public String[] readExcelByCell(int sheetNo, int cellNo) throws Exception { 157 return excelParse.readExcelByCell(sheetNo, cellNo); 158 } 159 160 /** 161 * 关闭excel工作区,释放资源 162 * 163 */ 164 public void close() { 165 excelParse.close(); 166 } 167 168 /** 169 * 测试方法 170 * 171 * @param args 172 */ 173 public static void main(String[] args) { 174 ExcelParse parse = new ExcelParse(); 175 try { 176 // 加载excel文件 177 parse.loadExcel("E:\\2007.xls"); 178 // 统计sheet页数 179 System.out.println(parse.getSheetCount()); 180 // 读取单元格信息 181 System.out.println(parse.readExcelByRowAndCell(1, 1, 1)); 182 } catch (Exception e) { 183 e.printStackTrace(); 184 } finally { 185 // 释放资源 186 parse.close(); 187 } 188 } 189 190 }
时间: 2024-10-21 21:40:29