使用java讀取excel表格,而後將excel文件內容寫到json文件中,參考連接:http://blog.csdn.net/readiay/article/details/45540067java
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Map; public class ExcelToJson { JSONArray dataArray = new JSONArray(); ; String newFile = null; File dataJson = null; /** * 讀取xlsx文件 * * @param path * @throws IOException * @throws ParseException */ public void readXlsx(String path) throws IOException, ParseException { XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(path)); // 循環工做表Sheet for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) { XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); String sheetName = xssfSheet.getSheetName(); if (xssfSheet == null) { continue; } //當前sheet的json文件 JSONObject sheetJson = new JSONObject(); //當前sheet的array,做爲sheetJson 的value值 JSONArray sheetArr = new JSONArray(); //sheet的第一行,獲取做爲json的key值 JSONArray key = new JSONArray(); int xssfLastRowNum = xssfSheet.getLastRowNum(); System.out.println("當前表單的最後一行: " + xssfLastRowNum); // 循環行Row for (int rowNum = 0; rowNum <= xssfLastRowNum; rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow == null) { continue; } // 循環列Cell,在這裏組合json文件 int firstCellNum = xssfRow.getFirstCellNum(); int lastCellNum = xssfRow.getLastCellNum(); JSONObject rowJson = new JSONObject(); for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) { XSSFCell cell = null; try { cell = xssfRow.getCell(cellNum); if (cell == null) { rowJson.put(key.getString(cellNum), ""); continue; } if (rowNum == 0) key.add(toString(cell)); else { //如果列號超過了key的大小,則跳過 if (cellNum >= key.size()) continue; rowJson.put(key.getString(cellNum), toString(cell)); } } catch (Exception e) { e.printStackTrace(); } } if (!rowJson.isEmpty()) sheetArr.add(rowJson); } sheetJson.put(sheetName, shuffleData(sheetArr)); dataArray.add(sheetJson); } System.out.println(dataArray); } public static void main(String[] args) { String path = "C:\\Users\\garila\\Downloads\\test.xlsx"; String newFilePath = path.substring(0, path.lastIndexOf(".")); System.out.println(newFilePath); ExcelToJson excelToJson = new ExcelToJson(); try { excelToJson.readXlsx(path); excelToJson.createJson(newFilePath); excelToJson.writeToJson(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } public String toString(XSSFCell cell) { switch (cell.getCellTypeEnum()) { case _NONE: cell.setCellType(CellType.STRING); return ""; case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); return sdf.format(cell.getDateCellValue()); } cell.setCellType(CellType.STRING); return cell.getStringCellValue(); case STRING: String val = cell.getStringCellValue(); if ("無".equalsIgnoreCase(val)) return ""; return val; case FORMULA: return cell.getCellFormula(); case BLANK: return ""; case BOOLEAN: return cell.getBooleanCellValue() + ""; case ERROR: return "非法字符"; default: return "未知字符"; } } public void createJson(String path) { newFile = path + ".json"; dataJson = new File(newFile); if (dataJson.exists()) { dataJson.delete(); } else { try { dataJson.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } public void writeToJson() { FileWriter fw = null; try { fw = new FileWriter(newFile); fw.write(dataArray.toString()); fw.close(); } catch (Exception e) { } } public JSONArray shuffleData(JSONArray sheetArr) { JSONArray array = new JSONArray(); sheetArr.forEach(jsonStr -> { JSONObject object = JSONObject.parseObject(jsonStr.toString()); int count = 0; int length = 0; for (Map.Entry<String, Object> map : object.entrySet()) { String key = map.getKey(); Object o = map.getValue(); length++; boolean b = StringUtils.isEmpty(o.toString()); if (b) { count++; } } if (count != length) { array.add(object); } }); return array; } }
maven依賴:apache
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
poi-ooxml是讀取xlsx的依賴包json