POi写入大批量数据

时间:2023-12-04 10:00:08

直接贴代码:

package jp.co.misumi.mdm.batch.common.jobrunner;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Demo16
{ public static void main(String[] args) throws InvalidFormatException
{
FileOutputStream out = null;
OPCPackage pkg;
// TODO Auto-generated method stub XSSFWorkbook wbTemplate;
try
{
pkg = OPCPackage.open(new File("F:/code/mdm-batch/target/classes/template/excel/TP.xlsx"));
wbTemplate = new XSSFWorkbook(pkg);
SXSSFWorkbook writeWB = new SXSSFWorkbook(wbTemplate, , true, true);
SXSSFSheet writeSheet = writeWB.getSheet("ProductMST");
// Workbook writeWB = new SXSSFWorkbook(500);
//org.apache.poi.ss.usermodel.Sheet writeSheet = writeWB.createSheet("1");
System.out.println(String.valueOf(new Date()));
try
{
Thread.sleep();
}
catch (Exception e)
{
// TODO: handle exception
}
for (int i=; i<;i++) {
System.out.println(String.valueOf(i));
Row writeRow = writeSheet.createRow(i);
for (int j = ; j < ; j++) {
Cell cell = writeRow.createCell(j);
//cell.setCellValue("Hello");
cell.setCellValue(j+"Hello"+i);
cell.setCellValue(j+"Hello");
/* CellStyle cellStyle = cell.getCellStyle();
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cell.setCellStyle(cellStyle);*/
}
}
System.out.println(String.valueOf(new Date()));
File file = new File("F:/code/mdm-batch/target/classes/template/excel/6666666666666555.xlsx");
out = new FileOutputStream(file);
writeWB.write(out);
out.flush();
out.close();
System.out.println("测试成功!");
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }