Java 将HSSF工作簿转换为工作簿

Java 将HSSF工作簿转换为工作簿,java,excel,spring-mvc,Java,Excel,Spring Mvc,我编写了ExcelBuilder类,使用org.apache.poi 3.14使用spring mvc将数据导出到excel文件,但是我得到了ClassCastException 这是我的代码: package net.codejava.spring; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import

我编写了ExcelBuilder类,使用org.apache.poi 3.14使用spring mvc将数据导出到excel文件,但是我得到了
ClassCastException

这是我的代码:

package net.codejava.spring;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.*;
//import org.apache.poi.hssf.usermodel.HSSFCellStyle;
//import org.apache.poi.hssf.usermodel.HSSFFont;
//import org.apache.poi.hssf.usermodel.HSSFRow;
//import org.apache.poi.hssf.usermodel.HSSFSheet;
//import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.WorkbookUtil;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import net.codejava.spring.model.Contacting;
public class ExcelBuilder2 extends AbstractExcelView {



    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
         // get data model which is passed by the Spring container
        List<Contacting> listContactings = (List<Contacting>) model.get("listContactings");
        Workbook wb = (Workbook) new HSSFWorkbook();
        CreationHelper createHelper = wb.getCreationHelper();
        // create a new Excel sheet
        Sheet sheet = wb.createSheet("Java Books");
        sheet.setDefaultColumnWidth((short) 30);

        // create style for header cells
        CellStyle style = (CellStyle) wb.createCellStyle();
        Font font = (Font) wb.createFont();
    //    font.setCharSet( FontCharset..getValue());
        font.setFontName("Arial");
        font.setCharSet(FontCharset.HEBREW.getValue());



     //   style.setFillForegroundColor(Color.);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
       font.setBoldweight(Font.BOLDWEIGHT_BOLD);
       // font.setColor(Color.WHITE.index);
      //  style.setFont(font);


        // create header row

        Row header = sheet.createRow(0);

        header.createCell((short) 0).setCellValue("contacting_id");
        header.getCell((short) 0).setCellStyle((CellStyle) style);

        header.createCell((short) 1).setCellValue("user_id");
        header.getCell((short) 1).setCellStyle((CellStyle) style);

        header.createCell((short) 2).setCellValue("subject");
        header.getCell((short) 2).setCellStyle((CellStyle)style);

        header.createCell((short) 3).setCellValue("location");
        header.getCell((short) 3).setCellStyle((CellStyle)style);

        header.createCell((short)4).setCellValue("content");
        header.getCell((short)4).setCellStyle((CellStyle)style);

        header.createCell((short)5).setCellValue("department");
        header.getCell((short)5).setCellStyle((CellStyle)style);



        header.createCell((short)6).setCellValue("status");
        header.getCell((short)6).setCellStyle((CellStyle)style);




        header.createCell((short)7).setCellValue("contacting_date");
        header.getCell((short)7).setCellStyle((CellStyle)style);


        header.createCell((short)8).setCellValue("house_Number");
        header.getCell((short)8).setCellStyle((CellStyle)style);

        header.createCell((short)9).setCellValue("Urgency");
        header.getCell((short)9).setCellStyle((CellStyle)style);

        header.createCell((short)10).setCellValue("is_inspector");
        header.getCell((short)10).setCellStyle((CellStyle)style);

        header.createCell((short)11).setCellValue("inspectorStatus");
        header.getCell((short)11).setCellStyle((CellStyle)style);



        header.createCell((short)12).setCellValue("stringDate");
        header.getCell((short)12).setCellStyle((CellStyle)style);

        header.createCell((short)13).setCellValue("openedBy");
        header.getCell((short)13).setCellStyle((CellStyle)style);

        // create data rows
        int rowCount = 1;

