• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

JXL导出Excel文件

OC/C/C++ 水墨上仙 1334次浏览

JXL导出Excel文件


/**
     * 导出为Excel
     * @param cdosCategoryKeyWords
     * @return
     */
    public boolean exportExcel(CDO[] cdosCategoryKeyWords)
	{
		HttpServletResponse response=ServletActionContext.getResponse();
		OutputStream os=null;
		try 
		{
			os = response.getOutputStream();
		} 
		catch (IOException e1) 
		{
			mallLog.error("获取response输出流出错");
		}
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String curDatetime = sdf.format(cal.getTime());
	    response.reset();
        response.setHeader("Content-disposition", "attachment; filename=CategoryKeyWords"+curDatetime+".xls");
	    response.setContentType("application/msexcel");
		WritableWorkbook wwb=null;
		WritableSheet ws=null;
		try 
		{
			wwb = Workbook.createWorkbook(os);
			ws=wwb.createSheet("关键词类目匹配列表",0);
			ws.getSettings().setDefaultColumnWidth(15);
			//创建表头
			WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
            WritableCellFormat wcfFC = new WritableCellFormat(wfc);
            Label lId2HeadLabel = new Label(0,0,"编号ID",wcfFC);
			Label strKeyWordHeadLabel = new Label(1,0,"关键词",wcfFC);
			Label strCategoryConfigHeadLabel = new Label(2,0,"类目",wcfFC);
			Label dtModifyTimeHeadLabel = new Label(3,0,"更新时间",wcfFC);
			
			ws.addCell(lId2HeadLabel);
			ws.addCell(strKeyWordHeadLabel);
			ws.setColumnView(2, 50);
			ws.addCell(strCategoryConfigHeadLabel);
			ws.addCell(dtModifyTimeHeadLabel);
			
	        Label lId2Label = null;
			Label strKeyWordLabel = null;
			Label strCategoryConfigLabel = null;
			Label dtModifyTimeLabel = null;
			for(int i=1;i<=cdosCategoryKeyWords.length;i++)
			{
				String lId2 = cdosCategoryKeyWords[i-1].getStringValue("lId"); //ID
				String strKeyWord = cdosCategoryKeyWords[i-1].getStringValue("strKeyWord"); //关键词
				String strCategoryConfig = cdosCategoryKeyWords[i-1].getStringValue("strCategoryConfig");//类目
				String dtModifyTime = cdosCategoryKeyWords[i-1].getStringValue("dtModifyTime");//更新时间
				lId2Label = new Label(0,i,lId2);
				strKeyWordLabel = new Label(1,i,strKeyWord);
				strCategoryConfigLabel = new Label(2,i,strCategoryConfig);
				dtModifyTimeLabel = new Label(3,i,dtModifyTime);
				ws.addCell(lId2Label);
				ws.addCell(strKeyWordLabel);
				ws.addCell(strCategoryConfigLabel);
				ws.addCell(dtModifyTimeLabel);
			}
		} 
		catch (Exception e) 
		{
			mallLog.error("输出Excel失败");
			return false;
		}
		finally
		{
			try 
			{
				wwb.write();
				wwb.close();
				os.close();
			} 
			catch (WriteException e) 
			{
				mallLog.error("关闭WritableWorkbook出错");
			} 
			catch (IOException e) 
			{
				mallLog.error("关闭WritableWorkbook出错");
			}
		}
		return true;
	}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明JXL导出Excel文件
喜欢 (0)
加载中……