6

Java アプリケーションで XLSX ファイルのダウンロードを有効にするのに問題があります。

このリンクに表示されている例に従って:ユーザーが Apache POI を使用してダウンロードするための Excel ファイルを作成します。スプレッドシートをダウンロード/保存するために 2 つの構成を試しました。

最初に .XLS ファイルを使用します。

response.setContentType("application/ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=testxls.xls");

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("Some text");

ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);

byte[] outArray = outByteStream.toByteArray();
OutputStream outStream = response.getOutputStream();
outStream.write(outArray);
outStream.flush();

これは機能します。

次に、XLSXファイルで試しました:

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=testxls.xlsx");

XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Some text");

ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);

byte[] outArray = outByteStream.toByteArray();
OutputStream outStream = response.getOutputStream();
outStream.write(outArray);
outStream.flush();

これを試してみると、次のメッセージが表示されます

このメッセージにもかかわらず、スプレッドシートは正常に開きますが、本当にこのメッセージを削除したいです。

何か案は?

4

2 に答える 2