问题描述

在我们的项目中经常会遇到需要导入一定规格的excel文档,然后来解析里面的内容转换为数据导入到数据库或者进行其他操作。这样解析的问题就来了,解析excle从文件后缀名上分为.xls和.xlsx两种,从数据的格式上分为行数据和列数据。

解决方案

直接上代码,解析行数据基础类

package com.test.excel;import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;/*** @author chinwer* @created 2014-5-20*/
public abstract class ExcelTransfer<T extends BaseEntity> {public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";public static final String NOT_EXCEL_FILE = " : Not the Excel file!";public static final String PROCESSING = "Processing...";public static final String EMPTY = "";public static final String POINT = ".";protected abstract T getModel();private List<String> parseErrorList = new ArrayList<String>();public String getParseErrorInfo() {if (!parseErrorList.isEmpty()) {return ArrayUtils.toString(parseErrorList, "\n");}return "";}/*** @return the parseErrorList*/public List<String> getParseErrorList() {return parseErrorList;}/*** 读取xls或者xlsx文件* @param is excel文件输入流* @param startRow 开始行 传null值默认取1* @param endRow 结束行  传null值默认取现有数据行* @param startColumn 开始列* @param endColumn 结束列* @return 每行数据以list返回,每个元素对应一列* @throws Exception */public List<T> readExcel(String fileName, InputStream is, Integer startRow, Integer endRow, Integer startColumn,Integer endColumn) throws Exception {if (is == null) {throw new Exception("is参数为必传");}if (startColumn == null || endColumn == null) {throw new Exception("startColumn和endColumn参数为必传");} else {String postfix = getPostfix(fileName);if (StringUtils.isNotBlank(postfix)) {if (OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {return readXls(is, startRow, endRow, startColumn, endColumn);} else if (OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {return readXlsx(is, startRow, endRow, startColumn, endColumn);}} else {System.out.println(fileName + NOT_EXCEL_FILE);}}return null;}/*** Read the Excel 2010* @param is of the excel file* @return* @throws IOException*/public List<T> readXlsx(InputStream is, Integer startRow, Integer endRow, Integer startColumn, Integer endColumn)throws IOException {XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);List<T> retlist = new ArrayList<T>();for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);if (xssfSheet == null) {continue;}if (startRow == null)startRow = 1;if (endRow == null)endRow = xssfSheet.getLastRowNum() + 1;for (int rowNum = startRow - 1; rowNum < endRow; rowNum++) {XSSFRow xssfRow = xssfSheet.getRow(rowNum);if (xssfRow != null) {List<String> errorList = new ArrayList<String>();T model = getModel();for (int colNum = startColumn; colNum <= endColumn; colNum++) {try {setCellValue(model, xssfRow, colNum);} catch (Exception e) {errorList.add(String.valueOf(colNum));}}if (!errorList.isEmpty()) {parseErrorList.add("第" + String.valueOf(xssfRow.getRowNum() + 1) + "行的第"+ ArrayUtils.toString(errorList, ",") + "列存在格式错误!");} else if (retlist.contains(model)) {parseErrorList.add("第" + String.valueOf(xssfRow.getRowNum() + 1) + "行的数据重复!");} else {model.setRowNum(String.valueOf(xssfRow.getRowNum() + 1));retlist.add(model);}}}break;}return retlist;}/*** Read the Excel 2003-2007* @param is of the Excel* @return* @throws IOException*/public List<T> readXls(InputStream is, Integer startRow, Integer endRow, Integer startColumn, Integer endColumn)throws IOException {HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);List<T> retlist = new ArrayList<T>();for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {System.out.println(numSheet);HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);if (hssfSheet == null) {continue;}if (startRow == null)startRow = 1;if (endRow == null)endRow = hssfSheet.getLastRowNum() + 1;for (int rowNum = startRow - 1; rowNum < endRow; rowNum++) {HSSFRow hssfRow = hssfSheet.getRow(rowNum);if (hssfRow != null) {List<String> errorList = new ArrayList<String>();T model = getModel();for (int colNum = startColumn; colNum <= endColumn; colNum++) {try {setCellValue(model, hssfRow, colNum);} catch (Exception e) {errorList.add(String.valueOf(colNum));}}if (!errorList.isEmpty()) {parseErrorList.add("第" + String.valueOf(hssfRow.getRowNum() + 1) + "行的第"+ ArrayUtils.toString(errorList, ",") + "列存在格式错误!");} else if (retlist.contains(model)) {parseErrorList.add("第" + String.valueOf(hssfRow.getRowNum() + 1) + "行的数据重复!");} else {model.setRowNum(String.valueOf(hssfRow.getRowNum() + 1));retlist.add(model);}}}break;}return retlist;}@SuppressWarnings("static-access")protected String getValue(Cell cell, DecimalFormat df) {if (cell == null) {return EMPTY;}if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {return String.valueOf(cell.getBooleanCellValue());} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {return df.format(cell.getNumericCellValue());} else {return String.valueOf(cell.getStringCellValue());}}private static String getPostfix(String path) {if (path == null || StringUtils.isEmpty(path.trim())) {return EMPTY;}if (path.contains(POINT)) {return path.substring(path.lastIndexOf(POINT) + 1, path.length());}return EMPTY;}/*** 设置每个单元格对应模型的属性<br>* (主要注意一下对存数字的单元格和即可能存字符又可能存数字的单元格的格式化)* @param model* @param row* @param colNum*/protected abstract void setCellValue(T model, Row row, int colNum) throws Exception;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210

解析行数据测试类,demo.java

package com.test.excel;public class Demo extends BaseEntity {private String id;private String orgName;private String orgCode;private String contactName;private String phone;private String email;private String productName;private String productCode;private String date;private String price;private String pice;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getOrgName() {return orgName;}public void setOrgName(String orgName) {this.orgName = orgName;}public String getOrgCode() {return orgCode;}public void setOrgCode(String orgCode) {this.orgCode = orgCode;}public String getContactName() {return contactName;}public void setContactName(String contactName) {this.contactName = contactName;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getProductName() {return productName;}public void setProductName(String productName) {this.productName = productName;}public String getProductCode() {return productCode;}public void setProductCode(String productCode) {this.productCode = productCode;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}public String getPice() {return pice;}public void setPice(String pice) {this.pice = pice;}@Overridepublic String toString() {return "Demo [id=" + id + ", orgName=" + orgName + ", orgCode=" + orgCode + ", contactName=" + contactName+ ", phone=" + phone + ", email=" + email + ", productName=" + productName + ", productCode="+ productCode + ", date=" + date + ", price=" + price + ", pice=" + pice + "]";}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113

BaseEntity .java

package com.test.excel;import java.io.Serializable;public class BaseEntity implements Serializable {/*** 域的描述:* 合法值范围:* 空值说明:*/private static final long serialVersionUID = 2047950479396700672L;private String rowNum;public String getRowNum() {return rowNum;}public void setRowNum(String rowNum) {this.rowNum = rowNum;}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

ProductTypeDictEnum.java

package com.test.excel;import java.util.HashMap;
import java.util.Map;import org.apache.commons.lang.StringUtils;/*********************************************************************************
//* Copyright (C) 2015 Pingan Haoche (PAHAOCHE). All Rights Reserved.
//*
//* Filename:      ProductDictEnum.java
//* Revision:      1.0
//* Author:        <gao yunqi>
//* Created On:    2016年2月18日
//* Modified by:
//* Modified On:
//*
//* Description:   <产品类型相关字典枚举类>
/********************************************************************************/
public enum ProductTypeDictEnum {IMSP_PRODUCT_TYPE_10("10", "见证业务"), IMSP_PRODUCT_TYPE_3("3", "净值型"), IMSP_PRODUCT_TYPE_4("4", "预期收益率型"), IMSP_PRODUCT_TYPE_5("5", "资产挂牌转让"), IMSP_PRODUCT_TYPE_6("6", "非标债权型"), IMSP_PRODUCT_TYPE_7("7", "智能宝"), IMSP_PRODUCT_TYPE_8("8", "ABS"), IMSP_PRODUCT_TYPE_9("9", "F2F"),;private String code;//状态codeprivate String des;//状态描述private static Map<String, String> desShowMap = new HashMap<String, String>();static {ProductTypeDictEnum[] rs = ProductTypeDictEnum.values();for (ProductTypeDictEnum productEnum : rs) {desShowMap.put(productEnum.getDes(), productEnum.getCode());}}/*** @param code* @param des*/private ProductTypeDictEnum(String code, String des) {this.code = code;this.des = des;}/*** @return the code*/public String getCode() {return code;}/*** @param code the code to set*/public void setCode(String code) {this.code = code;}/*** @return the des*/public String getDes() {return des;}/*** @param des the des to set*/public void setDes(String des) {this.des = des;}/*** * @param getCodeByDes* @return*/public static String getCodeByDes(String des) {if (StringUtils.isNotBlank(des)) {return ProductTypeDictEnum.desShowMap.get(des);} else {return null;}}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

demo转换测试类

package com.test.excel;import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.util.List;import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;public class DemoExcelTransfer extends ExcelTransfer<Demo> {@Overrideprotected Demo getModel() {return new Demo();}@Overridepublic void setCellValue(Demo demo, Row row, int colNum) {Cell cell = row.getCell(colNum - 1);DecimalFormat df = new DecimalFormat("#");switch (colNum) {case 1:demo.setId(getValue(cell, df));break;case 2:demo.setOrgName(getValue(cell, df));break;case 3:demo.setOrgCode(getValue(cell, df));break;case 4:demo.setContactName(getValue(cell, df));break;case 5:demo.setPhone(getValue(cell, df));break;case 6:demo.setEmail(getValue(cell, df));break;case 7:demo.setProductName(getValue(cell, df));break;case 8:demo.setProductCode(getValue(cell, df));break;case 9:demo.setDate(getValue(cell, df));break;case 10:DecimalFormat df1 = new DecimalFormat("####.00");demo.setPrice(getValue(cell, df1));break;case 11:demo.setPice(getValue(cell, df));break;}}public static void main(String[] args) throws Exception {String excel2010 = "D:\\testDemo.xlsx";FileInputStream inputStream = new FileInputStream(new File(excel2010));// read the 2003-2007 excelList<Demo> list = new DemoExcelTransfer().readExcel(excel2010, inputStream, 2, 3, 1, 11);for (Demo demo : list) {System.out.println(demo.toString());}}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

excel原始数据截图

解析结果输出如下

读取列数据的工具类如下,操作参考读取行数据的操作

package com.test.excel;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;public abstract class ExcelColumnTransfer<T extends BaseEntity> {public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";public static final String NOT_EXCEL_FILE = " : Not the Excel file!";public static final String PROCESSING = "Processing...";public static final String EMPTY = "";public static final String POINT = ".";protected abstract T getModel();private List<String> parseErrorList = new ArrayList<String>();private String parseMessage; /* 解析信息。总共解析条数,成功和失败条数 */public String getParseErrorInfo() {if (!parseErrorList.isEmpty()) {return ArrayUtils.toString(parseErrorList, "\n");}return "";}/*** @return the parseErrorList*/public List<String> getParseErrorList() {return parseErrorList;}/*** 读取xls或者xlsx文件* @param path 文件路径,包括文件名* @param startRow 开始行 传null值默认取1* @param endRow 结束行  传null值默认取现有数据行* @param startColumn 开始列* @param endColumn 结束列* @return 每行数据以list返回,每个元素对应一列* @throws Exception */public List<T> readExcel(String path, Integer startRow, Integer endRow, Integer startColumn, Integer endColumn)throws Exception {if (path == null || StringUtils.isBlank(path)) {throw new Exception("path参数为必传");}if (startColumn == null) {throw new Exception("startColumn和endColumn参数为必传");} else {String postfix = getPostfix(path);if (StringUtils.isNotBlank(postfix)) {if (OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {return readXls(path, startRow, endRow, startColumn, endColumn);} else if (OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {return readXlsx(path, startRow, endRow, startColumn, endColumn);}} else {System.out.println(path + NOT_EXCEL_FILE);}}return null;}/*** Read the Excel 2010* @param path the path of the excel file* @return* @throws IOException*/public List<T> readXlsx(String path, Integer startRow, Integer endRow, Integer startColumn, Integer endColumn)throws IOException {int totalRecord = 0;int successRecord = 0;int failureRecord = 0;System.out.println(PROCESSING + path);InputStream is = new FileInputStream(path);XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);List<T> retlist = new ArrayList<T>();DecimalFormat df = new DecimalFormat("#");for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);List<String> oneSheetErrorList = new ArrayList<String>();if (xssfSheet == null) {continue;}if (startRow == null)startRow = 1;endRow = xssfSheet.getLastRowNum() + 1;int maxColumn = 0;for (int i = 0; i < endRow.intValue(); i++) {if (null != xssfSheet.getRow(i)) {int rowColumn = xssfSheet.getRow(i).getLastCellNum();if (maxColumn < rowColumn) {Cell cell = xssfSheet.getRow(i).getCell(rowColumn - 1);String param = getValue(cell, df);if (null != param && !"".equals(param)) {maxColumn = rowColumn;}}}}for (int coloumNum = startColumn; coloumNum <= maxColumn; coloumNum++) {T model = getModel();String productTypeName = xssfSheet.getSheetName();String productType = ProductTypeDictEnum.getCodeByDes(productTypeName);List<String> errorList = new ArrayList<String>();for (int rowNum = startRow - 1; rowNum < endRow; rowNum++) {totalRecord += 1;XSSFRow xssfRows = xssfSheet.getRow(rowNum);try {setCellValue(model, xssfRows, coloumNum, numSheet + 1, productType);successRecord += 1;} catch (Exception e) {e.printStackTrace();errorList.add(String.valueOf(xssfRows.getRowNum() + 1));failureRecord += 1;}}if (!errorList.isEmpty()) {oneSheetErrorList.add("第" + String.valueOf(coloumNum) + "列的第" + ArrayUtils.toString(errorList, ",")+ "行存在格式错误!");} else {model.setRowNum(String.valueOf(coloumNum));retlist.add(model);}}if (!oneSheetErrorList.isEmpty()) {parseErrorList.add("第" + (numSheet + 1) + "个sheet页存在错误:" + oneSheetErrorList.toString());oneSheetErrorList.clear();}parseMessage = "总共导入" + totalRecord + "条数据,成功" + successRecord + ",失败" + failureRecord;}return retlist;}/*** Read the Excel 2003-2007* @param path the path of the Excel* @return* @throws IOException*/public List<T> readXls(String path, Integer startRow, Integer endRow, Integer startColumn, Integer endColumn)throws IOException {System.out.println(PROCESSING + path);InputStream is = new FileInputStream(path);HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);List<T> retlist = new ArrayList<T>();List<String> oneSheetErrorList = new ArrayList<String>();DecimalFormat df = new DecimalFormat("#");for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);if (hssfSheet == null) {continue;}if (startRow == null)startRow = 1;if (endRow == null)endRow = hssfSheet.getLastRowNum() + 1;int maxColumn = 0;for (int i = 0; i < endRow.intValue(); i++) {if (null != hssfSheet.getRow(i)) {int rowColumn = hssfSheet.getRow(i).getLastCellNum();if (maxColumn < rowColumn) {Cell cell = hssfSheet.getRow(i).getCell(rowColumn - 1);String param = getValue(cell, df);if (null != param && !"".equals(param)) {maxColumn = rowColumn;}}}}for (int coloumNum = startColumn; coloumNum <= maxColumn; coloumNum++) {T model = getModel();List<String> errorList = new ArrayList<String>();String productTypeName = hssfSheet.getSheetName();String productType = ProductTypeDictEnum.getCodeByDes(productTypeName);for (int rowNum = startRow - 1; rowNum < endRow; rowNum++) {HSSFRow hssfRows = hssfSheet.getRow(rowNum);try {setCellValue(model, hssfRows, coloumNum, numSheet + 1, productType);} catch (Exception e) {errorList.add(String.valueOf(hssfRows.getRowNum()));}}if (!errorList.isEmpty()) {oneSheetErrorList.add("第" + String.valueOf(coloumNum) + "列的第" + ArrayUtils.toString(errorList, ",")+ "行存在格式错误!");} else {model.setRowNum(String.valueOf(coloumNum));retlist.add(model);}retlist.add(model);}if (!oneSheetErrorList.isEmpty()) {parseErrorList.add("第" + (numSheet + 1) + "个sheet页存在错误:" + oneSheetErrorList.toString());oneSheetErrorList.clear();}}return retlist;}@SuppressWarnings("static-access")protected String getValue(Cell cell, DecimalFormat df) {if (cell == null) {return EMPTY;}if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {return String.valueOf(cell.getBooleanCellValue());} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {return df.format(cell.getNumericCellValue());} else {return String.valueOf(cell.getStringCellValue());}}private static String getPostfix(String path) {if (path == null || StringUtils.isEmpty(path.trim())) {return EMPTY;}if (path.contains(POINT)) {return path.substring(path.lastIndexOf(POINT) + 1, path.length());}return EMPTY;}/*** 设置每个单元格对应模型的属性<br>* (主要注意一下对存数字的单元格和即可能存字符又可能存数字的单元格的格式化)* @param model* @param row* @param colNum*/protected abstract void setCellValue(T model, Row row, int colNum, int type, String productType) throws Exception;public String getParseMessage() {return parseMessage;}public void setParseMessage(String parseMessage) {this.parseMessage = parseMessage;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263

注:


以上代码所需jar包地址如下:
http://download.csdn.net/download/gao36951/10032742

            <link rel="stylesheet" href="http://csdnimg.cn/release/phoenix/production/markdown_views-d4dade9c33.css"></div>

Java解析excel操作相关推荐

  1. java解析Excel文件

    下文介绍java解析Excel文件的方案 前置准备 1.第三方jar包或者Maven配置 org.apache.poi的jar包 Maven配置如下 <groupId>org.apache ...

  2. java解析Excel文件的方法

    java解析Excel文件的方法 介绍 1.1 pom依赖 1.2 将数据流转化为可解析的Workbook类型文件 1.3 解析 1.4 Controller层接收前端传递的Excel文件(前端使用E ...

  3. Java 解析Excel(xls、xlsx两种格式)

    Java 解析Excel(xls.xlsx两种格式) 一.环境 JDK 1.8 二.JAR 1.commons-collections4-4.1.jar 2.poi-3.9-20121203.jar ...

  4. java解析excel的方法_Java解析Excel内容的方法

    本文实例讲述了Java解析Excel内容的方法.分享给大家供大家参考.具体实现方法如下: import java.io.File; import java.io.FileInputStream; im ...

  5. java解析excel的工具_Java 解析 Excel 工具 easyexcel

    软件介绍 easyexcel -- JAVA 解析 Excel 工具 Java 解析.生成 Excel 比较有名的框架有 Apache poi.jxl .但他们都存在一个严重的问题就是非常的耗内存,p ...

  6. 使用EasyExcel导入导出Excel报表-JAVA解析Excel工具

    一.EasyExcel概述 Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内 ...

  7. Java解析excel表格中的图片的方式

    我们要用java解析首先得在项目中引入解析excel的相关包,我们这里使用的是apache的poi-3.12.jar来做开发. 首先获取excel文件,获取文件的方式这里就不细说了,获取到文件后,将文 ...

  8. java解析excel工具EasyExcel使用详情

    EasyExcel 1 EasyExcel的集成 1.1 引入依赖 1.2 模型映射 1.3 读Excel 1.4 写Excel 1.5 web上传.下载 2 自定义多Sheet页下载 2.1 工具类 ...

  9. easyexcel生成excel_阿里JAVA解析Excel工具easyexcel

    java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有 ...

最新文章

  1. 使用fis优化web站点
  2. [机器学习] Coursera ML笔记 - 神经网络(Representation)
  3. NOIP模拟测试16「Drink·blue·weed」
  4. React-Native开发App,修改图标和名字
  5. 电子技术基础数字部分第六版_知识速递 | 数字电子技术基础知识要点
  6. WinRunner:强大的企业级自动化测试工具
  7. 字符串(Linux应用编程篇)
  8. 上传git编译失败回退
  9. python爬取文献资料_Python 批量爬取Web of Science 文献信息数据
  10. ubuntu下好用的录屏软件之Vokoscreen
  11. 自制乐高同款机器人瓦力—Wall-E
  12. linux系统使用实验报告操作系统,linux操作系统实验报告1.doc
  13. shell判断所输整数是否为质数
  14. 使用java完成一个猜数字的小游戏(数据范围在1-100之间)
  15. geotools 的书籍
  16. 虚幻引擎中的反射(译)
  17. redis核心数据结构以及他的应用场景
  18. 如何使用matlab实现分段函数
  19. [ 题解 ] A. The Bucket List (待更名)
  20. 60分钟快速掌握RabbitMQ,java面试技巧和注意事项

热门文章

  1. 拥抱变革——RSNA2017参会记录
  2. ycf 梗_你从哪些情况下觉得云次方是真的?
  3. 用ftp的网址无法在谷歌浏览器打开:用ie浏览器打开
  4. 二叉树前序遍历,后序遍历
  5. PR曲线原理及通过曲线判断分类器优劣
  6. 此网站的安全证书有问题,没有继续浏览选项
  7. matlab入门教程四 ----- 绘制平面图形
  8. (简单)华为畅玩6A DLI-AL10的USB调试模式在哪里打开的经验
  9. AI 使用 GPU运行和FPGA运行,哪个速度快,哪个便宜
  10. android手机上图像分类技术的研究,用图片分类技术实现“拍照识花”的原理详解...