java讀取cvs文件并導(dǎo)入數(shù)據(jù)庫
本文實(shí)例為大家分享了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)入一個(gè)jxl的jar包
private static void getExecl(Statement statement) { jxl.Workbook readwb = null; try { // 構(gòu)建Workbook對(duì)象, 只讀Workbook對(duì)象 // 直接從本地文件創(chuàng)建Workbook,根據(jù)實(shí)際情況更改文件路徑 InputStream instream = new FileInputStream("文件路徑"); readwb = Workbook.getWorkbook(instream); // Sheet的下標(biāo)是從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(); } }
但是有些從平臺(tái),后臺(tái)之類的地方導(dǎo)出的excel是cvs類型。cvs是文本類型的文件,每一個(gè)單元格的數(shù)據(jù)使用“,”隔開。
public static void getExecl(Statement statement, String path) { try { BufferedReader reader = new BufferedReader(new FileReader(path));// 換成你的文件名 reader.readLine();// 第一行信息,為標(biāo)題信息,不用,如果需要,注釋掉 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格式文件為逗號(hào)分隔符文件,這里根據(jù)逗號(hào)切分 int j = 0; String item[] = list.get(i).split(","); } if (item[j] != null) { String id = item[0]; String datetime=item[8]; } } }
關(guān)于時(shí)間格式,excel中的時(shí)間需要格式化一下,才能導(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ù)庫。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java基于fork/koin類實(shí)現(xiàn)并發(fā)排序
這篇文章主要介紹了Java基于fork/koin類實(shí)現(xiàn)并發(fā)排序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02java實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01springboot項(xiàng)目idea熱部署的教程詳解
這篇文章主要介紹了springboot項(xiàng)目idea熱部署,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08解決復(fù)制springboot項(xiàng)目后,啟動(dòng)日志無顏色的問題
這篇文章主要介紹了解決復(fù)制springboot項(xiàng)目后,啟動(dòng)日志無顏色的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07