利用servlet输出流导出excel,需要有poi包个人封装了一个excel类仅供参考
public class Excle implements BaseExcle
{
public Excle(){
}
public Excle(HttpServletResponse response,String savedName){
this.setupDefaultHeader(response, savedName);
}
/**
* 版本:EAS7.0
* 作者(修改人):xu chang
* 修改时间:2014-9-17
* <p>
* 描 述:覆盖父类方法 #excle基本导出功能
* @throws IOException
* @throws WriteException
* @see com.kingdee.eas.custom.common.excle.BaseExcle#export(javax.servlet.ServletOutputStream,
* com.kingdee.eas.framework.CoreBaseCollection, java.lang.String[])
*/
public void export(ServletOutputStream servletOutputStream, CoreBaseCollection col, String[] colName) throws EASBizException, BOSException, IOException, WriteException
{
WritableWorkbook workbook=Workbook.createWorkbook(servletOutputStream);
//定义表格title和内容的字体以及格式
WritableFont font = new WritableFont(WritableFont.TAHOMA, 11,WritableFont.BOLD);// 定义字体
// 字体颜色
font.setColour(Colour.BLACK);
WritableCellFormat headFormat = new WritableCellFormat(font);
headFormat.setAlignment(Alignment.CENTRE); // 设置标题居中对其
headFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线
headFormat.setBackground(jxl.format.Colour.YELLOW); // 设置单元格的背景颜色
WritableCellFormat lineFormat = new WritableCellFormat();
lineFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线
WritableCellFormat amountFormat = new WritableCellFormat();
amountFormat.setAlignment(Alignment.RIGHT); // 设置标题居中对其
amountFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线
WritableSheet sheet=workbook.createSheet("单据导出", 0);
sheet.setRowView(0, 280); // 设置第一行的行高
for (int i = 0; i < colName.length; i++) {
if (i == 0) {
sheet.setColumnView(i, 35);// 设置列宽
}else if(i==colName.length-1){
sheet.setColumnView(i, 15);// 设置列宽
}else {
sheet.setColumnView(i, 25);// 设置列宽
}
sheet.addCell(new Label(i, 0, colName[i], headFormat));
}
CoreBaseInfo baseInfo=col.get(0);
if(baseInfo instanceof ProjectBillInfo){
//立项 --11个字段
for(int i=0;i<col.size();i++){
ProjectBillInfo info=(ProjectBillInfo) col.get(i);
sheet.addCell(new Label(0, i + 1,info.getLxnumber(),lineFormat));
sheet.addCell(new Label(1, i + 1,info.getPmname(),lineFormat));
sheet.addCell(new Label(2, i + 1,getObjName(info.getBuname()),lineFormat));
sheet.addCell(new Label(3, i + 1,getObjName(info.getBuorz()),lineFormat));
sheet.addCell(new Label(4, i + 1,getObjName(info.getCname()),lineFormat));
sheet.addCell(new Label(5, i + 1,info.getOrzmag(),lineFormat));//区域负责人
sheet.addCell(new Label(6, i + 1,info.getOrzsales(),lineFormat));//销售负责人
sheet.addCell(new Label(7, i + 1,DateUtil(info.getSigndate()),lineFormat));//签订日期
sheet.addCell(new Label(8, i + 1,formatObj(info.getProfitamt()),lineFormat));//评估金额
sheet.addCell(new Label(9, i + 1,formatObj(info.getProfitpre()),lineFormat));//评估利润
sheet.addCell(new Label(10, i + 1,formatObj(info.getState()),lineFormat));//单据状态
}
}
workbook.write();
workbook.close();
servletOutputStream.flush();
System.out.println("导出Excel成功!!!");
}
public void setupDefaultHeader(HttpServletResponse httpRes,String savedName) {
String contentType = null;
contentType = "application/octet-stream";
httpRes.setContentType(contentType);
StringBuffer contentDisposition = new StringBuffer();
contentDisposition.append("attachment;");
try {
savedName = new String(savedName.getBytes("gb2312"), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("savedName is invalid!");
}
contentDisposition.append("filename=\"").append(
savedName.replace("\n", ""));
contentDisposition.append("\"");
httpRes.setHeader("Content-Disposition", contentDisposition.toString());
}
private String formatObj(Object obj)
{
if(obj instanceof Date){
return obj != null ? new SimpleDateFormat("yyyy-MM-dd").format(obj)
: null;
}else if(obj instanceof CoreBaseInfo){
String returnVal = "";
if(obj != null){
try{//特殊处理下,有些数据存在问题
returnVal = ((CoreBaseInfo)obj).get("name").toString() ;
}catch(Exception e){
}
}
return returnVal;
}else if(obj instanceof BigDecimal){
NumberFormat formater = new DecimalFormat("#,###,##0.00");
return obj == null ? formater.format(BigDecimal.ZERO) : formater
.format(obj);
}
return obj==null ? "":obj.toString();
}
}