[英]How to read 1 million records of rows in excel using java
任何人都可以幫助提供代碼段以使用Java從excel讀取100萬條記錄
我們有XSSF apache poi,但是它無法讀取500 k記錄,因為它試圖一次讀取整個文件,
拋出“超出GC超出限制”錯誤
我在互聯網上搜索,但我得到的示例僅是使用Java將100萬條記錄的數據寫入excel
當然,您可以使用java從excel讀取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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.