0

私は Wicket を使用しています (問題があるかどうかはわかりません) が、Workbook を使用して、ユーザーがダウンロードする 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(); 
   }
  }
4

3 に答える 3

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 はその場でファイルを作成するため、どこにも保存する必要はありません。ファイルはダウンロードされると削除されます。

これが誰かを助けることを願っています!

于 2013-10-16T17:53:46.777 に答える