strut1+jxl将后台数据导出为excel

   适用于每行记录存放的是在一个以为数组中,这样方便读取
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;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值