使用的pom.xml文件如下所示:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8-beta3</version>
</dependency>
注意这里的poi-ooxml版本,一定是3.8-beta3版本,因为3.8正式版还没有。其中使用的写excel操作与常规的写法是一样的。
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8-beta3</version>
</dependency>
注意这里的poi-ooxml版本,一定是3.8-beta3版本,因为3.8正式版还没有。其中使用的写excel操作与常规的写法是一样的。
java代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileOutputStream out = null;
try {
out = new FileOutputStream(new File("D://result.xlsx"));
Workbook writeWB = new SXSSFWorkbook(500); //读取500行后会缓到磁盘
org.apache.poi.ss.usermodel.Sheet writeSheet = writeWB.createSheet("1");
for (int i=0; i<1000000;i++) {
Row writeRow = writeSheet.createRow(i);
for (int j = 0; j < 10; j++) {
writeRow.createCell(j).setCellValue("中国");
}
}
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();
}
}
}