0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaでSQLiteデータベースの内容をExcelにエクスポートする方法

Last updated at Posted at 2025-05-09

業務システムでは、データのエクスポート機能がよく求められます。Excelファイルとして出力することで、報告書の作成やデータの共有が容易になります。

この記事では、Javaを使ってSQLiteのデータをExcelファイルに書き出す方法を解説します。

使用技術

  • SQLite:データ取得元となる軽量データベース
  • JDBC:SQLクエリを実行しデータを取得するためのAPI
  • Free Spire.XLS for Java:Excelファイルの作成と保存に使用

処理の手順

  1. JDBCでSQLiteデータベースに接続し、テーブルを取得
  2. 各テーブルの全データをSQLで取得
  3. Excelワークブックを作成し、カラム名とデータをシートに書き込み
  4. 最終的にExcelファイルとして保存

Javaコード例:SQLite → Excel

import com.spire.xls.*;
import java.sql.*;

public class SQLiteToExcel {
    public static void main(String[] args) {
        String dbPath = "jdbc:sqlite:BusinessData.db";
        String outputExcel = "output/exported_data.xlsx";

        // Excel ワークブックを作成し、デフォルトのワークシートを削除する
        Workbook workbook = new Workbook();
        workbook.getWorksheets().clear();

        try (Connection conn = DriverManager.getConnection(dbPath)) {
            DatabaseMetaData meta = conn.getMetaData();
            ResultSet tables = meta.getTables(null, null, "%", new String[]{"TABLE"});

            while (tables.next()) {
                String tableName = tables.getString("TABLE_NAME");
                Worksheet sheet = workbook.getWorksheets().add(tableName);

                // テーブル全体をクエリで取得
                String query = "SELECT * FROM `" + tableName + "`";
                try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query)) {
                    ResultSetMetaData rsmd = rs.getMetaData();
                    int columnCount = rsmd.getColumnCount();

                    // 列名(ヘッダー)を書き込む
                    for (int i = 1; i <= columnCount; i++) {
                        sheet.getCellRange(1, i).setText(rsmd.getColumnName(i));
                    }

                    // データを書き込む
                    int rowIndex = 2;
                    while (rs.next()) {
                        for (int i = 1; i <= columnCount; i++) {
                            String value = rs.getString(i);
                            sheet.getCellRange(rowIndex, i).setText(value != null ? value : "");
                        }
                        rowIndex++;
                    }
                }
            }

            // デフォルトの空ワークシートを削除(未使用の場合)
            if (workbook.getWorksheets().get(0).getName().equalsIgnoreCase("Sheet1") &&
                    workbook.getWorksheets().getCount() > 1) {
                workbook.getWorksheets().removeAt(0);
            }

            // Excel ファイルを保存
            workbook.saveToFile(outputExcel, ExcelVersion.Version2016);
            System.out.println("Success!");

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

データベースの例
JavaでSQLiteからExcelにデータをエクスポートする例

出力された Excel ファイル
JavaでSQLiteからExcelにデータをエクスポートした結果

まとめ

本記事では、SQLiteのデータをJavaでExcelファイルにエクスポートする方法を紹介しました。報告書の作成、データのバックアップや分析用途で非常に有用です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?