这篇文章给大家分享的是有关java如何实现将excel表格数据解析成JSONArray的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
程序主体:
public static final int HEADER_VALUE_TYPE_O = 1;
public static ExcelToJson getExcelToJson() {
return new ExcelToJson();
}
public JSONArray readExcel(File file, int headerIndex, int headType) {
List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
if (!fileNameFileter(file)) {
return null;
} else {
try {
WorkbookFactory factory = new WorkbookFactory();
Workbook workbook = factory.create(file);
Sheet sheet = workbook.getSheetAt(0);
Row headerRow = getHeaderRow(sheet, headerIndex);
FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
for (int r = headerIndex + 1; r < sheet.getLastRowNum() + 1; r++) {
Row dataRow = sheet.getRow(r);
Map<String, Object> map = new HashMap<String, Object>();
for (int h = 0; h < dataRow.getLastCellNum(); h++) {
String key = getHeaderCellValue(headerRow, h, headType);
Object value = getCellValue(dataRow, h, formulaEvaluator);
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;
}
public boolean fileNameFileter(File file) {
boolean endsWith = false;
if (file != null) {
String fileName = file.getName();
endsWith = fileName.endsWith(".xls") || fileName.endsWith(".xlsx");
}
return endsWith;
}
public Row getHeaderRow(Sheet sheet, int index) {
Row headerRow = null;
if (sheet != null) {
headerRow = sheet.getRow(index);
}
return headerRow;
}
public String getHeaderCellValue(Row headerRow, int cellIndex, int type) {
Cell cell = headerRow.getCell(cellIndex);
String headerValue = null;
if (cell != null) {
if (HEADER_VALUE_TYPE_O == type) {
headerValue = cell.getRichStringCellValue().getString();
}
}
return headerValue;
}
public Object getCellValue(Row row, int cellIndex, FormulaEvaluator formulaEvaluator) {
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 formulaEvaluator.evaluate(cell).getNumberValue();
default:
return null;
}
}
return null;
}
测试方法:
public static void main(String[] args) {
File file = new File("C:\\a.xls");
ExcelToJson excelToJson = getExcelToJson();
JSONArray jsonArray = excelToJson.readExcel(file, 0, 1);
System.out.println(jsonArray.toString());
}
依赖的jar包:
public static void main(String[] args) {
File file = new File("C:\\a.xls");
ExcelToJson excelToJson = getExcelToJson();
JSONArray jsonArray = excelToJson.readExcel(file, 0, 1);
System.out.println(jsonArray.toString());
}
<!--POI-->
<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>
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<!--POI-->
<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>
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
感谢各位的阅读!关于“java如何实现将excel表格数据解析成JSONArray”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!