Java讀取Excel檔案的內容,歸檔到json檔案中(對json資料進行讀取、新增/修改put、刪除remove操作)
阿新 • • 發佈:2021-10-11
Java讀取Excel檔案的內容,歸檔到json檔案中(對json資料進行讀取、新增/修改put、刪除remove操作)
今天有個需求,寫個指令碼檔案,對Excel中的資料,歸檔到json檔案中(其實就是將Excel檔案中的內容,新增或修改到json檔案中所對應的屬性欄位:uid、name和head)。
其實我也就是使用Java寫了個工具類,使用檔案流來實現上面的操作(需求)。
不廢話,直接上程式碼:
package com.dw.datamodificationscript.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject;import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import org.junit.Test; import java.io.*; public class ExcelToJsonUtil { public static final String USER_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\user111.xls"; //user.xls public static final String DUB_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\dub111.json"; //dub.json public static final String INDEX_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\index111.json"; //index.json public static final String NEW_DUB_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\new_dub.json"; //new_dub.json public static final String NEW_INDEX_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\new_index.json"; //new_index.json @Test public void dubJsonFile_test(){ //歸檔第一個json檔案,先讀取json檔案,更新(新增或替換,其實都是put操作)內容,通過流輸出到一個新的json檔案 try{ File dub_file = new File(NEW_DUB_PATH); if (!dub_file.exists()){ try { dub_file.createNewFile(); }catch (IOException e) { e.printStackTrace(); } } String dub = readJsonFile(DUB_PATH); JSONObject json_dub = JSON.parseObject(dub); JSONArray dubs = json_dub.getJSONArray("dubs"); JSONArray new_dubs = new JSONArray(dubs.size()); Workbook wb = readExcelFile(USER_PATH); //int sheetSize = wb.getNumberOfSheets(); //獲取sheet總頁數 Sheet sheet = wb.getSheet(0); //獲取第一頁 int row_total = sheet.getRows(); //System.out.println("========================user的長度:" + row_total); //System.out.println("========================dubs的長度:" + dubs.size()); for (int i = 0 ; i < dubs.size(); i++) {
//更新操作,具體邏輯操作可以不用看,知道大致流程就好(具體問題,具體分析) JSONObject dub_item = dubs.getJSONObject(i); JSONObject user = dub_item.getJSONObject("user"); int j = i + 1; //排除第一行標題 Cell[] cells = sheet.getRow(j); user.put("uid", cells[0].getContents()); user.put("name", cells[1].getContents()); user.put("head", cells[2].getContents()); new_dubs.add(dub_item); } json_dub.put("dubs", new_dubs); writeJsonFile(json_dub.toString(), NEW_DUB_PATH); }catch (Exception e) { e.printStackTrace(); } } @Test public void indexJsonFile_test(){ //歸檔第二個json檔案,操作類似,區別就是讀取json檔案中的需要進行修改的欄位的json物件(說白了就是怎麼獲取該欄位json物件,並對其進行更新操作) try{ File index_file = new File(NEW_INDEX_PATH); if (!index_file.exists()){ try { index_file.createNewFile(); }catch (IOException e) { e.printStackTrace(); } } String index = readJsonFile(INDEX_PATH); JSONObject json_listens = JSON.parseObject(index); JSONArray listens = json_listens.getJSONArray("listens"); JSONArray new_listens = new JSONArray(listens.size()); Workbook wb = readExcelFile(USER_PATH); //int sheetSize = wb.getNumberOfSheets(); //獲取sheet總頁數 Sheet sheet = wb.getSheet(0); //獲取第一頁 int row_total = sheet.getRows(); //System.out.println("========================user的長度:" + row_total); //System.out.println("========================listens的長度:" + listens.size()); for (int i = 0 ; i < listens.size(); i++) {
// 更新操作,具體操作邏輯可以忽略不看,知道大致流程就好(具體問題,具體分析) JSONObject listens_item = listens.getJSONObject(i); JSONObject dub = listens_item.getJSONObject("dub"); JSONObject user = dub.getJSONObject("user"); int j = i + 21; //為了避免重複使用,排除第一行標題 + 後面的有效資料20行(dub.json檔案已使用的行數) Cell[] cells = sheet.getRow(j); user.put("uid", cells[0].getContents()); user.put("name", cells[1].getContents()); user.put("head", cells[2].getContents()); new_listens.add(listens_item); } json_listens.put("listens", new_listens); writeJsonFile(json_listens.toString(), NEW_INDEX_PATH); }catch (Exception e) { e.printStackTrace(); } } /** * @Function 讀取Excel檔案內容 * @Date 2021年10月9日16:29:52 * @Author Liangjw */ public Workbook readExcelFile(String filePath) { try { Workbook wb = null; InputStream is = new FileInputStream(USER_PATH); wb = Workbook.getWorkbook(is); is.close(); return wb; }catch (Exception e) { e.printStackTrace(); return null; }finally { } } /** * @Function 讀取json檔案 * @Date 2021年10月9日11:21:24 * @Author Liangjw */ public static String readJsonFile(String filePath) { String jsonStr = ""; try { File jsonFile = new File(filePath); //FileReader fileReader = new FileReader(jsonFile); Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } //fileReader.close(); reader.close(); jsonStr = sb.toString(); return jsonStr; } catch (IOException e) { e.printStackTrace(); return null; } } /** * @Function 寫出json檔案 * @Date 2021年10月9日14:21:24 * @Author Liangjw */ public static void writeJsonFile(String newJsonString, String path){ try { FileWriter fw = new FileWriter(path); PrintWriter out = new PrintWriter(fw); out.write(newJsonString); out.println(); fw.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
// 刪除操作我就不演示了,其實就是remove操作 /*for(int i = 0; i < xx.size; i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); if(model.getModelid().equals(jsonObject.getString("id"))){ jsonArray.remove(jsonObject); } }*/
其實,主要流程就是:
1. 讀取Excel、json檔案到檔案流中,通過檔案流獲取內容;
2. 操作自己的業務邏輯(新增、刪除、更新等操作)
3. 將檔案流輸出到新的json檔案
TRANSLATE with x English TRANSLATE with EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back