2013-10-15 54 views
0

我正在使用Wicket(不知道它是否重要),但我正在使用工作簿为用户下载创建一个Excel文件。但我不确定究竟该如何做到这一点。我想要发生的是用户点击按钮,创建一个日志,并提示用户打开(并保存到临时文件)或保存到他们的计算机。然后将文件从服务器端删除,或者将其存储在用户会话中并在会话结束时删除。如何从Web应用程序下载Excel文件?

有人能指出我正确的方向吗?如果我能有未保存在会话中的所有文件,那将会创造和拥有它只是它使用FileOutputStream中以某种方式发送到客户端..

这里是我当前的代码:

private void excelCreator() 
{ 
    Workbook workbook = new HSSFWorkbook(); 

    Sheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName("SSA User ID " + currentSSAIDSelection2.getSsaUserId())); 

    Iterator<AuditLogEntry> auditLogEntrys = logList.iterator(); 

    int i = 0; 
    while (auditLogEntrys.hasNext()) 
    { 
    final AuditLogEntry auditLogEntry = auditLogEntrys.next(); 

    Row row = sheet.createRow(i); 

    row.createCell(0).setCellValue(auditLogEntry.getTimeStamp()); 
    row.createCell(1).setCellValue(auditLogEntry.getSourceName()); 
    row.createCell(2).setCellValue(auditLogEntry.getCategory()); 
    row.createCell(3).setCellValue(auditLogEntry.getSsaAdmin()); 
    row.createCell(4).setCellValue(auditLogEntry.getAction()); 

    i++; 
    } 

    try 
    { 
     FileOutputStream output = new FileOutputStream("ssaUserIDAccess.xls"); 
     workbook.write(output); 
     output.close(); 

    }catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    } 
+0

没关系,我发现这个链接 http://stackoverflow.com/questions/7646270/how-to-use-wickets-downloadlink-with-a-file-generated-on-the-fly – eaglei22

回答

0

您必须创建一个带有临时文件的DownloadLink作为输入。下载后必须删除临时文件(file.delete()))。

或者,也可以尝试:

IResourceStream stream = new ByteArrayResourceStream(data, "application/vnd.ms-excel"); 
RequestCycle.get().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(stream, filename).setContentDisposition(ContentDisposition.ATTACHMENT)); 

在这种情况下数据是工作簿的字节[]的内容可以是例如用output.toByteArray检索()。

+0

感谢您的回答。我实际上找到了一个解决方案,我现在要发布它。 – eaglei22

0

如果有人遇到这个问题,这里是我的解决方案。在这方面没有很多简单的答案,但这是我的解决方案:

我的excelCreator方法处理Excel表单的创建,并将其作为文件返回。

private File excelCreator() 
    { 
    Workbook workbook = new HSSFWorkbook(); 
    File excelfile = new File("userIDAccess.xls"); 
    logList = getServer().findAuditLogs(getUserId(), null); 
    Sheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName("User ID " + getUserId())); 

    Iterator<AuditLogEntry> auditLogEntrys = logList.iterator(); 

    int i = 0; 
    while (auditLogEntrys.hasNext()) 
    { 
    final AuditLogEntry auditLogEntry = auditLogEntrys.next(); 

    Row row = sheet.createRow(i); 

    row.createCell(0).setCellValue(auditLogEntry.getTimeStamp()); 
    row.createCell(1).setCellValue(auditLogEntry.getSourceName()); 
    row.createCell(2).setCellValue(auditLogEntry.getCategory()); 
    row.createCell(3).setCellValue(auditLogEntry.getSsaAdmin()); 
    row.createCell(4).setCellValue(auditLogEntry.getAction()); 

    i++; 
    } 

    try 
    { 

     FileOutputStream output = new FileOutputStream(excelfile); 
     workbook.write(output); 
     output.close(); 


    }catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return excelfile; 
} 

    IModel excelFileModel = new AbstractReadOnlyModel() 
     { 
     public Object getObject() 
       { 
      return excelCreator(); 
     } 
    }; 

我创建了一个IModel来捕获在我的excelCreator()方法中创建并返回的文件。

 auditDownloadlink = new DownloadLink("auditDownloadlink", excelFileModel); 

     I pass the I.D. of the download link, and then pass the imodel. 

finally, 

我打电话,

auditDownloadlink.setDeleteAfterDownload(true); 
    auditDownloadlink.setCacheDuration(Duration.NONE); 

这将删除该文件创建后。缓存设置是一个设置,以确保它与所有浏览器兼容(这就是我对它的解释,但您可能不需要它)。

Imodel即时创建文件,因此不必将其存储在任何位置,然后一旦下载文件就会将其删除。

希望这可以帮助别人!

0

您可以创建一个Resource来执行此操作,并创建一个ResourceLink

public class ExcelProducerResource extends AbstractResource 
{ 
    public ExcelProducerResource() 
    { 

    } 

    @Override 
    protected ResourceResponse newResourceResponse(Attributes attributes) 
    { 

     final String fileName = getFileName(); 

     ResourceResponse resourceResponse = new ResourceResponse(); 
     resourceResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
     resourceResponse.setCacheDuration(Duration.NONE); 
     resourceResponse.setFileName(fileName); 
     resourceResponse.setWriteCallback(new WriteCallback() 
     { 
      @Override 
      public void writeData(Attributes attributes) throws IOException 
      { 
       OutputStream outputStream = attributes.getResponse().getOutputStream(); 

       writeToStream(outputStream); 
       outputStream.close(); 
      } 
     }); 
     return resourceResponse; 
    } 

    void writeToStream(OutputStream outputStream) throws IOException 
    { 
     //.. do stuff here :) 
    } 
    String getFileName() 
    { 
     //.. do stuff here :) 
    } 

} 
相关问题