        for (Contacting Contacting : listContactings) {
            Row aRow = sheet.createRow(rowCount++);
            if(Contacting.getContacting_id()!=null)
            aRow.createCell((short) 0).setCellValue(Contacting.getContacting_id());
            if(Contacting.getUser_id()!=null)
            aRow.createCell((short)1).setCellValue(Contacting.getUser_id());
            if(Contacting.getSubject()!=null)
            aRow.createCell((short)2).setCellValue(Contacting.getSubject());
            if(Contacting.getLocation()!=null)
            aRow.createCell((short)3).setCellValue(Contacting.getLocation());
            if(Contacting.getContent()!=null)
            aRow.createCell((short)4).setCellValue(Contacting.getContent());
            if(Contacting.getdepartment()!=null)
            aRow.createCell((short)5).setCellValue(Contacting.getdepartment());
            if(Contacting.getStatus()!=null)
            aRow.createCell((short)6).setCellValue(Contacting.getStatus());
            if(Contacting.getContacting_date()!=null)
            aRow.createCell((short)7).setCellValue(Contacting.getContacting_date());
            if(Contacting.getHouse_Number()!=null)
            aRow.createCell((short)8).setCellValue(Contacting.getHouse_Number());
            if(Contacting.getUrgency()!=null)
            aRow.createCell((short)9).setCellValue(Contacting.getUrgency());
            if(Contacting.getIs_inspector()!=null)
            aRow.createCell((short)10).setCellValue(Contacting.getIs_inspector());
            if(Contacting.getInspectorStatus()!=null)
            aRow.createCell((short)11).setCellValue(Contacting.getInspectorStatus());
            if(Contacting.getStringDate()!=null)
            aRow.createCell((short)12).setCellValue(Contacting.getStringDate());
            if(Contacting.getOpenedBy()!=null)
            aRow.createCell((short)13).setCellValue(Contacting.getOpenedBy());



        }
    }


}
package net.codejava.spring;
导入java.util.Date;
导入java.util.List;
导入java.util.Map;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入org.apache.poi.hssf.usermodel.*;
//导入org.apache.poi.hssf.usermodel.HSSFCellStyle;
//导入org.apache.poi.hssf.usermodel.hssfont;
//导入org.apache.poi.hssf.usermodel.HSSFRow;
//导入org.apache.poi.hssf.usermodel.HSSFSheet;
//导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
//导入org.apache.poi.hssf.util.HSSFColor;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.WorkbookUtil;
导入org.springframework.web.servlet.view.document.AbstractExcelView;
导入net.codejava.spring.model.Contacting;
公共类ExcelBuilder2扩展了AbstractExcelView{
@凌驾
受保护的void buildExcelDocument(映射模型、HSSF工作簿、HttpServletRequest请求、,
HttpServletResponse)引发异常{
//TODO自动生成的方法存根
//获取Spring容器传递的数据模型
List listContactings=(List)model.get(“listContactings”);
工作簿wb=(工作簿)新的HSSF工作簿();
CreationHelper createHelper=wb.getCreationHelper();
//创建新的Excel工作表
Sheet Sheet=wb.createSheet(“Java书籍”);
活页宽度((短)30);
//创建标题单元格的样式
CellStyle=(CellStyle)wb.createCellStyle();
Font Font=(Font)wb.createFont();
//font.setCharSet(FontCharset..getValue());
font.setFontName(“Arial”);
setCharSet(FontCharset.HEBREW.getValue());
//样式。setFillForegroundColor(颜色);
style.setFillPattern(CellStyle.SOLID\u前景);
font.setBoldweight(font.BOLDWEIGHT\u BOLD);
//font.setColor(Color.WHITE.index);
//style.setFont(字体);
//创建标题行
行标题=sheet.createRow(0);
header.createCell((短)0.setCellValue(“联系人id”);
header.getCell((短)0.setCellStyle((CellStyle)样式);
header.createCell((短)1.setCellValue(“用户id”);
header.getCell((短)1.setCellStyle((CellStyle)样式);
header.createCell((短)2.setCellValue(“主题”);
header.getCell((短)2.setCellStyle((CellStyle)样式);
header.createCell((短)3.setCellValue(“位置”);
header.getCell((短)3.setCellStyle((CellStyle)样式);
header.createCell((短)4.setCellValue(“内容”);
header.getCell((短)4.setCellStyle((CellStyle)样式);
header.createCell((短)5.setCellValue(“部门”);
header.getCell((短)5.setCellStyle((CellStyle)样式);
header.createCell((短)6.setCellValue(“状态”);
header.getCell((短)6.setCellStyle((CellStyle)样式);
header.createCell((短)7.setCellValue(“联系日期”);
header.getCell((短)7.setCellStyle((CellStyle)样式);
header.createCell((短)8.setCellValue(“房屋号”);
header.getCell((短)8.setCellStyle((CellStyle)样式);
header.createCell((短)9.setCellValue(“紧迫性”);
header.getCell((短)9.setCellStyle((CellStyle)样式);
header.createCell((短)10.setCellValue(“is_inspector”);
header.getCell((短)10.setCellStyle((CellStyle)样式);
header.createCell((短)11.setCellValue(“inspectorStatus”);
header.getCell((短)11.setCellStyle((CellStyle)样式);
header.createCell((短)12.setCellValue(“stringDate”);
header.getCell((短)12.setCellStyle((CellStyle)样式);
header.createCell((短)13.setCellValue(“openedBy”);
header.getCell((短)13.setCellStyle((CellStyle)样式);
//创建数据行
int rowCount=1;
用于(联系人:列表联系人){
Row aRow=sheet.createRow(rowCount++);
if(Contacting.getContacting_id()!=null)
aRow.createCell((短)0.setCellValue(Contacting.getContacting_id());
if(Contacting.getUser_id()!=null)
aRow.createCell((短)1.setCellValue(Contacting.getUser_id());
if(Contacting.getSubject()!=null)
createCell((short)2.setCellValue(Contacting.getSubject());
if(Contacting.getLocation()!=null)
createCell((short)3.setCellValue(Contacting.getLocation());
if(Contacting.getContent()!=null)
createCell((短)4.setCellValue(Contacting.getContent());
if(Contacting.getdepartment()!=null)
aRow.createCell((短)5.setCellValue(Contacting.getdepartment());
if(Contacting.getStatus()!=null)
aRow.createCell((短)6.setCellValue(Contacting.getStatus());
if(Contacting.getContacting_date()!=null)
aRow.createCell((短)7.setCellValue(Contacting.getContacting_date());
如果(联系.getHouse_Number()!=null)
aRow.createCell((短)8.setCellValue(Contacting.getHouse_Number());
if(Contacting.geturrency()!=null)
aRow.createCell((短)9.setCellValue(Contacting.getUrgency());
if(联系.getIs_inspector()!=null)
aRow.createCell((短)10.setCellValue(Contacting.getIs_inspector());
if(联系.getInspectorStatus()!=null)
createCell((short)11.setCellValue(Contacting.getInspectorStatus());
if(Contacting.getStringDate()!=null)
aRow.createCell((短)12.setCellValue(Contacting.getStringDate());
if(Contacting.getOpenedBy()!=null)
createCell((short)13.setCellValue(Contacting.getOpenedBy());
}
}
}
<