2012-12-14 59 views
0

我有用于导出excel文档的这种方法,但是当我打开excel时,我得到这种带有奇怪字符和符号的文件,没有格式化;有人能指导我如何解决这个问题吗?在Apache Poi中从我导出的Excel中删除特殊字符和符号

enter image description here

这是我的方法,感谢您的帮助;我仍在为此工作。

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(); 
     } 


    } 
} 
+1

您确定您的查询没有返回“奇怪”结果吗?也许尝试打印任何结果查询返回 – acostache

+0

我相信我的查询不会返回任何特殊的符号或字符,我试图改为utf 8但它不起作用 – bentham

+0

请帮助,我卡在这 – bentham

回答

1

由于Excel文件可以接收文本内容,而不是打印出来二进制数据,试图打印出文本数据。如果这样不能解决问题,至少应该能够指出你比打印二进制数据更好的方向。

+0

对不起,我不明白你的答案我该怎么做呢? – bentham

2

你为什么不直接从工作簿中的响应outpustream写:

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

所以,尽量跳过字节数组(outpu流)的一部分,因为这可能导致您的问题。

这里的some more info可能有所帮助。

希望这可以让你脱钩(或者至少在脱钩的道路上)。

+0

感谢您的支持,让我试试这个 – bentham

+0

我得到了同样的结果,请帮助,任何其他的想法? – bentham

+0

即使使用静态数据,我也能得到相同的结果,任何方法? – bentham

0

如果您使用某些服务器,这是一个常见错误。您必须在写入之前重置响应:

... 
response.reset(); 
response.setHeader("Content-Disposition", 
           "attachment; filename=testxls.xls"); 
...