首页 文章

在使用POI编写大型电子表格时,为什么超链接会停止工作?

提问于
浏览
1

我正在使用POI 3.9和SXSSF来创建电子表格 . 当行数较少时,创建超链接会起作用 . 但是,随着大小的增加,处理时间大大增加,链接也不起作用 . 它适用于700行但不是70,000行 . 尝试打开文件时,会发生此错误:

Excel在'out.xlsx'中找到了不可读的内容 . 你想恢复这个工作簿的内容吗?如果您信任此工作簿的来源,请单击“是” .

public static void main(String[] args) throws Throwable {
    Workbook 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 < 70000; 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);
            if (address.contains("A"))
                setHyperlink(cell, address);
        }
    }

    FileOutputStream out = new FileOutputStream("C:/temp/sxssf.xlsx");
    wb.write(out);
    out.close();
}

protected static void setHyperlink(Cell cell, String address) {
    Workbook workbook = cell.getSheet().getWorkbook();
    Hyperlink hyperlink = workbook.getCreationHelper()
            .createHyperlink(Hyperlink.LINK_URL);
    String encoded = null;
    try {
        encoded = URLEncoder.encode(address, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    hyperlink.setAddress(encoded);
    cell.setHyperlink(hyperlink);
}

1 回答

相关问题