您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)java如何實現(xiàn)將excel表格數(shù)據(jù)解析成JSONArray的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
/*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>
感謝各位的閱讀!關(guān)于“java如何實現(xiàn)將excel表格數(shù)據(jù)解析成JSONArray”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。