多個(gè)sheet Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
多個(gè)sheet Excel 數(shù)據(jù) 導(dǎo)入數(shù)據(jù)庫(kù) 如何實(shí)現(xiàn)?
將 Excel 文件中的多個(gè) sheet 導(dǎo)入數(shù)據(jù)庫(kù),一般有以下幾種實(shí)現(xiàn)方式:
- 使用 JDBC 直接插入。可以使用 Java 的 JDBC 接口直接連接數(shù)據(jù)庫(kù),然后讀取 Excel 文件中的數(shù)據(jù),并將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。這種方式比較直接,但需要編寫大量的 JDBC 代碼,對(duì) Excel 文件格式的支持也比較有限。
- 使用第三方庫(kù)。市面上有很多 Java 的第三方庫(kù)可以用來(lái)讀取 Excel 文件,如 Apache POI、JExcelAPI、EasyExcel 等。這些庫(kù)通常都提供了比較簡(jiǎn)單易用的 API,可以方便地讀取 Excel 文件中的數(shù)據(jù),并將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。
- 先將 Excel 文件轉(zhuǎn)換成 CSV 文件,再導(dǎo)入數(shù)據(jù)庫(kù)。Excel 文件可以先轉(zhuǎn)換成 CSV 文件,然后使用 JDBC 直接將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。CSV 文件相對(duì)于 Excel 文件來(lái)說(shuō),結(jié)構(gòu)更加簡(jiǎn)單,處理起來(lái)也更加方便。
無(wú)論使用哪種方式,都需要注意以下幾個(gè)問(wèn)題:
Excel 文件格式的兼容性問(wèn)題。不同版本的 Excel 文件可能存在格式差異,需要進(jìn)行測(cè)試和兼容性處理。
數(shù)據(jù)的類型和格式問(wèn)題。Excel 文件中的數(shù)據(jù)類型和格式可能需要進(jìn)行轉(zhuǎn)換和處理,以適配數(shù)據(jù)庫(kù)中的數(shù)據(jù)類型和格式要求。
數(shù)據(jù)的一致性問(wèn)題。如果 Excel 文件中的數(shù)據(jù)有重復(fù)或沖突,需要進(jìn)行處理,以保證數(shù)據(jù)的一致性和完整性。
綜上所述,將 Excel 文件中的多個(gè) sheet 導(dǎo)入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方式有多種,具體使用哪種方式,還需要根據(jù)實(shí)際情況進(jìn)行評(píng)估和選擇。
傳統(tǒng)方式
處理 普通數(shù)據(jù)的 Excel 文件,需要考慮到內(nèi)存和性能的問(wèn)題,以下是一個(gè)基于流式讀取和寫入的示例代碼:
// 獲取 Excel 文件輸入流 InputStream is = new BufferedInputStream(new FileInputStream(filePath)); Workbook workbook = WorkbookFactory.create(is); // 遍歷每個(gè) Sheet for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) { Sheet sheet = workbook.getSheetAt(sheetIndex); String sheetName = sheet.getSheetName(); System.out.println("開始處理 Sheet:" + sheetName); // 準(zhǔn)備寫入的輸出流 OutputStream os = new BufferedOutputStream(new FileOutputStream(outputDir + "/" + sheetName + ".xlsx")); // 設(shè)置寫入的 Sheet 名稱 SXSSFWorkbook writer = new SXSSFWorkbook(new XSSFWorkbook(), 10000); SXSSFSheet outSheet = writer.createSheet(sheetName); // 讀取并寫入 Sheet 的標(biāo)題行 Row titleRow = sheet.getRow(0); Row outTitleRow = outSheet.createRow(0); for (int i = 0; i < titleRow.getLastCellNum(); i++) { outTitleRow.createCell(i).setCellValue(titleRow.getCell(i).getStringCellValue()); } // 逐行讀取并寫入數(shù)據(jù) for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); Row outRow = outSheet.createRow(i); for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); if (cell != null) { switch (cell.getCellType()) { case BLANK: outRow.createCell(j, CellType.BLANK); break; case BOOLEAN: outRow.createCell(j, CellType.BOOLEAN).setCellValue(cell.getBooleanCellValue()); break; case ERROR: outRow.createCell(j, CellType.ERROR).setCellValue(cell.getErrorCellValue()); break; case FORMULA: outRow.createCell(j, CellType.FORMULA).setCellFormula(cell.getCellFormula()); break; case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { outRow.createCell(j, CellType.NUMERIC).setCellValue(cell.getDateCellValue()); } else { outRow.createCell(j, CellType.NUMERIC).setCellValue(cell.getNumericCellValue()); } break; case STRING: outRow.createCell(j, CellType.STRING).setCellValue(cell.getStringCellValue()); break; default: outRow.createCell(j, CellType.BLANK); break; } } } // 每隔 10000 行進(jìn)行一次緩存寫入 if (i % 10000 == 0) { ((SXSSFSheet) outSheet).flushRows(); } } // 最后寫入緩存的數(shù)據(jù) writer.write(os); os.flush(); os.close(); writer.dispose(); System.out.println("處理 Sheet:" + sheetName + " 完成"); } // 關(guān)閉輸入流 is.close();
上述示例代碼使用了 Apache POI 的流式讀取和寫入方式,可以有效地處理大量數(shù)據(jù)。為了避免內(nèi)存溢出,采用了緩存寫入的方式,每隔一定數(shù)量的行進(jìn)行一次寫入操作。
Apache POI
使用 Apache POI 實(shí)現(xiàn)將 Excel 文件中的多個(gè) sheet 導(dǎo)入到數(shù)據(jù)庫(kù)的 Java 代碼:
import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelImporter { private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase"; private static final String DB_USER = "myuser"; private static final String DB_PASSWORD = "mypassword"; private static final String INSERT_SQL = "INSERT INTO mytable (col1, col2, col3) VALUES (?, ?, ?)"; public static void main(String[] args) { try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) { FileInputStream file = new FileInputStream("myexcel.xlsx"); Workbook workbook = new XSSFWorkbook(file); int numSheets = workbook.getNumberOfSheets(); for (int i = 0; i < numSheets; i++) { Sheet sheet = workbook.getSheetAt(i); for (Row row : sheet) { String col1 = null; String col2 = null; int col3 = 0; for (Cell cell : row) { int columnIndex = cell.getColumnIndex(); switch (columnIndex) { case 0: col1 = cell.getStringCellValue(); break; case 1: col2 = cell.getStringCellValue(); break; case 2: col3 = (int) cell.getNumericCellValue(); break; default: // Ignore other columns break; } } PreparedStatement statement = conn.prepareStatement(INSERT_SQL); statement.setString(1, col1); statement.setString(2, col2); statement.setInt(3, col3); statement.executeUpdate(); } } System.out.println("Import successful"); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
在上面的代碼中,首先通過(guò) FileInputStream 和 Workbook 對(duì)象讀取 Excel 文件中的數(shù)據(jù),然后通過(guò) for 循環(huán)遍歷每個(gè) sheet 和每行數(shù)據(jù),并將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。在讀取單元格數(shù)據(jù)時(shí),可以根據(jù)單元格的列索引和數(shù)據(jù)類型進(jìn)行類型轉(zhuǎn)換和賦值。最后通過(guò) PreparedStatement 執(zhí)行 SQL 插入語(yǔ)句,將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。
需要注意的是,上面的代碼只是一個(gè)簡(jiǎn)單的示例,還需要根據(jù)實(shí)際情況進(jìn)行修改和完善,比如加入異常處理、事務(wù)管理等功能。
JExcelAPI
使用 JExcelAPI 實(shí)現(xiàn)將 Excel 文件中的多個(gè) sheet 導(dǎo)入到數(shù)據(jù)庫(kù)的 Java 代碼:
import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; public class ExcelImporter { private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase"; private static final String DB_USER = "myuser"; private static final String DB_PASSWORD = "mypassword"; private static final String INSERT_SQL = "INSERT INTO mytable (col1, col2, col3) VALUES (?, ?, ?)"; public static void main(String[] args) { try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) { Workbook workbook = Workbook.getWorkbook(new File("myexcel.xls")); int numSheets = workbook.getNumberOfSheets(); for (int i = 0; i < numSheets; i++) { Sheet sheet = workbook.getSheet(i); for (int j = 1; j < sheet.getRows(); j++) { String col1 = null; String col2 = null; int col3 = 0; for (int k = 0; k < sheet.getColumns(); k++) { Cell cell = sheet.getCell(k, j); switch (k) { case 0: col1 = cell.getContents(); break; case 1: col2 = cell.getContents(); break; case 2: col3 = Integer.parseInt(cell.getContents()); break; default: // Ignore other columns break; } } PreparedStatement statement = conn.prepareStatement(INSERT_SQL); statement.setString(1, col1); statement.setString(2, col2); statement.setInt(3, col3); statement.executeUpdate(); } } System.out.println("Import successful"); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
在上面的代碼中,首先通過(guò) Workbook 對(duì)象讀取 Excel 文件中的數(shù)據(jù),然后通過(guò) for 循環(huán)遍歷每個(gè) sheet 和每行數(shù)據(jù),并將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。在讀取單元格數(shù)據(jù)時(shí),可以根據(jù)單元格的行索引、列索引和數(shù)據(jù)類型進(jìn)行類型轉(zhuǎn)換和賦值。最后通過(guò) PreparedStatement 執(zhí)行 SQL 插入語(yǔ)句,將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。
需要注意的是,上面的代碼只是一個(gè)簡(jiǎn)單的示例,還需要根據(jù)實(shí)際情況進(jìn)行修改和完善,比如加入異常處理、事務(wù)管理等功能。另外,JExcelAPI 只支持舊版的 .xls 格式,不支持 .xlsx 格式
。
EasyExcel
使用 EasyExcel 實(shí)現(xiàn)將 Excel 文件中的多個(gè) sheet 導(dǎo)入到數(shù)據(jù)庫(kù)的 Java 代碼:
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.metadata.Sheet; import java.util.ArrayList; import java.util.List; public class ExcelImporter { private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase"; private static final String DB_USER = "myuser"; private static final String DB_PASSWORD = "mypassword"; private static final String INSERT_SQL = "INSERT INTO mytable (col1, col2, col3) VALUES (?, ?, ?)"; public static void main(String[] args) { List<List<Object>> data = new ArrayList<>(); EasyExcel.read("myexcel.xlsx", new MyEventListener()).sheet().doRead(); try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) { PreparedStatement statement = conn.prepareStatement(INSERT_SQL); for (List<Object> row : data) { statement.setString(1, (String) row.get(0)); statement.setString(2, (String) row.get(1)); statement.setInt(3, (Integer) row.get(2)); statement.addBatch(); } statement.executeBatch(); System.out.println("Import successful"); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } static class MyEventListener extends AnalysisEventListener<Object> { private List<Object> row = new ArrayList<>(); @Override public void invoke(Object data, AnalysisContext context) { row.add(data); if (context.getCurrentRowNum() == 0) { // Ignore the header row row.clear(); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { // Ignore } @Override public void doAfterAllAnalysed(AnalysisContext context) { // Ignore } } }
在上面的代碼中,首先通過(guò) EasyExcel 對(duì)象讀取 Excel 文件中的數(shù)據(jù),然后通過(guò) AnalysisEventListener 監(jiān)聽(tīng)器將每行數(shù)據(jù)存儲(chǔ)到一個(gè) List 中,最后將 List 中的數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。需要注意的是,在處理每行數(shù)據(jù)時(shí),需要根據(jù)數(shù)據(jù)類型進(jìn)行類型轉(zhuǎn)換和賦值。此外,EasyExcel 支持 .xlsx 和 .xls 格式的 Excel 文件,但由于 .xlsx 格式的文件在讀取時(shí)需要占用大量?jī)?nèi)存,因此建議在處理大量數(shù)據(jù)時(shí)使用 .xls 格式。
需要注意的是,上面的代碼只是一個(gè)簡(jiǎn)單的示例,還需要根據(jù)實(shí)際情況進(jìn)行修改和完善,比如加入異常處理、事務(wù)管理等功能。另外,EasyExcel 還提供了很多高級(jí)功能,比如讀取大量數(shù)據(jù)時(shí)的分頁(yè)讀取、讀取時(shí)的數(shù)據(jù)轉(zhuǎn)換和驗(yàn)證等??梢愿鶕?jù)實(shí)際需求進(jìn)行使用。
總結(jié)
除了使用 Apache POI 和 EasyExcel 這兩個(gè)庫(kù)之外,還有其他的實(shí)現(xiàn)方式,比如:
使用 OpenCSV:OpenCSV 是一個(gè)輕量級(jí)的 CSV 格式文件讀寫庫(kù),也支持讀寫 Excel 文件。與 Apache POI 相比,它的內(nèi)存占用更少,但功能相對(duì)較少。
使用 JExcelAPI:JExcelAPI 是一個(gè)老牌的 Java Excel 文件讀寫庫(kù),也支持讀寫多個(gè) sheet。與 Apache POI 相比,它的內(nèi)存占用更少,但功能相對(duì)較少。
使用 Excel Streaming Reader:Excel Streaming Reader 是一個(gè)基于 SAX 的 Excel 文件讀取庫(kù),能夠高效地讀取大型 Excel 文件。與 Apache POI 相比,它的內(nèi)存占用更少,但功能相對(duì)較少。
使用 CSV 文件代替 Excel 文件:如果數(shù)據(jù)量不是很大,并且不需要使用 Excel 特有的功能,可以將 Excel 文件轉(zhuǎn)換為 CSV 格式文件,然后使用 OpenCSV 或其他的 CSV 文件讀寫庫(kù)進(jìn)行讀寫。
需要根據(jù)實(shí)際情況選擇合適的實(shí)現(xiàn)方式,綜合考慮內(nèi)存占用、性能、功能等因素。
結(jié)語(yǔ)
到此這篇關(guān)于多個(gè)sheet Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)多個(gè)sheet Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java常用HASH算法總結(jié)【經(jīng)典實(shí)例】
這篇文章主要介紹了Java常用HASH算法,結(jié)合實(shí)例形式總結(jié)分析了Java常用的Hash算法,包括加法hash、旋轉(zhuǎn)hash、FNV算法、RS算法hash、PJW算法、ELF算法、BKDR算法、SDBM算法、DJB算法、DEK算法、AP算法等,需要的朋友可以參考下2017-09-09手把手帶你分析SpringBoot自動(dòng)裝配完成了Ribbon哪些核心操作
這篇文章主要介紹了詳解Spring Boot自動(dòng)裝配Ribbon哪些核心操作的哪些操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08Mybatis錯(cuò)誤引起的程序啟動(dòng)卡死問(wèn)題及解決
這篇文章主要介紹了Mybatis錯(cuò)誤引起的程序啟動(dòng)卡死問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot項(xiàng)目@Async方法問(wèn)題解決方案
這篇文章主要介紹了SpringBoot項(xiàng)目@Async方法問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04java 中使用maven shade plugin 打可執(zhí)行Jar包
這篇文章主要介紹了java 中使用maven shade plugin 打可執(zhí)行Jar包的相關(guān)資料,需要的朋友可以參考下2017-05-05詳解如何全注解方式構(gòu)建SpringMVC項(xiàng)目
這篇文章主要介紹了詳解如何全注解方式構(gòu)建SpringMVC項(xiàng)目,利用Eclipse構(gòu)建SpringMVC項(xiàng)目,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕節(jié)快樂(lè))
這篇文章主要介紹了java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕節(jié)快樂(lè)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12