这篇文章给大家分享的是有关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 
- 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 - map = new HashMap - (); 
- 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()); 
- } 
- org.apache.poi 
- poi 
- 3.15 
- org.apache.poi 
- poi-ooxml 
- 3.15 
- net.sourceforge.jexcelapi 
- jxl 
- 2.6.12 
感谢各位的阅读!关于“java如何实现将excel表格数据解析成JSONArray”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
名称栏目:java如何实现将excel表格数据解析成JSONArray
当前网址:http://www.jxjierui.cn/article/pdhopi.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 