我写了ExcelBuilder类,使用spring mvc使用org.apache.poi 3.14将数据导出到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());
}
}
}
但是我得到了例外:
HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: org.apache.poi.hssf.usermodel.HSSFWorkbook cannot be cast to org.apache.poi.ss.usermodel.Workbook
如何解决此异常?