qq_27795789
qq_27795789
采纳率0%
2016-06-12 14:23 阅读 5.0k

怎么使用java Poi解决导入excel表格大数据量时的内存溢出问???大神们,帮帮我。

public String admin_product_list() {
// System.out.println("myFileFileName "+myFileFileName);
String path = "";
String name = "";
String realpath = "";
String afterfilename = "";
drlogbean = new Daorulog();
int daorutiaoshu = 0;
String oldfilename = "";
int rowaccess=2;
try {
if (myFileFileName != null && !myFileFileName.equals("")) {

            oldfilename = myFileFileName;
            String b[] = oldfilename.split("\\.");
            // 设置上传文件目录

            String filePath = ServletActionContext.getServletContext().getRealPath("/upload");
            // 基于myFile创建文件输入流
            InputStream is = new FileInputStream(myFile);
            String a[] = myFileFileName.split("\\.");
            // System.out.println("上传文件名:"+a[0]+"上传文件类型:"+a[1]);
            // 设置目标文件
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            Date date = new Date();
            String strdf = df.format(date).toString();
            name = "upload/" + b[0] + "_" + strdf + "." + a[1];
            File toFile = new File(filePath, b[0] + "_" + df.format(date).toString() + "." + a[1]);
            path = filePath + "\\" + name;
            // System.out.println(path);
            // 创建一个输出流
            OutputStream os = new FileOutputStream(toFile);
            // OutputStream bos = new
            // FileOutputStream(filePath);//建立一个上传文件的输出流
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);// 将文件写入服务器
            }
            realpath = filePath + "\\" + b[0] + "_" + strdf + "." + a[1];
            afterfilename = b[0] + "_" + strdf + "." + a[1];
            System.out.println("realpath " + realpath);
            System.out.println("sourseTypeName " + sourseTypeName);
            InputStream is2 = new FileInputStream(realpath);
            fileRealName = realpath;
            SXSSWorkbook workbook =new SXSSFWorkbook(rowaccess);
            Sheet sheet = workbook.getSheetAt(0);
            Row row = sheet.getRow(headerRow);
            int lastCellNum = row.getLastCellNum();
            for (int i = 0; i < lastCellNum; i++) {
                if (row.getCell(i) == null || row.getCell(i).toString() == null)
                    continue;
                cellMap.put(i, row.getCell(i).toString());
            }
            //每当行数达到设置的值就刷新数据到硬盘,以清理内存
            if(i/rowaccess==0){
                System.out.println("清除缓存重新输入");
                //sheet.flushRows();
                ((SXSSFSheet)sh).flushRows();
            }
            dwzdlist = productDAO.findAlldanweizidianList();

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    malist = productDAO.findAlldanweiList();

    return "importStep2";
}

/**
 * 单位码表类型
 * 
 * @return
 */
public String admin_find_danweizidian() {
    return "danweizidian";
}

// =============================2016-05-12 end
public String importPreview() {
    int introwaccess=2;//内存中缓存记录行数
    //System.out.println(kuozhan1);
    try {
        InputStream is2 = new FileInputStream(fileRealName);
         SXSSFWorkbook workbook = new SXSSFWorkbook(introwaccess);
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        List<ResourceImport> riList = new ArrayList<ResourceImport>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = firstDataRow; i < lastRowNum+1; i++) {
            Row row = sheet.getRow(i);
            ResourceImport ri = new ResourceImport();

            // 非标准的详细地址
            String nsAddress = "";
            if (row.getCell(xiangxiaddress) != null) {
                nsAddress = row.getCell(xiangxiaddress).toString();
                ri.setYehuxiangxiaddress(nsAddress);
            }

            String nsAddressClone = nsAddress;
            ri.setLeibie(leibie.split(",")[0]);
            if (row.getCell(name) != null) {
                String nameR = row.getCell(name).toString();
                if (nameR.contains("\n")) {
                    String[] nameRA = nameR.split("\n");
                    StringBuffer sb = new StringBuffer();
                    for (int k = 0; k < nameRA.length; k++) {
                        if (k != nameRA.length - 1) {
                            sb.append(nameRA[k]);
                            sb.append(",");
                        } else {
                            sb.append(nameRA[k]);
                        }
                    }
                    ri.setYehuname(sb.toString());
                } else {
                    ri.setYehuname(nameR);
                }
            }
            if (row.getCell(tel) != null) {
                Cell cell = row.getCell(tel);
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {//poi导入,转换成String类型
                    DecimalFormat df = new DecimalFormat("0");//格式化实例化数字
                    ri.setYehumobilephone(df.format(cell.getNumericCellValue()));
                } else {
                    ri.setYehumobilephone(cell.toString());
                }
            }
            if (row.getCell(sex) != null)
                ri.setYehusex(row.getCell(sex).toString());
            if (row.getCell(age) != null)
                ri.setYehuage(row.getCell(age).toString());
            if (row.getCell(dateofbirth) != null)
                ri.setYehudateofbirth(row.getCell(dateofbirth).toString());
            ri.setYehuzhenjiantype(zhenjiantype);
            if (row.getCell(zhengjiancode) != null)
                ri.setYehuzhengjiancode(row.getCell(zhengjiancode).toString());



            if (row.getCell(yehujinjilinkman) != null)
                ri.setYehujinjilinkman(row.getCell(yehujinjilinkman).toString());

            if (row.getCell(yehujinjilinkmanphone) != null) {
                Cell cell = row.getCell(yehujinjilinkmanphone);
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {//poi导入,转换成String类型
                    DecimalFormat df = new DecimalFormat("0");//格式化实例化数字
                    ri.setYehujinjilinkmanphone(df.format(cell.getNumericCellValue()));
                } else {
                    ri.setYehujinjilinkmanphone(cell.toString());
                }
            }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • zhoujiaping123 the-13th-floor 2016-06-13 14:03

    别用POI,先用数据库,将excle导入数据库,再用jdbc从数据库读。思路是这样,自己也没试过,你可以试一下。

    点赞 评论 复制链接分享

相关推荐