0

私はExcelドキュメントをエクスポートするためのこの方法を持っていますが、Excelを開くと、この種のファイルに奇妙な文字や記号が含まれ、フォーマットされていません。誰かがこれを修正する方法を教えてもらえますか?

ここに画像の説明を入力してください

これが私の方法です。助けてくれてありがとう。私はまだこれに取り組んでいます。

 public void CrearExcel() throws SQLException {
        //Map param = new HashMap();


        try {
            Connection conn = Conexion.getConnTest();
            PreparedStatement ps = null;
            ResultSet rs = null;
            String sql =
                "SELECT NUM_FICHA, ASPIRANTE.AP_PATERNO, ASPIRANTE.AP_MATERNO, ASPIRANTE.NOMBRE FROM REFERENCIA INNER JOIN ASPIRANTE ON REFERENCIA.FK_ASPIRANTE_ID_ASPIRANTE = ASPIRANTE.ID_USUARIO WHERE NUM_FICHA IS NOT NULL";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();


            HSSFWorkbook hwb = new HSSFWorkbook();
            HSSFSheet sheet = hwb.createSheet("new sheet");

            Row rowhead = sheet.createRow(0);

            rowhead.createCell(0).setCellValue("NUMFICHA");
            rowhead.createCell(1).setCellValue("APELLIDO PATERNO");
            rowhead.createCell(2).setCellValue("APELLIDO MATERNO");
            rowhead.createCell(3).setCellValue("NOMBRE");
            rowhead.createCell(4).setCellValue("EXAMEN1");
            rowhead.createCell(5).setCellValue("EXAMEN2");

            int index = 1;
            while (rs.next()) {
                Row row = sheet.createRow(index);

                row.createCell(0).setCellValue(rs.getInt(1));
                row.createCell(1).setCellValue(rs.getString(2));
                row.createCell(2).setCellValue(rs.getString(3));
                row.createCell(3).setCellValue(rs.getString(4));
                row.createCell(4).setCellValue("");
                row.createCell(5).setCellValue("");
                index++;

            }


            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            hwb.write(outByteStream);
            byte[] outArray = outByteStream.toByteArray();

            response.setHeader("Content-Disposition",
                               "attachment; filename=testxls.xls");
            response.setContentType("application/vnd.ms-excel");
            response.flushBuffer();
            OutputStream outStream = response.getOutputStream();
            outStream.write(outArray);
            outStream.flush();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
4

3 に答える 3

2

ワークブックから応答出力ストリームに直接書き込まないのはなぜですか。

HSSFWorkbook hwb = new HSSFWorkbook();
...
OutputStream outStream = response.getOutputStream();
hwb.write(outputStream);
....

したがって、問題が発生する可能性があるため、バイト配列 (出力ストリーム) 部分をスキップしてみてください。

ここに役立つ情報がいくつかあります。

これで行き詰まりが解消されることを願っています (または、少なくとも行き詰まりを解消する道を進んでいます)。

于 2012-12-15T09:13:45.577 に答える
1

Excelファイルはテキストコンテンツを受信できるため、バイナリデータを印刷する代わりに、テキストデータを印刷してみてください。それでも問題が解決しない場合は、少なくともバイナリデータを印刷するよりも良い方向を示すことができるはずです。

于 2012-12-14T22:11:03.067 に答える
0

特定のサーバーを使用している場合、よくある間違いです。書き込む前に応答をリセットする必要があります。

...
response.reset(); 
response.setHeader("Content-Disposition",
                               "attachment; filename=testxls.xls");
...
于 2015-04-28T11:13:03.903 に答える