Excel2003版最大行數是65536行。Excel2007開始的版本最大行數是1048576行。
Excel2003的最大列數是256列,2007以上版本是16384列。
poi導出excel,不使用模板的
http://happyqing.iteye.com/blog/2075985
xls格式導出使用HSSFWorkbook,(這個暫時沒有好辦法)
xlsx格式導出以前使用XSSFWorkbook,可以使用新的SXSSFWorkbook(poi3.8+)
Workbook wb = new SXSSFWorkbook(1000); //大於1000行時會把之前的行寫入硬盤
Row,Cell還跟之前的一樣
官方樣例
http://poi.apache.org/spreadsheet/how-to.html#sxssf
- import junit.framework.Assert;
- 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.ss.usermodel.Workbook;
- import org.apache.poi.ss.util.CellReference;
- import org.apache.poi.xssf.streaming.SXSSFWorkbook;
- public static void main(String[] args) throws Throwable {
- SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
- Sheet sh = wb.createSheet();
- for(int rownum = 0; rownum < 1000; rownum++){
- Row row = sh.createRow(rownum);
- for(int cellnum = 0; cellnum < 10; cellnum++){
- Cell cell = row.createCell(cellnum);
- String address = new CellReference(cell).formatAsString();
- cell.setCellValue(address);
- }
- }
- // Rows with rownum < 900 are flushed and not accessible
- for(int rownum = 0; rownum < 900; rownum++){
- Assert.assertNull(sh.getRow(rownum));
- }
- // ther last 100 rows are still in memory
- for(int rownum = 900; rownum < 1000; rownum++){
- Assert.assertNotNull(sh.getRow(rownum));
- }
- FileOutputStream out = new FileOutputStream("/temp/sxssf.xlsx");
- wb.write(out);
- out.close();
- // dispose of temporary files backing this workbook on disk
- wb.dispose();
- }
值得注意的是SXSSFWorkbook只能寫不能讀。但是往往我們需要向一個Excel模版裏導出數據,這樣才更好提前定義裏面的格式和vba代碼。
這裏就需要使用SXSSFWorkbook的另外一個構造函數:
SXSSFWorkbook(XSSFWorkbook workbook)
Construct a workbook from a template.
通過XSSFWorkbook來讀取模版,然後用SXSSFWorkbook來設置樣式和寫數據,詳細使用就參考API吧。 http://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFWorkbook.html