excel-logo-new

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


P.S JExcel API – 2.6.12

でテスト済み

1. JExcelをダウンロードする

Mavenユーザー。

pom.xml

   <dependency>
         <groupId>net.sourceforge.jexcelapi</groupId>
         <artifactId>jxl</artifactId>
         <version>2.6.12</version>
   </dependency>

またはhttps://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/[この場所]から直接ダウンロードしてください。

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.mkyong;

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を作成するために使用できる多くの追加機能があります。この記事では、頭出しをしています。

より速く進むために参考文献の以下のリンクを参照してください。

参考文献

ダウンロードリンク]。

http://jexcelapi.sourceforge.net/resources/javadocs/2


6

10/docs/index.html[JExcel

関数とオブジェクト参照]。

https://www.teamdev.com/downloads/jexcel/docs/JExcel-PGuide.html

[Complete

POIライブラリ(JExcel APIの代わりに) – ダウンロード]。

http://poi.apache.org/spreadsheet/quick-guide.html

[Apache POI

Java – JXLまたはApache POIでExcelシートを読む方が優れたAPIです]