LoginSignup
4
5

More than 5 years have passed since last update.

JavaでExcelファイルを作成する

Last updated at Posted at 2016-01-28

JavaでExcelファイルを作成した時のメモ。
Apache-poiで使うクラスがExcelのバージョンによって異なる。

「Excel 2003」まで:HSSFWorkbook
「Excel 2007」以降:XSSFWorkbook


import java.io.FileOutputStream;
import java.io.IOException;

//import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Sample{
  public static void main(String[] args){
      //HSSFWorkbook workbook = new HSSFWorkbook();
      XSSFWorkbook workbook = new XSSFWorkbook();

      workbook.createSheet("test");

      FileOutputStream out = null;
      try{
          //out = new FileOutputStream("sample.xls");
          out = new FileOutputStream("sample.xlsx");
          workbook.write(out);
      }catch(IOException e){
          System.out.println(e.toString());
      }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }
}
4
5
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
4
5