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にエクスポートする方法

Posted at

業務システムでは、データのエクスポート機能がよく求められます。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:excel_data.db"; // SQLiteデータベースのパス
        String outputExcel = "exported_data.xlsx";    // 出力するExcelファイル名

        Workbook workbook = new Workbook(); // 新しいExcelワークブックを作成

        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(); // カラム数

                    // カラム名を1行目に書き込む
                    for (int i = 1; i <= columnCount; i++) {
                        sheet.getCellRange(1, i).setText(rsmd.getColumnName(i));
                    }

                    // データを書き込む(2行目以降)
                    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("データをExcelファイル「" + outputExcel + "」にエクスポートしました。");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

まとめ

本記事では、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?