JExcel API - JavaでExcelファイルを読み書きする

JExcel API – JavaでExcelファイルを読み書きする

excel-logo-new

この記事では、JExcel APIを使用してExcelファイルを読み書きする方法について説明します。JExcelAPIは、高レベルの書式設定や複雑な数式ベースの操作を伴わない単純な操作に広く使用されるシンプルなライブラリです。

P.S Tested with JExcel API – 2.6.12

1. JExcelをダウンロード

Mavenユーザー。

pom.xml

   
         net.sourceforge.jexcelapi
         jxl
         2.6.12
   

または、this locationから直接ダウンロードしてください

2. Excelファイルを書き込む

Excelファイルの作成方法とデータの書き込み方法を示すJExcel APIの例。

ExcelWrite.java

package com.techfou;

import jxl.Workbook;
import jxl.write.*;
import jxl.write.Number;

import java.io.File;
import java.io.IOException;

public class ExcelWrite {

    private static final String EXCEL_FILE_LOCATION = "C:\\temp\\MyFirstExcel.xls";

    public static void main(String[] args) {

        //1. Create an Excel file
        WritableWorkbook myFirstWbook = null;
        try {

            myFirstWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION));

            // create an Excel sheet
            WritableSheet excelSheet = myFirstWbook.createSheet("Sheet 1", 0);

            // add something into the Excel sheet
            Label label = new Label(0, 0, "Test Count");
            excelSheet.addCell(label);

            Number number = new Number(0, 1, 1);
            excelSheet.addCell(number);

            label = new Label(1, 0, "Result");
            excelSheet.addCell(label);

            label = new Label(1, 1, "Passed");
            excelSheet.addCell(label);

            number = new Number(0, 2, 2);
            excelSheet.addCell(number);

            label = new Label(1, 2, "Passed 2");
            excelSheet.addCell(label);

            myFirstWbook.write();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        } finally {

            if (myFirstWbook != null) {
                try {
                    myFirstWbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }


        }

    }

}

出力、Excelファイルは次の内容で作成されます:

jexcel-write-excel

3. Excelファイルを読む

上記のExcelファイルを読み取る例。

ExcelRead.java

package com.techfou;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.io.File;
import java.io.IOException;

public class ExcelRead {

    private static final String EXCEL_FILE_LOCATION = "C:\\temp\\MyFirstExcel.xls";

    public static void main(String[] args) {

        Workbook workbook = null;
        try {

            workbook = Workbook.getWorkbook(new File(EXCEL_FILE_LOCATION));

            Sheet sheet = workbook.getSheet(0);
            Cell cell1 = sheet.getCell(0, 0);
            System.out.print(cell1.getContents() + ":");    // Test Count + :
            Cell cell2 = sheet.getCell(0, 1);
            System.out.println(cell2.getContents());        // 1

            Cell cell3 = sheet.getCell(1, 0);
            System.out.print(cell3.getContents() + ":");    // Result + :
            Cell cell4 = sheet.getCell(1, 1);
            System.out.println(cell4.getContents());        // Passed

            System.out.print(cell1.getContents() + ":");    // Test Count + :
            cell2 = sheet.getCell(0, 2);
            System.out.println(cell2.getContents());        // 2

            System.out.print(cell3.getContents() + ":");    // Result + :
            cell4 = sheet.getCell(1, 2);
            System.out.println(cell4.getContents());        // Passed 2

        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } finally {

            if (workbook != null) {
                workbook.close();
            }

        }

    }

}

上記のコードはほとんど自己理解可能です。 各セルまたはシートは、Javaのオブジェクトとしてマップされます。 上記のコードでは、JExcel jarを使用してワークシートを作成しました。 コードを実行すると、次のように出力が取得されます。

Test Count:1
Result:Passed
Test Count:2
Result:Passed 2

4. Excelファイルに書式を追加する

いくつかの書式設定を追加することで、例をさらに強化できます。 書式設定を追加する短いコードを以下に示します。

ExcelFormat .java

package com.example;

import jxl.Workbook;
import jxl.write.*;
import jxl.write.Number;

import java.io.File;
import java.io.IOException;

public class ExcelFormat {

    private static final String EXCEL_FILE_LOCATION = "C:\\temp\\MyFormattedExcel.xls";

    public static void main(String[] args) {

        //1. Create an Excel file
        WritableWorkbook mySecondWbook = null;
        try {

            mySecondWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION));
            WritableSheet myFirstSheet = mySecondWbook.createSheet("Sheet 1", 0);

            WritableCellFormat cFormat = new WritableCellFormat();
            WritableFont font = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD);
            cFormat.setFont(font);
            Label label = new Label(0, 0, "Test Count", cFormat);
            myFirstSheet.addCell(label);
            Number number = new Number(0, 1, 1);
            myFirstSheet.addCell(number);

            label = new Label(1, 0, "Result", cFormat);
            myFirstSheet.addCell(label);
            label = new Label(1, 1, "Passed");
            myFirstSheet.addCell(label);

            number = new Number(0, 2, 2);
            myFirstSheet.addCell(number);

            label = new Label(1, 2, "Passed 2");
            myFirstSheet.addCell(label);

            mySecondWbook.write();

        } catch (WriteException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (mySecondWbook != null) {
                try {
                    mySecondWbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }


        }


    }

}

出力

jexcel-format-excel

コードはヘッダーをArial、16px、Boldフォントにフォーマットします。 他にも、サポートされているフォントとサイズがいくつかあります。 JExcelには、さらに洗練され、適切にフォーマットされたExcelを作成するために使用できる多くの追加機能があります。 この記事はあなたに有利なスタートを与えます。 速度を上げるには、リファレンスの次のリンクを参照してください。