溫馨提示×

溫馨提示×

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

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

多個sheet?Excel數(shù)據(jù)怎么導入數(shù)據(jù)庫

發(fā)布時間:2023-03-27 14:30:45 來源:億速云 閱讀:123 作者:iii 欄目:數(shù)據(jù)庫

本篇內(nèi)容介紹了“多個sheet Excel數(shù)據(jù)怎么導入數(shù)據(jù)庫”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

多個sheet Excel 數(shù)據(jù) 導入數(shù)據(jù)庫 如何實現(xiàn)?

將 Excel 文件中的多個 sheet 導入數(shù)據(jù)庫,一般有以下幾種實現(xiàn)方式:

  • 使用 JDBC 直接插入。可以使用 Java 的 JDBC 接口直接連接數(shù)據(jù)庫,然后讀取 Excel 文件中的數(shù)據(jù),并將數(shù)據(jù)插入到數(shù)據(jù)庫中。這種方式比較直接,但需要編寫大量的 JDBC 代碼,對 Excel 文件格式的支持也比較有限。

  • 使用第三方庫。市面上有很多 Java 的第三方庫可以用來讀取 Excel 文件,如 Apache POI、JExcelAPI、EasyExcel 等。這些庫通常都提供了比較簡單易用的 API,可以方便地讀取 Excel 文件中的數(shù)據(jù),并將數(shù)據(jù)插入到數(shù)據(jù)庫中。

  • 先將 Excel 文件轉(zhuǎn)換成 CSV 文件,再導入數(shù)據(jù)庫。Excel 文件可以先轉(zhuǎn)換成 CSV 文件,然后使用 JDBC 直接將數(shù)據(jù)插入到數(shù)據(jù)庫中。CSV 文件相對于 Excel 文件來說,結(jié)構(gòu)更加簡單,處理起來也更加方便。

無論使用哪種方式,都需要注意以下幾個問題:

Excel 文件格式的兼容性問題。不同版本的 Excel 文件可能存在格式差異,需要進行測試和兼容性處理。

數(shù)據(jù)的類型和格式問題。Excel 文件中的數(shù)據(jù)類型和格式可能需要進行轉(zhuǎn)換和處理,以適配數(shù)據(jù)庫中的數(shù)據(jù)類型和格式要求。

數(shù)據(jù)的一致性問題。如果 Excel 文件中的數(shù)據(jù)有重復或沖突,需要進行處理,以保證數(shù)據(jù)的一致性和完整性。

綜上所述,將 Excel 文件中的多個 sheet 導入數(shù)據(jù)庫的實現(xiàn)方式有多種,具體使用哪種方式,還需要根據(jù)實際情況進行評估和選擇。

傳統(tǒng)方式

處理 普通數(shù)據(jù)的 Excel 文件,需要考慮到內(nèi)存和性能的問題,以下是一個基于流式讀取和寫入的示例代碼:

// 獲取 Excel 文件輸入流
InputStream is = new BufferedInputStream(new FileInputStream(filePath));
Workbook workbook = WorkbookFactory.create(is);

// 遍歷每個 Sheet
for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
    Sheet sheet = workbook.getSheetAt(sheetIndex);
    String sheetName = sheet.getSheetName();
    System.out.println("開始處理 Sheet:" + sheetName);

    // 準備寫入的輸出流
    OutputStream os = new BufferedOutputStream(new FileOutputStream(outputDir + "/" + sheetName + ".xlsx"));

    // 設置寫入的 Sheet 名稱
    SXSSFWorkbook writer = new SXSSFWorkbook(new XSSFWorkbook(), 10000);
    SXSSFSheet outSheet = writer.createSheet(sheetName);

    // 讀取并寫入 Sheet 的標題行
    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 行進行一次緩存寫入
        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ù)量的行進行一次寫入操作。

Apache POI

使用 Apache POI 實現(xiàn)將 Excel 文件中的多個 sheet 導入到數(shù)據(jù)庫的 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();
        }
    }
}

在上面的代碼中,首先通過 FileInputStream 和 Workbook 對象讀取 Excel 文件中的數(shù)據(jù),然后通過 for 循環(huán)遍歷每個 sheet 和每行數(shù)據(jù),并將數(shù)據(jù)插入到數(shù)據(jù)庫中。在讀取單元格數(shù)據(jù)時,可以根據(jù)單元格的列索引和數(shù)據(jù)類型進行類型轉(zhuǎn)換和賦值。最后通過 PreparedStatement 執(zhí)行 SQL 插入語句,將數(shù)據(jù)插入到數(shù)據(jù)庫中。

需要注意的是,上面的代碼只是一個簡單的示例,還需要根據(jù)實際情況進行修改和完善,比如加入異常處理、事務管理等功能。

JExcelAPI

使用 JExcelAPI 實現(xiàn)將 Excel 文件中的多個 sheet 導入到數(shù)據(jù)庫的 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();
        }
    }
}

在上面的代碼中,首先通過 Workbook 對象讀取 Excel 文件中的數(shù)據(jù),然后通過 for 循環(huán)遍歷每個 sheet 和每行數(shù)據(jù),并將數(shù)據(jù)插入到數(shù)據(jù)庫中。在讀取單元格數(shù)據(jù)時,可以根據(jù)單元格的行索引、列索引和數(shù)據(jù)類型進行類型轉(zhuǎn)換和賦值。最后通過 PreparedStatement 執(zhí)行 SQL 插入語句,將數(shù)據(jù)插入到數(shù)據(jù)庫中。

需要注意的是,上面的代碼只是一個簡單的示例,還需要根據(jù)實際情況進行修改和完善,比如加入異常處理、事務管理等功能。另外,JExcelAPI 只支持舊版的 .xls 格式,不支持 .xlsx 格式。

EasyExcel

使用 EasyExcel 實現(xiàn)將 Excel 文件中的多個 sheet 導入到數(shù)據(jù)庫的 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
        }
    }
}

在上面的代碼中,首先通過 EasyExcel 對象讀取 Excel 文件中的數(shù)據(jù),然后通過 AnalysisEventListener 監(jiān)聽器將每行數(shù)據(jù)存儲到一個 List 中,最后將 List 中的數(shù)據(jù)插入到數(shù)據(jù)庫中。需要注意的是,在處理每行數(shù)據(jù)時,需要根據(jù)數(shù)據(jù)類型進行類型轉(zhuǎn)換和賦值。此外,EasyExcel 支持 .xlsx 和 .xls 格式的 Excel 文件,但由于 .xlsx 格式的文件在讀取時需要占用大量內(nèi)存,因此建議在處理大量數(shù)據(jù)時使用 .xls 格式。

需要注意的是,上面的代碼只是一個簡單的示例,還需要根據(jù)實際情況進行修改和完善,比如加入異常處理、事務管理等功能。另外,EasyExcel 還提供了很多高級功能,比如讀取大量數(shù)據(jù)時的分頁讀取、讀取時的數(shù)據(jù)轉(zhuǎn)換和驗證等??梢愿鶕?jù)實際需求進行使用。

“多個sheet Excel數(shù)據(jù)怎么導入數(shù)據(jù)庫”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(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