适用于每行记录存放的是在一个以为数组中,这样方便读取
public class ReportAction extends DispatchAction{
/**
*
* @describe
* @return void
* @param ActionMapping
* @param ActionForm
* @param HttpServletReuqest
* @param HttpServletResponse
* @return ActionForward
*/
public void importExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
List dataList=(List) request.getSession().getAttribute("list");
String statdesc=(String)request.getSession().getAttribute("statdesc");
String[] title=(String[])dataList.get(0);
String content[]=null;
Label label;
String url = "files/";
String filePath = request.getSession().getServletContext().getRealPath("/")+url;;//文件存放初始路径
String sdf = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
OutputStream os = new FileOutputStream(filePath+"Count_"+sdf+".xls");
WritableWorkbook wwb=Workbook.createWorkbook(os);
WritableSheet sheet = wwb.createSheet("故障数统计", 0);
// 添加表头 " 故障数统计 ",合并单元格
sheet.mergeCells(0, 0, title.length - 1, 0);
label = new Label(0, 0, "故障数统计:"+statdesc, setTitleCellFormat());
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
// 将业务数据写入到工作表中
if(dataList!= null){
for(int i=0; i<dataList.size(); i++){
content =(String[])dataList.get(i);
for(int j=0;j<content.length;j++){
label = new Label(j,i+1, content[j]==null?"0":content[j], setContentCellFormat());
sheet.addCell(label);
sheet.setColumnView(i,20);
}
}
}
// 写入数据
wwb.write();
// 关闭文件
wwb.close();
os.close();
String filename=url+"Count_"+sdf+".xls";
out.write(filename);
out.flush();
out.close();
}
/**
*
* @describe 设置内容单元格格式
* @return WritableCellFormat
* @param
*/
public WritableCellFormat setContentCellFormat() throws Exception{
//内容字体
WritableFont cf= new WritableFont(
WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jx l.format.Colour.BLACK);
WritableCellFormat contentCellFormat = new WritableCellFormat(cf);
contentCellFormat.setAlignment(Alignment.CENTRE); //把水平对齐方式指定为居中
contentCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE); //把垂直对齐方式指定为居中
contentCellFormat.setWrap(true);
return contentCellFormat;
}
/**
* @describe 设置表头单元格格式
* @return WritableCellFormat
* @param
*/
public WritableCellFormat setTitleCellFormat() throws WriteException{
//表头字体
WritableFont tf= new WritableFont(
WritableFont.createFont("宋体"), 15, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl .format.Colour.BLACK);
WritableCellFormat titleCellFormat = new WritableCellFormat(tf);
titleCellFormat.setAlignment(Alignment.CENTRE); //把水平对齐方式指定为居中
titleCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE); //把垂直对齐方式指定为居中
return titleCellFormat;
}
}