`
lbxhappy
  • 浏览: 302930 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

导出excel2007 poi3.8

阅读更多
poi3.8默认情况下,用到的临时目录是系统自带的临时目录,在本次项目中遇到临时目录超限,所以需要修改源代码指定需要设置的临时目录
需要修改的类为:GZIPSheetDataWriter,SheetDataWriter
  GZIPSheetDataWriter压缩时候用到,如在构造workbook时:
wokbook = new SXSSFWorkbook(new XSSFWorkbook(
new FileInputStream(FanExcel2007.TEMPLATE_PATH)), 2000,true);
false代表不压缩,默认为False用SheetDataWriter

GZIPSheetDataWriter修改:
     /**
     * @return temp file to write sheet data
     */
    public File createTempFile()throws IOException {
// 获取tmp目录
String tmpDir = null;
File fd = null;
File file = null;
try {
// tmpDir = FileUtils.readFileToString(new File(SheetDataWriter.class
// .getResource("/tmpPath.txt").getFile()), "utf8");
tmpDir="/home/hdfs/effect/liubx/tmp";
System.out.println("临时缓存目录为:" + tmpDir);
if (tmpDir != null && !tmpDir.equals("")) {
file = new File(tmpDir);
if (!file.exists()) {
file.mkdirs();
}
}

} catch (Exception ex) {
System.out.println("指定缓存目录失败:" + tmpDir);
tmpDir = null;
}
if (tmpDir == null || tmpDir.equals("")) {
fd = File.createTempFile("poi-sxssf-sheet-xml", ".gz");
} else {
fd = File.createTempFile("poi-sxssf-sheet-xml", ".gz",file);
}

      
        fd.deleteOnExit();
        return fd;
    }
SheetDataWriter修改处:
    /**
     * Create a temp file to write sheet data.
     * By default, temp files are created in the default temporary-file directory
     * with a prefix "poi-sxssf-sheet" and suffix ".xml".  Subclasses can override
     * it and specify a different temp directory or filename or suffix, e.g. <code>.gz</code>
     *
     * @return temp file to write sheet data
     */
    public File createTempFile()throws IOException {
    // 获取tmp目录
    String tmpDir = null;
    File fd = null;
    File file = null;
    try {
    tmpDir="/home/hdfs/effect/liubx/tmp";
    System.out.println("临时缓存目录为:" + tmpDir);
    if (tmpDir != null && !tmpDir.equals("")) {
    file = new File(tmpDir);
    if (!file.exists()) {
    file.mkdirs();
    }
    }

    } catch (Exception ex) {
    System.out.println("指定缓存目录失败:" + tmpDir);
    tmpDir = null;
    }
    if (tmpDir == null || tmpDir.equals("")) {
    fd = File.createTempFile("poi-sxssf-sheet", ".xml");
    } else {
    fd = File.createTempFile("poi-sxssf-sheet", ".xml",file);
    }
      
        fd.deleteOnExit();
        return fd;
    }
这样可以写超大数据,在写超大数据时最好用压缩的方式,写文件比较快!
分享到:
评论
3 楼 ae6623 2013-09-18  
ae6623 写道
大哥,你是怎么知道它对临时文件有限制的,我也发现这个bug了,在WinNT系统里面也是无法创建临时文件的,createTempFile的时候,IO异常。按照楼主的办法修改上面两个类之后,还有一个类需要修改。org.apache.poi.xssf.streaming.SXSSFWorkbook中第754行 public void write(OutputStream stream) throws IOException里面。我想问一下,大哥。这个你是怎么知道的。我qq 394263788 非常想知道您的这个答案。O(∩_∩)O谢谢


额,我今天找到原因了。是因为我的Tomcat里面我以前手贱,把temp这个文件夹给删掉了。现在重新加上了之后一切ok。
2 楼 ae6623 2013-09-17  
大哥,你是怎么知道它对临时文件有限制的,我也发现这个bug了,在WinNT系统里面也是无法创建临时文件的,createTempFile的时候,IO异常。按照楼主的办法修改上面两个类之后,还有一个类需要修改。org.apache.poi.xssf.streaming.SXSSFWorkbook中第754行 public void write(OutputStream stream) throws IOException里面。我想问一下,大哥。这个你是怎么知道的。我qq 394263788 非常想知道您的这个答案。O(∩_∩)O谢谢
1 楼 qingyezhu 2012-09-23  
请问,用poi3.8中的wordtohtmlconver类将doc转化为html,其中doc中含有表格(表格很宽)时,在网页中显示不正常,怎么解决呀?谢谢!

相关推荐

Global site tag (gtag.js) - Google Analytics