Skip to content
Simple sample to create large excel using POI.
Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.settings
src
.classpath
.gitignore
.project
LICENSE
README.md
pom.xml

README.md

target

2018-04-18 diary: [Java] 大きな Excel ブックを Apache POI で作成

いがぴょん画像(小) いがぴょんの日記に関連のあるコンテンツ。

[Java] 大きな Excel ブックを Apache POI で作成

大きな Excel ブックを Apache POI を用いて作成するシンプルなサンプルをメモします。

完全なソースコードは以下にあります。

ポイントとなるソースコードは以下。

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

        try (SXSSFWorkbook workbook = new SXSSFWorkbook(10)) {
            for (int indexSheet = 0; indexSheet < 4; indexSheet++) {
                final Sheet sheet = workbook.createSheet();

                for (int indexColumn = 0; indexColumn < 20; indexColumn++) {
                    sheet.setColumnWidth(indexColumn, 256 * 3);
                }

                for (int indexRow = 0; indexRow < 100; indexRow++) {
                    final Row row = sheet.createRow(indexRow);
                    for (int indexColumn = 0; indexColumn < 10; indexColumn++) {
                        final Cell cell = row.createCell(indexColumn);
                        cell.setCellValue("Data of (" + indexColumn + ":" + indexRow + ")");
                    }
                }
            }

            try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("./target/aout.xlsx"))) {
                workbook.write(out);
            }

            workbook.dispose();
        }
You can’t perform that action at this time.