如何使用java读取excel中的100万行记录

时间:2018-04-10 07:57:41

标签: java excel apache-poi streaming spring-batch

任何人都可以帮助提供一个代码片段,以便使用java从e​​xcel读取100万条记录

我们有XSSF apache poi,但它无法读取500 k记录,因为它试图一次读取整个文件,

" GC超出限制"错误被抛出

我在互联网上搜索但是我只是通过使用java将100万条记录的数据写入excel的示例

1 个答案:

答案 0 :(得分:1)

可以肯定的是,您可以使用java从e​​xcel读取100万条记录,但是您需要非常大的计算机内存。如果通过eclipse运行应用程序,则需要很大的内存。实际上,我通过java的eclipse读取了181234条记录,它需要11G以上的内存,因此,如果您想读取100万条记录,请考虑一下所需的内存...

下面是测试代码。

public class Test {
public static void main(String[] args) throws Exception {

    File file = new File("C:\\opt\\upload\\1.xlsx");
    InputStream in = new FileInputStream(file);
    XSSFWorkbook xssfWorkbook = getXSSFWorkbook(in);
    XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
    XSSFRow row = xssfSheet.getRow(0);
    long time = System.currentTimeMillis();

    NetworkParas bean = new NetworkParas();
    bean.setCellId(Double.valueOf(row.getCell(0) == null ? null : row.getCell(0).toString()).intValue());
    bean.setLac(Double.valueOf(row.getCell(1) == null ? null : row.getCell(1).toString()).intValue());
    bean.setLongitude(Double.valueOf(row.getCell(2) == null ? null : row.getCell(2).toString()));
    bean.setLatitude(Double.valueOf(row.getCell(3) == null ? null : row.getCell(3).toString()));
    bean.setAntAzimuth(Double.valueOf(row.getCell(4) == null ? null : row.getCell(4).toString()).intValue());

    bean.setRat(row.getCell(5) == null ? null : row.getCell(5).toString());
    bean.setCity(row.getCell(6) == null ? null : row.getCell(6).toString());
    bean.setCell(row.getCell(7) == null ? null : row.getCell(7).toString());
    bean.setCellName(row.getCell(8) == null ? null : row.getCell(8).toString());
    bean.setLocation(row.getCell(9) == null ? null : row.getCell(9).toString());

    bean.setCellType(row.getCell(10) == null ? null : row.getCell(10).toString());
    bean.setSiteId(row.getCell(11) == null ? null : row.getCell(11).toString());
    bean.setSiteType(row.getCell(12) == null ? null : row.getCell(12).toString());
    bean.setConf(row.getCell(13) == null ? null : row.getCell(13).toString());
    bean.setAntHeight(Double.valueOf(row.getCell(14) == null ? null : row.getCell(14).toString()));

    bean.setMtilt(Double.valueOf(row.getCell(15) == null ? null : row.getCell(15).toString()));
    bean.setEtilt(Double.valueOf(row.getCell(16) == null ? null : row.getCell(16).toString()));
    bean.setNe(row.getCell(17) == null ? null : row.getCell(17).toString());
    bean.setArfcnDl(Double.valueOf(row.getCell(18) == null ? null : row.getCell(18).toString()).intValue());
    bean.setScramblingCode(Double.valueOf(row.getCell(19) == null ? null : row.getCell(19).toString()).intValue());

    bean.setCpichPower(Double.valueOf(row.getCell(20) == null ? null : row.getCell(20).toString()));
    bean.setEnbId(Double.valueOf(row.getCell(22) == null ? null : row.getCell(22).toString()).intValue());


    bean.setEci(Integer.valueOf(row.getCell(23) == null ? null : row.getCell(23).getCTCell().getV()));
    bean.setLaccellId(row.getCell(26) == null ? null : row.getCell(26).getCTCell().getV());

    System.out.println(bean);

    time = System.currentTimeMillis() - time;
    System.out.println("Elapsed Time :" + time);

}

public static XSSFWorkbook getXSSFWorkbook(InputStream in) throws IOException {
    long time = System.currentTimeMillis();
    XSSFWorkbook xssfWorkbook = new XSSFWorkbook(in);
    System.out.println("\n*** Elapsed Time: " + ((System.currentTimeMillis()-time)/1000) + "s ");
    return xssfWorkbook;
}
相关问题