java excel 读取工具_excel读取 工具类

1 packagecn.yongche.utils;2

3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.IOException;6 importjava.io.InputStream;7 importjava.util.ArrayList;8 importjava.util.List;9 importorg.apache.poi.hssf.usermodel.HSSFCell;10 importorg.apache.poi.hssf.usermodel.HSSFWorkbook;11 importorg.apache.poi.ss.usermodel.Cell;12 importorg.apache.poi.ss.usermodel.Row;13 importorg.apache.poi.ss.usermodel.Sheet;14 importorg.apache.poi.ss.usermodel.Workbook;15 importorg.apache.poi.xssf.usermodel.XSSFWorkbook;16

17 /**

18 * excel读取 工具类19 *20 * @jar包21 * 该类使用到了以下jar:22 * 1、poi-ooxml-3.9.jar23 * 2、poi-3.9.jar24 */

25 public classImportExecl {26

27 /**

28 * main测试29 */

30 public static void main(String[] args) throwsException {31 ImportExecl poi = newImportExecl();32 List> list = poi.read("E:/批量导入客户模板.xlsx");33 if (list != null) {34 for (int i = 0; i < list.size(); i++) {35 List cellList =list.get(i);36 for (int j = 0; j < cellList.size(); j++) {37 System.out.print(" " +cellList.get(j));38 }39 System.out.println();40 }41 }42 }43

44 //总行数

45 private int totalRows = 0;46

47 //总列数

48 private int totalCells = 0;49

50 //错误信息

51 privateString errorInfo;52

53 //构造方法

54 publicImportExecl() {55 }56

57 /**

58 * 得到总行数59 */

60 public intgetTotalRows() {61 returntotalRows;62 }63

64 /**

65 * 得到总列数66 */

67 public intgetTotalCells() {68 returntotalCells;69 }70

71 /**

72 * 得到错误信息73 */

74 publicString getErrorInfo() {75 returnerrorInfo;76 }77

78 /**

79 * 验证excel文件80 */

81 public booleanvalidateExcel(String filePath) {82 /**检查文件名是否为空或者是否是Excel格式的文件*/

83 if (filePath == null || !(CheckExcelUtil.isExcel2003(filePath) ||CheckExcelUtil.isExcel2007(filePath))) {84 errorInfo = "文件名不是excel格式";85 return false;86 }87

88 /**检查文件是否存在*/

89 File file = newFile(filePath);90 if (file == null || !file.exists()) {91 errorInfo = "文件不存在";92 return false;93 }94 return true;95 }96

97 /**

98 * 根据文件路径读取excel文件99 */

100 public List> read(String filePath) throwsIOException {101 List> dataLst = new ArrayList>();102 InputStream is = null;103 try{104 /**验证文件是否合法*/

105 if (!validateExcel(filePath)) {106 System.out.println(errorInfo);107 return null;108 }109

110 /**判断文件的类型,是2003还是2007*/

111 boolean isExcel2003 = true;112 if(CheckExcelUtil.isExcel2007(filePath)) {113 isExcel2003 = false;114 }115

116 /**调用本类提供的根据流读取的方法*/

117 File file = newFile(filePath);118 is = newFileInputStream(file);119 dataLst =read(is, isExcel2003);120 is.close();121 is = null;122 } catch(Exception ex) {123 ex.printStackTrace();124 } finally{125 if (is != null) {126 try{127 is.close();128 } catch(IOException e) {129 is = null;130 e.printStackTrace();131 }132 }133 }134 returndataLst;135 }136

137 /**

138 * 根据流读取Excel文件139 *140 *@paraminputStream 文件输入流141 *@paramisExcel2003 标识是否2003的excel。142 * true:是2003的excel,false:是2007的excel143 *@return

144 *145 * @扩展说明146 * 如果使用springmvc的MultipartFile接收前端上传的excel文件的话,可以使用MultipartFile的对象,获取上传的文件名称,147 * 然后,可以通过 CheckExcelUtil 类的方法,接收文件名称参数,来判断excel所属的版本。最后再调用此方法来读取excel数据。148 *149 */

150 public List> read(InputStream inputStream, booleanisExcel2003) {151 List> dataLst = null;152 try{153 /**根据版本选择创建Workbook的方式*/

154 Workbook wb = null;155 if(isExcel2003) {156 wb = newHSSFWorkbook(inputStream);157 } else{158 wb = newXSSFWorkbook(inputStream);159 }160 dataLst =read(wb);161 } catch(IOException e) {162 e.printStackTrace();163 }164 returndataLst;165 }166

167 /**

168 * 读取数据169 */

170 private List>read(Workbook wb) {171 List> dataLst = new ArrayList>();172 //得到第一个shell

173 Sheet sheet = wb.getSheetAt(0);174 //得到Excel的行数

175 this.totalRows =sheet.getPhysicalNumberOfRows();176 //得到Excel的列数

177 if (this.totalRows >= 1 && sheet.getRow(0) != null) {178 this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();179 }180

181 //循环Excel的行

182 for (int r = 0; r < this.totalRows; r++) {183 Row row =sheet.getRow(r);184 if (row == null) {185 continue;186 }187 List rowLst = new ArrayList();188 //循环Excel的列

189 for (int c = 0; c < this.getTotalCells(); c++) {190 Cell cell =row.getCell(c);191 String cellValue = "";192 if (null !=cell) {193 //以下是判断数据的类型

194 switch(cell.getCellType()) {195 case HSSFCell.CELL_TYPE_NUMERIC: //数字

196 cellValue = cell.getNumericCellValue() + "";197 break;198

199 case HSSFCell.CELL_TYPE_STRING: //字符串

200 cellValue =cell.getStringCellValue();201 break;202

203 case HSSFCell.CELL_TYPE_BOOLEAN: //Boolean

204 cellValue = cell.getBooleanCellValue() + "";205 break;206

207 case HSSFCell.CELL_TYPE_FORMULA: //公式

208 cellValue = cell.getCellFormula() + "";209 break;210

211 case HSSFCell.CELL_TYPE_BLANK: //空值

212 cellValue = "";213 break;214

215 case HSSFCell.CELL_TYPE_ERROR: //故障

216 cellValue = "非法字符";217 break;218

219 default:220 cellValue = "未知类型";221 break;222 }223 }224 rowLst.add(cellValue);225 }226

227 //保存第r行的第c列

228 dataLst.add(rowLst);229 }230 returndataLst;231 }232

233 }234

235 classCheckExcelUtil {236 /**

237 * 检查是否是2003的excel,若是,则返回true238 */

239 public static booleanisExcel2003(String filePath) {240 return filePath.matches("^.+\\.(?i)(xls)$");241 }242

243 /**

244 * 检查是否是2007的excel,若是,则返回true245 */

246 public static booleanisExcel2007(String filePath) {247 return filePath.matches("^.+\\.(?i)(xlsx)$");248 }249 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值