这篇文章给大家分享的是有关java如何实现将excel表格数据解析成JSONArray的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
/*json头模板*/
public static final int HEADER_VALUE_TYPE_O = 1;
/*实例*/
public static ExcelToJson getExcelToJson() {
return new ExcelToJson();
}
/*读取excel*/
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;
}
/*获取表头的value*/
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());
}
<!--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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。