讀取Excel表格資料存入mongodb資料庫
阿新 • • 發佈:2019-02-14
@Test public void readExcel() throws IOException, BiffException { // 讀取xls檔案 InputStream ins = new FileInputStream("D:/lesiea/文件/course.xls"); // 設定讀檔案編碼 WorkbookSettings setEncode = new WorkbookSettings(); setEncode.setEncoding("UTF-8"); Workbook rwb = Workbook.getWorkbook(ins, setEncode); Sheet sheet = rwb.getSheet(0); int cols = sheet.getColumns(); // 列 int rows = sheet.getRows(); // 行 for(int i = 0;i<rows;i++){ Document document = new Document(); document.put("_id",UUID.randomUUID().toString().replace("-","")); for (int c = 0; c < cols; c++) { Cell excelRows = sheet.getCell(c, 0); Cell excel = sheet.getCell(c, i+1); String strRow = excelRows.getContents(); String str = excel.getContents(); document.put(strRow,str); } baseDao.geCollection("course").insertOne(document); } ins.close(); }