1:需求:將excel中的數據獲取出來,轉化為json格式,之后輸出到.json文件中。
2:步驟:(1): 將excel中的數據獲取出來,使用jsonObject轉化為json格式字符串
(2):使用輸出流將json字符串輸出到json文件中
3:需要的依賴:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <classifier>jdk15</classifier> <version>2.2.3</version> </dependency> |
4:代碼
public class ExcelTojson { //json的key private static String[] titleArray = { "userName", "phoneNum"}; public static void main(String[] args) { InputStream is = null; String excelToJson = excelToJson(is); try { createJsonFile(excelToJson); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String excelToJson(InputStream is) { JSONObject jsonRow = new JSONObject(); try { // 獲取文件 is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\新建 XLSX 工作表.xlsx"); // 創建excel工作空間 Workbook workbook = WorkbookFactory.create(is); // 獲取sheet頁的數量 int sheetCount = workbook.getNumberOfSheets(); // 遍歷每個sheet頁 for (int i = 0; i < sheetCount; i++) { // 獲取每個sheet頁 Sheet sheet = workbook.getSheetAt(i); // 獲取sheet頁的總行數 int rowCount = sheet.getPhysicalNumberOfRows(); // 遍歷每一行 for (int j = 0; j < rowCount; j++) { //記錄每一行的一個唯一標識作為json的key String key=""; // 獲取列對象 Row row = sheet.getRow(j); // 獲取總列數 int cellCount = row.getPhysicalNumberOfCells(); Map<String, String> map = new HashMap<String, String>(); //除去第一行的標題 if (j > 0) { // 遍歷所有數據 for (int n = 0; n < cellCount; n++) { // 獲取每一個單元格 Cell cell = row.getCell(n); // 在讀取單元格內容前,設置所有單元格中內容都是字符串類型 cell.setCellType(Cell.CELL_TYPE_STRING); // 按照字符串類型讀取單元格內數據 String cellValue = cell.getStringCellValue(); if(n==1) { key=cellValue; } map.put(titleArray[n], cellValue); } } if (map.size() != 0) { jsonRow.put(key,map); } } } System.out.println(jsonRow.toString()); } catch (Exception e) { } return jsonRow.toString(); } public static void createJsonFile(String jsonData) throws IOException { File file = new File("C:\\Users\\Administrator\\Desktop\\用戶.json"); //連接流 FileOutputStream fos=null; //鏈接流 BufferedOutputStream bos =null; try { fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(jsonData.getBytes());; bos.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(null!=bos) { bos.close(); } if(null!=fos) { fos.close(); } } } } |
4:效果
excel文件數據:

json文件
{"20161031993":{"phoneNum":"20161031993","userName":"小明"},"20161031974":{"phoneNum":"20161031974","userName":"小紅"},"20161031847":{"phoneNum":"20161031847","userName":"小張"},"20161031893":{"phoneNum":"20161031893","userName":"小李"},"20161032008":{"phoneNum":"20161032008","userName":"小王"},"20161031811":{"phoneNum":"20161031811","userName":"小牛"},"20161032052":{"phoneNum":"20161032052","userName":"小宋"},"20161032042":{"phoneNum":"20161032042","userName":"小金"}} |
5:改進
可以查找java代碼,將輸出的json格式化,使其更加美觀。