データSXSSFWorkbook500,000をローカルExcelファイルにエクスポートします(簡単な実装例)



Export Data Sxssfworkbook 500



コード上、ナンセンスではありません:

import org.apache.poi.ss.usermodel.Cell import org.apache.poi.ss.usermodel.Row import org.apache.poi.ss.util.CellReference import org.apache.poi.xssf.streaming.SXSSFSheet import org.apache.poi.xssf.streaming.SXSSFWorkbook import org.apache.poi.xssf.usermodel.XSSFWorkbook import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException public class BigDataExcelUtils { public static void writeBigData() throws IOException { long startTime = System.currentTimeMillis () // acquisition start time, in milliseconds FileInputStream inputStream = new FileInputStream('C:\temp\template.xls') XSSFWorkbook wb_template = new XSSFWorkbook(inputStream) inputStream.close() SXSSFWorkbook wb = new SXSSFWorkbook(wb_template) wb.setCompressTempFiles(true) SXSSFSheet sh = (SXSSFSheet) wb.getSheetAt(0) sh.setRandomAccessWindowSize (100) // keep 100 rows in memory, exceeding rows will be flushed to disk, each loaded into memory 100 for (int rowNum = 0 rowNum <500000 rowNum ++) {// 500000 is the number of rows Row row = sh.createRow(rowNum) for (int cellNum = 0 cellNum <10 cellNum ++) {// 10 Number of columns Cell cell = row.createCell(cellNum) String address = new CellReference(cell).formatAsString() cell.setCellValue(address) } } FileOutputStream outFile = new FileOutputStream('C:\temp\tempsxssf.xlsx') wb.write(outFile) outFile.close() wb.dispose()// dispose of temporary files backing this workbook on disk long endTime = System.currentTimeMillis () // acquisition start time, in milliseconds long duration = endTime - startTime System.out.println ( 'Long run time:' + Double.valueOf (duration) / 1000 + 's') } public static void main(String[] args) throws Throwable { writeBigData() } }

約25秒の実行時間。もちろん、実際のビジネスプロセスに時間がかかる場所は他にもあります。