来源:http://blog.csdn.net/allen202/article/details/54145479
今天有个朋友问我,有没有excel表格到处json的方法,在网上找到了好几个工具,都不太理想,于是根据自己的需求,自己写了一个工具。
功能代码
package org.duang.test;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
/**
* excel表格转成json
* @ClassName: Excel2JSONHelper
* @Description:TODO(这里用一句话描述这个类的作用)
* @author LiYonghui
* @date 2017年1月6日 下午4:42:43
*/
public class Excel2JSONHelper {
//常亮,用作第一种模板类型,如下图
private static final int HEADER_VALUE_TYPE_Z=1;
//第二种模板类型,如下图
private static final int HEADER_VALUE_TYPE_S=2;
public static void main(String[] args) {
File dir = new File("e:\\2003.xls");
Excel2JSONHelper excelHelper = getExcel2JSONHelper();
//dir文件,0代表是第一行为保存到数据库或者实体类的表头,一般为英文的字符串,2代表是第二种模板,
JSONArray jsonArray = excelHelper.readExcle(dir, 0, 2);
System.out.println(jsonArray.toString());;
}
/**
*
* 获取一个实例
*/
private static Excel2JSONHelper getExcel2JSONHelper(){
return new Excel2JSONHelper();
}
/**
* 文件过滤
* @Title: fileNameFileter
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param:
* @author LiYonghui
* @date 2017年1月6日 下午4:45:42
* @return: void
* @throws
*/
private boolean fileNameFileter(File file){
boolean endsWith = false;
if(file != null){
String fileName = file.getName();
endsWith = fileName.endsWith(".xls") || fileName.endsWith(".xlsx");
}
return endsWith;
}
/**
* 获取表头行
* @Title: getHeaderRow
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param sheet
* @param: @param index
* @param: @return
* @author LiYonghui
* @date 2017年1月6日 下午5:05:24
* @return: Row
* @throws
*/
private Row getHeaderRow(Sheet sheet, int index){
Row headerRow = null;
if(sheet!=null){
headerRow = sheet.getRow(index);
}
return headerRow;
}
/**
* 获取表格中单元格的value
* @Title: getCellValue
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param row
* @param: @param cellIndex
* @param: @param formula
* @param: @return
* @author LiYonghui
* @date 2017年1月6日 下午5:40:28
* @return: Object
* @throws
*/
private Object getCellValue(Row row,int cellIndex,FormulaEvaluator formula){
Cell cell = row.getCell(cellIndex);
if(cell != null){
switch (cell.getCellType()) {
//String类型
case Cell.CELL_TYPE_STRING:
return cell.getRichStringCellValue().getString();
//number类型
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().getTime();
} else {
return cell.getNumericCellValue();
}
//boolean类型
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();
//公式
case Cell.CELL_TYPE_FORMULA:
return formula.evaluate(cell).getNumberValue();
default:
return null;
}
}
return null;
}
/**
* 获取表头value
* @Title: getHeaderCellValue
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param headerRow
* @param: @param cellIndex 英文表头所在的行,从0开始计算哦
* @param: @param type 表头的类型第一种 姓名(name)英文于实体类或者数据库中的变量一致
* @param: @return
* @author LiYonghui
* @date 2017年1月6日 下午6:12:21
* @return: String
* @throws
*/
private String getHeaderCellValue(Row headerRow,int cellIndex,int type){
Cell cell = headerRow.getCell(cellIndex);
String headerValue = null;
if(cell != null){
//第一种模板类型
if(type == HEADER_VALUE_TYPE_Z){
headerValue = cell.getRichStringCellValue().getString();
int l_bracket = headerValue.indexOf("(");
int r_bracket = headerValue.indexOf(")");
if(l_bracket == -1){
l_bracket = headerValue.indexOf("(");
}
if(r_bracket == -1){
r_bracket = headerValue.indexOf(")");
}
headerValue = headerValue.substring(l_bracket+1, r_bracket);
}else if(type == HEADER_VALUE_TYPE_S){
//第二种模板类型
headerValue = cell.getRichStringCellValue().getString();
}
}
return headerValue;
}
/**
* 读取excel表格
* @Title: readExcle
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param file
* @param: @param headerIndex
* @param: @param headType 表头的类型第一种 姓名(name)英文于实体类或者数据库中的变量一致
* @author LiYonghui
* @date 2017年1月6日 下午6:13:27
* @return: void
* @throws
*/
public JSONArray readExcle(File file,int headerIndex,int headType){
List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
if(!fileNameFileter(file)){
return null;
}else{
try {
//加载excel表格
WorkbookFactory wbFactory = new WorkbookFactory();
Workbook wb = wbFactory.create(file);
//读取第一个sheet页
Sheet sheet = wb.getSheetAt(0);
//读取表头行
Row headerRow = getHeaderRow(sheet, headerIndex);
//读取数据
FormulaEvaluator formula = wb.getCreationHelper().createFormulaEvaluator();
for(int r = headerIndex+1; r<= sheet.getLastRowNum();r++){
Row dataRow = sheet.getRow(r);
Map<String, Object> map = new HashMap<String, Object>();
for(int h = 0; h<dataRow.getLastCellNum();h++){
//表头为key
String key = getHeaderCellValue(headerRow,h,headType);
//数据为value
Object value = getCellValue(dataRow, h, formula);
if(!key.equals("") && !key.equals("null") && key != null ){
map.put(key, value);
}
}
lists.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
}
JSONArray jsonArray = JSONArray.fromObject(lists);
return jsonArray;
}
}
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
/**
* excel表格转成json
* @ClassName: Excel2JSONHelper
* @Description:TODO(这里用一句话描述这个类的作用)
* @author LiYonghui
* @date 2017年1月6日 下午4:42:43
*/
public class Excel2JSONHelper {
//常亮,用作第一种模板类型,如下图
private static final int HEADER_VALUE_TYPE_Z=1;
//第二种模板类型,如下图
private static final int HEADER_VALUE_TYPE_S=2;
public static void main(String[] args) {
File dir = new File("e:\\2003.xls");
Excel2JSONHelper excelHelper = getExcel2JSONHelper();
//dir文件,0代表是第一行为保存到数据库或者实体类的表头,一般为英文的字符串,2代表是第二种模板,
JSONArray jsonArray = excelHelper.readExcle(dir, 0, 2);
System.out.println(jsonArray.toString());;
}
/**
*
* 获取一个实例
*/
private static Excel2JSONHelper getExcel2JSONHelper(){
return new Excel2JSONHelper();
}
/**
* 文件过滤
* @Title: fileNameFileter
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param:
* @author LiYonghui
* @date 2017年1月6日 下午4:45:42
* @return: void
* @throws
*/
private boolean fileNameFileter(File file){
boolean endsWith = false;
if(file != null){
String fileName = file.getName();
endsWith = fileName.endsWith(".xls") || fileName.endsWith(".xlsx");
}
return endsWith;
}
/**
* 获取表头行
* @Title: getHeaderRow
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param sheet
* @param: @param index
* @param: @return
* @author LiYonghui
* @date 2017年1月6日 下午5:05:24
* @return: Row
* @throws
*/
private Row getHeaderRow(Sheet sheet, int index){
Row headerRow = null;
if(sheet!=null){
headerRow = sheet.getRow(index);
}
return headerRow;
}
/**
* 获取表格中单元格的value
* @Title: getCellValue
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param row
* @param: @param cellIndex
* @param: @param formula
* @param: @return
* @author LiYonghui
* @date 2017年1月6日 下午5:40:28
* @return: Object
* @throws
*/
private Object getCellValue(Row row,int cellIndex,FormulaEvaluator formula){
Cell cell = row.getCell(cellIndex);
if(cell != null){
switch (cell.getCellType()) {
//String类型
case Cell.CELL_TYPE_STRING:
return cell.getRichStringCellValue().getString();
//number类型
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().getTime();
} else {
return cell.getNumericCellValue();
}
//boolean类型
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();
//公式
case Cell.CELL_TYPE_FORMULA:
return formula.evaluate(cell).getNumberValue();
default:
return null;
}
}
return null;
}
/**
* 获取表头value
* @Title: getHeaderCellValue
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param headerRow
* @param: @param cellIndex 英文表头所在的行,从0开始计算哦
* @param: @param type 表头的类型第一种 姓名(name)英文于实体类或者数据库中的变量一致
* @param: @return
* @author LiYonghui
* @date 2017年1月6日 下午6:12:21
* @return: String
* @throws
*/
private String getHeaderCellValue(Row headerRow,int cellIndex,int type){
Cell cell = headerRow.getCell(cellIndex);
String headerValue = null;
if(cell != null){
//第一种模板类型
if(type == HEADER_VALUE_TYPE_Z){
headerValue = cell.getRichStringCellValue().getString();
int l_bracket = headerValue.indexOf("(");
int r_bracket = headerValue.indexOf(")");
if(l_bracket == -1){
l_bracket = headerValue.indexOf("(");
}
if(r_bracket == -1){
r_bracket = headerValue.indexOf(")");
}
headerValue = headerValue.substring(l_bracket+1, r_bracket);
}else if(type == HEADER_VALUE_TYPE_S){
//第二种模板类型
headerValue = cell.getRichStringCellValue().getString();
}
}
return headerValue;
}
/**
* 读取excel表格
* @Title: readExcle
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param file
* @param: @param headerIndex
* @param: @param headType 表头的类型第一种 姓名(name)英文于实体类或者数据库中的变量一致
* @author LiYonghui
* @date 2017年1月6日 下午6:13:27
* @return: void
* @throws
*/
public JSONArray readExcle(File file,int headerIndex,int headType){
List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
if(!fileNameFileter(file)){
return null;
}else{
try {
//加载excel表格
WorkbookFactory wbFactory = new WorkbookFactory();
Workbook wb = wbFactory.create(file);
//读取第一个sheet页
Sheet sheet = wb.getSheetAt(0);
//读取表头行
Row headerRow = getHeaderRow(sheet, headerIndex);
//读取数据
FormulaEvaluator formula = wb.getCreationHelper().createFormulaEvaluator();
for(int r = headerIndex+1; r<= sheet.getLastRowNum();r++){
Row dataRow = sheet.getRow(r);
Map<String, Object> map = new HashMap<String, Object>();
for(int h = 0; h<dataRow.getLastCellNum();h++){
//表头为key
String key = getHeaderCellValue(headerRow,h,headType);
//数据为value
Object value = getCellValue(dataRow, h, formula);
if(!key.equals("") && !key.equals("null") && key != null ){
map.put(key, value);
}
}
lists.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
}
JSONArray jsonArray = JSONArray.fromObject(lists);
return jsonArray;
}
}
- 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
excel表格模板类型和调用方式
**
第一种 :用括号把实体类变量名称或者数据库字段名称括起来
调用方法如下:
- 1
- 2
- 3
- 4
- 5
第二种: 实体类变量名称或者数据库字段另起一行,如下两张图都行
调用方法如下:
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5