public class ExportTest {
private final static Object object = new Object();
public static void main(String[] args) {
System.out.println("开始");
long startTime=System.currentTimeMillis();
int processor = Runtime.getRuntime().availableProcessors();
SXSSFWorkbook workBook = new SXSSFWorkbook();
CellStyle style = workBook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
ExecutorService executorService = new ThreadPoolExecutor(processor, processor, 1000,
TimeUnit.MILLISECONDS, new LinkedBlockingDeque(),new BasicThreadFactory.Builder()
.namingPattern("poi-task-%d")
.priority(Thread.MAX_PRIORITY)
.build());
CountDownLatch countDownLatch = new CountDownLatch(1);
for (int i = 0; i < 1; i++) {
int sheetId = i;
executorService.execute(new ExportThread(workBook, style, sheetId, countDownLatch));
}
try {
countDownLatch.await();
executorService.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
FileOutputStream fou = null;
try {
fou = new FileOutputStream("D:\\multiSheetNew.xlsx");
workBook.write(fou);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fou != null) {
try {
fou.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("总共耗时"+(System.currentTimeMillis()-startTime)/1000+"s");
}
}
class ExportThread implements Runnable{
private final static Object object = new Object();
SXSSFWorkbook workBook; CellStyle style; int sheetId; CountDownLatch countDownLatch;
public ExportThread(SXSSFWorkbook workBook, CellStyle style, int sheetId, CountDownLatch countDownLatch) {
this.workBook=workBook;
this.style=style;
this.sheetId=sheetId;
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
try {
SXSSFSheet hSSFSheet = null;
synchronized (object) {
hSSFSheet = (SXSSFSheet) workBook.createSheet();
}
SXSSFRow hssfRow = (SXSSFRow) hSSFSheet.createRow(0);
SXSSFCell hssfCell = (SXSSFCell) hssfRow.createCell(0);
hssfCell.setCellStyle(style);
hssfCell.setCellValue("第" + sheetId + "个sheet页,第一行,第一个单元格");
hssfCell = (SXSSFCell) hssfRow.createCell(1);
hssfCell.setCellStyle(style);
hssfCell.setCellValue("第" + sheetId + "个sheet页,第一行,第二个单元格");
hssfCell = (SXSSFCell) hssfRow.createCell(2);
hssfCell.setCellStyle(style);
hssfCell.setCellValue("第" + sheetId + "个sheet页,第一行,第三个单元格");
SXSSFRow hssfRows;
SXSSFCell hSSFCells;
for (int i = 1; i < 100000; i++) {
hssfRows = (SXSSFRow) hSSFSheet.createRow(i);
hSSFCells = (SXSSFCell) hssfRows.createCell(0);
hSSFCells.setCellStyle(style);
hSSFCells.setCellValue("第" + sheetId + "个sheet页,第" + (i + 1) + "行,第一个单元格");
hSSFCells = (SXSSFCell) hssfRows.createCell(1);
hSSFCells.setCellStyle(style);
hSSFCells.setCellValue("第" + sheetId + "个sheet页,第一个单元格");
hSSFCells = (SXSSFCell) hssfRows.createCell(2);
hSSFCells.setCellStyle(style);
hSSFCells.setCellValue("第" + sheetId + "个sheet页,第一个单元格");
hSSFCells = (SXSSFCell) hssfRows.createCell(3);
hSSFCells.setCellStyle(style);
hSSFCells.setCellValue("第" + sheetId + "个sheet页,第一个单元格");
}
} finally {
countDownLatch.countDown();
}
}