溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java怎么讀取cvs文件并導(dǎo)入數(shù)據(jù)庫

發(fā)布時間:2021-08-17 18:38:19 來源:億速云 閱讀:269 作者:chen 欄目:編程語言

這篇文章主要講解了“java怎么讀取cvs文件并導(dǎo)入數(shù)據(jù)庫”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“java怎么讀取cvs文件并導(dǎo)入數(shù)據(jù)庫”吧!

本文實例為大家分享了java讀取cvs文件并導(dǎo)入數(shù)據(jù)庫的具體代碼,供大家參考,具體內(nèi)容如下

首先獲取文件夾下面的所有類型相同的excel,可以用模糊匹配contains(“匹配字段”)

public static List getDictory(String path) { File f = new File(path); List<String> dictories = new ArrayList<String>(); if (!f.exists()) {  System.out.println(path + "路徑不存在"); } else {  File fa[] = f.listFiles();  for (int i = 0; i < fa.length; i++) {  File fs = fa[i];  if (!fs.isDirectory() && fs.getName().contains("csv")) {   dictories.add(path + fs.getName());  }  }  System.out.println(dictories); } return dictories; }

操作jxl類型的excel表格需要導(dǎo)入一個jxl的jar包

private static void getExecl(Statement statement) { jxl.Workbook readwb = null; try {  // 構(gòu)建Workbook對象, 只讀Workbook對象  // 直接從本地文件創(chuàng)建Workbook,根據(jù)實際情況更改文件路徑  InputStream instream = new FileInputStream("文件路徑");  readwb = Workbook.getWorkbook(instream);  // Sheet的下標是從0開始  // 獲取第一張Sheet表  Sheet readsheet = readwb.getSheet(0);  // 獲取Sheet表中所包含的總行數(shù)  int rsRows = readsheet.getRows();  // 循環(huán)獲取excel的一行數(shù)據(jù)  for (int i = 2; i < rsRows; i++) {  // System.out.println("\n");  // 獲取需要導(dǎo)入數(shù)據(jù)庫的單元格(列)  int[] number = { 0, 4, 5, 7 };  Cell cell0 = readsheet.getCell(0, i);//第i行第一格  Cell cell4 = readsheet.getCell(4, i);//第i行第五格  Cell cell5 = readsheet.getCell(5, i);//第i行第六格    int id=cell0.getContents)();//獲取第一格的數(shù)據(jù)            }readwb.close();      }catch (Exception e) {  e.printStackTrace(); }}

但是有些從平臺,后臺之類的地方導(dǎo)出的excel是cvs類型。cvs是文本類型的文件,每一個單元格的數(shù)據(jù)使用“,”隔開。

public static void getExecl(Statement statement, String path) { try {  BufferedReader reader = new BufferedReader(new FileReader(path));// 換成你的文件名  reader.readLine();// 第一行信息,為標題信息,不用,如果需要,注釋掉  String line = null;  String everyLine = null;  List<String> list = new ArrayList<String>();  while ((line = reader.readLine()) != null) {  // 行數(shù)  everyLine = line;  list.add(everyLine);  }  // 讀每一行數(shù)據(jù)  for (int i = 1; i < list.size(); i++) {  // CSV格式文件為逗號分隔符文件,這里根據(jù)逗號切分  int j = 0;  String item[] = list.get(i).split(",");           }           if (item[j] != null) {           String id = item[0];           String datetime=item[8];                      }         } }

關(guān)于時間格式,excel中的時間需要格式化一下,才能導(dǎo)入數(shù)據(jù)庫中相應(yīng)的字段,而cvs的不用。前提是數(shù)據(jù)庫中的字段是datetime類型的。

String ReceiveTime = null;if (cell11.getType() == CellType.DATE) {    DateCell dc = (DateCell) cell11;    Date date = dc.getDate(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ReceiveTime = sdf.format(date);   }

最后連接數(shù)據(jù)庫。

感謝各位的閱讀,以上就是“java怎么讀取cvs文件并導(dǎo)入數(shù)據(jù)庫”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對java怎么讀取cvs文件并導(dǎo)入數(shù)據(jù)庫這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI