番茄棒 2017-04-26 02:31 采纳率: 0%
浏览 1008
已结题

关于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
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题