6

ユーザーが apache poi 経由でダウンロードする Excel ファイルを作成することを目指しています。

サーブレットに次のコードがあります。

protected void doGet(HttpServletRequest request, 
              HttpServletResponse response) throws ServletException, IOException 
      {

            // create a workbook , worksheet
            Workbook wb = new HSSFWorkbook();
            Sheet sheet = wb.createSheet("MySheet");
            CreationHelper createHelper = wb.getCreationHelper();

            // Create a row and put some cells in it. Rows are 0 based.
            Row row = sheet.createRow((short)0);
            Cell cell = row.createCell(0);
            cell.setCellValue(1);
            row.createCell(1).setCellValue(1.2);
            row.createCell(2).setCellValue( createHelper.createRichTextString("This is a string") );
            row.createCell(3).setCellValue(true);

            //write workbook to outputstream
            ServletOutputStream out = response.getOutputStream();
            wb.write(out);
            out.flush();
            out.close();

            //offer the user the option of opening or downloading the resulting Excel file
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls");

問題は、これらの奇妙な値を取得していることです:

「…<em>MySheetŒ®üÿ » ÌÁ w dü©ñÒMbP?*+‚€%ÿÁƒ„」d,,à?à?

助言がありますか?

4

2 に答える 2

7

何が間違っているかを見つけました。HttpServletResponse を最初に設定する必要があります。

//offer the user the option of opening or downloading the resulting Excel file
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=MyExcel.xls");
于 2012-06-28T05:52:51.337 に答える
1

各セルのセル タイプを設定する必要があります。次を使用して設定できます。

cell.setCellType(Cell.CELL_TYPE_STRING );

より多くの細胞タイプについては、ここでapi を確認してください。

または、同じためにDataFormatterを使用できます。

于 2012-06-28T05:56:51.577 に答える