番茄棒
2017-04-26 02:31
采纳率: 0%
浏览 1.0k

关于ssh 在页面上显示的数据以Excel格式导出时,一直是中文乱码,代码如下,请大家帮忙看一下。

action中:public String export()throws Exception{
List<?> list =userService.selectAllUser();
/*设置表头:对Excel每列取名
(必须根据你取的数据编写)
*/
String []tableHeader={"编号","用户名","姓名","性别","年龄","年级","手机号"};
/

下面的都可以拷贝不用编写
*/
short cellNumber=(short)tableHeader.length;//表的列数
HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个excel
HSSFCell cell = null; //Excel的列
HSSFRow row = null; //Excel的行
HSSFCellStyle style = workbook.createCellStyle(); //设置表头的类型
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCellStyle style1 = workbook.createCellStyle(); //设置数据类型
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font = workbook.createFont(); //设置字体
HSSFSheet sheet = workbook.createSheet("sheet1"); //创建一个sheet
HSSFHeader header = sheet.getHeader();//设置sheet的头
try { /
*
根据是否取出数据,设置header信息
*
*/
if(list.size() < 1 ){
header.setCenter("查无资料");
}else{
header.setCenter("学生表");
row = sheet.createRow(0);
row.setHeight((short)400);
for(short k = 0;k < cellNumber;k++){
cell = row.createCell(k);//创建第0行第k列
cell.setCellValue(tableHeader[k]);//设置第0行第k列的值
sheet.setColumnWidth(k,(short) 8000);//设置列的宽度
font.setColor(HSSFFont.COLOR_NORMAL); // 设置单元格字体的颜色.
font.setFontHeight((short)350); //设置单元字体高度
style1.setFont(font);//设置字体风格
cell.setCellStyle(style1);
}
/

* // 给excel填充数据这里需要编写
*

*/
for(int i = 0 ;i < list.size() ;i++){

User ur = (User)list.get(i);//获取student对象
row = sheet.createRow((short) (i + 1));//创建第i+1行
row.setHeight((short)400);//设置行高

        if(ur.getId() != null){ 
        cell = row.createCell((short) 0);//创建第i+1行第0列 
        cell.setCellValue(ur.getId());//设置第i+1行第0列的值 
        cell.setCellStyle(style);//设置风格 
        } 
        if(ur.getUsername() != null){ 
        cell = row.createCell((short) 1); //创建第i+1行第1列 

        cell.setCellValue(ur.getUsername());//设置第i+1行第1列的值 

        cell.setCellStyle(style); //设置风格 
        } 
         if(ur.getRealname()!= null){ 
                cell = row.createCell((short) 2); 
                cell.setCellValue(ur.getRealname()); 
                cell.setCellStyle(style); 
                } 
         if(ur.getSex()!= null){ 
                cell = row.createCell((short) 3); 
                cell.setCellValue(ur.getSex()); 
                cell.setCellStyle(style); 
                }

          if(ur.getAge()!= null){ 
                cell = row.createCell((short) 4); 
                cell.setCellValue(ur.getAge()); 
                cell.setCellStyle(style); 
                }   
          if(ur.getGrade()!= null){ 
                cell = row.createCell((short) 5); 
                cell.setCellValue(ur.getGrade()); 
                cell.setCellStyle(style); 
                } 
                if(ur.getMobile()!= null){ 
                cell = row.createCell((short) 6); 
                cell.setCellValue(ur.getMobile()); 
                cell.setCellStyle(style); 
                } 



            } 

            } 

            } catch (Exception e) { 
            e.printStackTrace(); 
            } 

        HttpServletResponse response = null;//创建一个HttpServletResponse对象 

        OutputStream out = null;//创建一个输出流对象 
        try { 
        response = ServletActionContext.getResponse();//初始化HttpServletResponse对象 


        //out.write(new   byte []{( byte ) 0xEF ,( byte ) 0xBB ,( byte ) 0xBF });  
                response.setHeader("Content-disposition","attachment; filename="+"student.xls");//filename是下载的xls的名,建议最好用英文 
                response.setContentType("application/msexcel;charset=UTF-8");//设置类型 
                response.setCharacterEncoding("UTF-8");
                response.setHeader("Pragma","No-cache");//设置头 
                response.setHeader("Cache-Control","no-cache");//设置头 
                response.setDateHeader("Expires", 0);//设置日期头
                out = response.getOutputStream();//
                workbook.write(out); 
                out.flush(); 
               workbook.write(out); 
        } catch (IOException e) { 
        e.printStackTrace(); 
        }finally{ 
        try{ 

        if(out!=null){ 
        out.close(); 
        } 

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

        } 

    return null;   


}

jsp中:<form action="excel" method="post" enctype="multipart/form-data" theme="simple">
            <s:submit value="导出"/>
            </form> 
struts:配置正确应该,就不贴了![![图片说明](https://img-ask.csdn.net/upload/201704/26/1493172527_981798.png)图片说明](https://img-ask.csdn.net/upload/201704/26/1493172523_88211.png)

导出的excel:如下图

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • boymzq 2017-04-26 03:08

    查看一下自己的操作系统支持什么编码

    打赏 评论
  • Nirvana-- 2017-04-26 03:30

    写一个字符过滤方法, 聪数据库中查出时转换一下

    打赏 评论
  • 点点滴滴的淡淡的 2017-04-26 09:05
     我的response只有两句
     String fileName = this.getTitle() + ".xlsx";  
    
                        final String userAgent = request.getHeader("USER-AGENT");  
                       if(userAgent!=null&&userAgent.contains("MSIE")){//IE浏览器  
                          fileName = URLEncoder.encode(fileName,"UTF8");  
                        }else if(userAgent!=null&&userAgent.contains("Mozilla")){//google,火狐浏览器  
                            fileName = new String(fileName.getBytes(), "ISO8859-1");  
                        }else{  
                            fileName = URLEncoder.encode(fileName,"UTF8");//其他浏览器  
                        }  
    
    
    
                        String headStr = "attachment; filename=\"" + fileName + "\"";  
                        response.setContentType("APPLICATION/OCTET-STREAM");  
                        response.setHeader("Content-Disposition", headStr);  
    
    打赏 评论