最近由於需要,需要將excel中的文件內容,導入到數據庫,就寫了一個Java讀取excel文件的方法。
核心代碼如下:
package com.*.service.ExcelImport;
/**
* @Author:
* @Description:
* @Date: Created at 17:09 2019-8-25
* @Modified By:
*/
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.*.controllers.ApiController;
import com.*.**.service.I**ModuleService;
import com.*.util.DSFClient;
import com.*.util.HttpUtil;
import jxl.*;
public class ReadXLS {
static I**ModuleService i**ModuleService = DSFClient.get**ModuleService();
public static void main(String args[]) {
try {
Workbook book = Workbook.getWorkbook(new File("C:\\Users\\*\\Desktop\\yuheng.xls"));
//獲得第一個工作簿對象
Sheet sheet = book.getSheet(0);
int rows = sheet.getRows();
//循環遍歷
int flaNum = 1;
for (int i = 0; i < rows; i++) {
Cell cell11 = sheet.getCell(0, i);
String moduleName = cell11.getContents();
Cell cell12 = sheet.getCell(1, i);
String manager = cell12.getContents();
Cell cell13 = sheet.getCell(2, i);
String qaManager = cell13.getContents();
Cell cell14 = sheet.getCell(3, i);
String qaTester = cell14.getContents();
// i**ModuleService.getMuduleByClusterName();
// System.out.println(moduleName + "#" + qaManager + "#" + qaTester + "#");
/* if (!strLis.contains(moduleName)) {
continue;
}*/
Map<String, String> mapString = new HashMap<>();
mapString.put("managerNames", manager);
mapString.put("qaManagerName", qaManager);
mapString.put("qaPersonName", qaTester);
mapString.put("moduleName", moduleName);
String httpGetResult = HttpUtil.sendGet("http://**.*-inc.com/api/moduleUpdate", mapString);
// ApiController apiController = new ApiController();
// apiController.updateModule("", qaManager, qaTester, moduleName);
if (JSONObject.parseObject(httpGetResult).getIntValue("repCode") == 1) {
System.out.println("模塊名:" + moduleName + "--->" + JSONObject.parseObject(httpGetResult).getString("repData"));
}
flaNum++;
}
System.out.println("總計:" + flaNum);
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
}