Apache POIを簡単に使うライブラリpoiWrapper
POIについての全投稿は/tag/POIを参照されたい。
poiWrapperというライブラリを製作した。Apache POIを使用して簡単にXLS/XLSXファイルを作成するためのライブラリである。
https://gitlab.com/ysugimura/poiwrapper
Mavenリポジトリは以下になる。
https://ysugimura.github.io/maven/
詳細な説明は今後行うが、まずはサンプルを示す。
サンプルコード
import java.nio.file.*;
import com.gwtcenter.poi.*;
public class Consts {
static final Path FOLDER = Paths.get("C:\\Users\\admin\\Desktop");
static final Path getExcelFile(XType xType) {
return FOLDER.resolve("out." + xType.extension);
}
private static int tempNumber;
static final Path getTempFile() {
return FOLDER.resolve("temp-" + tempNumber++);
}
}
XLS/XLSX書き込み例
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
import com.gwtcenter.poi.*;
import com.gwtcenter.poi.write.*;
import com.gwtcenter.poi.write.XFmtStyle.*;
import com.gwtcenter.poi.write.impl.*;
public class WriteTest {
public static void main(String[] args) throws IOException {
XBookCreator.setTempFileSupplier(()->Consts.getTempFile());
create(XType.XLSX);
create(XType.XLS);
}
private static void create(XType xType) throws IOException {
try (XBook book = XBookCreator.create(xType)) {
XFont mincho = book.createFontBuilder().setName("MS 明朝").setSize(20).build();
XFont gothic = book.createFontBuilder().setName("MS ゴシック").setSize(18).build();
XCellStyle cellStyle = book.createCellStyle().setThinBorders().setFont(mincho);
StringStyle titleStyle = cellStyle.stringStyle(XFormat.GENERAL);
DateStyle dateStyle = cellStyle.dateStyle(book.dateFormat("yyyy/MM/dd"));
TimestampStyle timestampStyle = cellStyle.timestampStyle(book.timestampFormat("yyyy/MM/dd HH:mm:ss"));
IntStyle intStyle = cellStyle.intStyle(XFormat.THOUSANDS_INTEGER);
FloatStyle floatStyle = cellStyle.floatStyle(XFormat.THOUSANDS_FLOAT);
XSheet sheet = book.createSheet("シート1");
sheet.skip();
IntStream.range(0, 5).forEach(x->sheet.add(titleStyle, "列タイトル " + x));
sheet.setRowHeight(20);
sheet.nextRow();
IntStream.range(0, 10).forEach(y-> {
sheet.add(titleStyle, "行タイトル " + y).getStyle().setFont(gothic);
sheet.add(timestampStyle, new Date());
sheet.add(dateStyle, new Date());
XCell cell = sheet.add(titleStyle, "寿限無じゅげむ");
if (y == 5) cell.getStyle().setFont(gothic).setBgColor(XColor.AQUA);
sheet.add(intStyle, y * 12345);
sheet.add(floatStyle, y * 12345).getStyle().setFont(gothic);
sheet.setRowHeight(20);
sheet.nextRow();
});
sheet.setHeader(1, 1);
sheet.autoSizeAll();
book.write(Files.newOutputStream(Consts.getExcelFile(xType)));
}
}
}
XLS/XLSX読み込み例
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
import com.gwtcenter.poi.*;
import com.gwtcenter.poi.read.*;
import com.gwtcenter.poi.read.impl.*;
public class ReadTest {
public static void main(String[]args) throws IOException {
read(XType.XLSX);
read(XType.XLS);
}
private static void read(XType xType) throws IOException {
Path path = Consts.getExcelFile(xType);
System.out.println("Reading " + path);
try (XReader reader = XReaderCreator.create(xType, path)) {
String[][]cells = reader.read(0, new XToString());
Arrays.stream(cells).forEach(row->
System.out.println(Arrays.stream(row).collect(Collectors.joining(",")))
);
}
}
}
書き込み結果
XLS/XLSX共に同じである。以下のようなファイルが作成される(LibreOfficeによる)。
読み込み結果
XLS/XLSXを読み込んでみる。
Reading C:\Users\admin\Desktop\out.xlsx
,列タイトル 0,列タイトル 1,列タイトル 2,列タイトル 3,列タイトル 4,
行タイトル 0,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,0,0.00,
行タイトル 1,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,12345,12345.00,
行タイトル 2,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,24690,24690.00,
行タイトル 3,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,37035,37035.00,
行タイトル 4,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,49380,49380.00,
行タイトル 5,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,61725,61725.00,
行タイトル 6,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,74070,74070.00,
行タイトル 7,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,86415,86415.00,
行タイトル 8,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,98760,98760.00,
行タイトル 9,2019/02/05 18:37:33,2019/02/05,寿限無じゅげむ,111105,111105.00,
Reading C:\Users\admin\Desktop\out.xls
,列タイトル 0,列タイトル 1,列タイトル 2,列タイトル 3,列タイトル 4,
行タイトル 0,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,0,0.00,
行タイトル 1,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,12345,12345.00,
行タイトル 2,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,24690,24690.00,
行タイトル 3,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,37035,37035.00,
行タイトル 4,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,49380,49380.00,
行タイトル 5,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,61725,61725.00,
行タイトル 6,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,74070,74070.00,
行タイトル 7,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,86415,86415.00,
行タイトル 8,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,98760,98760.00,
行タイトル 9,2019/02/05 18:37:34,2019/02/05,寿限無じゅげむ,111105,111105.00,
ディスカッション
コメント一覧
まだ、コメントがありません