使用jxcell创建表格设置行宽、背景色并进行自定义下载路径

jxcell导出图表使用范围较广,将项目部署在Linux下也可正常使用,此篇博客只讲解了创建表格并进行了一些自定义设置,未使用创建图功能。

		View m_view = new View();
		RangeRef newRange = null;
		try {
			m_view.getLock();
			//设置标题行背景颜色
			ConditionFormat condfmt[]=new ConditionFormat[1];
			condfmt[0] = m_view.CreateConditionFormat();
			// Condition #1
			CellFormat cf=condfmt[0].getCellFormat();
			condfmt[0].setType(ConditionFormat.TypeFormula);
			condfmt[0].setFormula1("and(iseven(row()), $D1 > 1000)", 0, 0);
			
			//  cf.setFontColor(0x00ff00);//设置字体颜色
			cf.setPattern((short)1);
			cf.setFontSize(10);
			cf.setPatternFG(0x00CCFF);//设置背景颜色
			condfmt[0].setCellFormat(cf);
			// Select the range and apply conditional formatting
			m_view.setSelection(1, 1, 1, 14);//设定背景色位置   第一个第二个参数定位起始点  第三个参数定位多少行  第四个参数定位多少列 
			m_view.setConditionalFormats(condfmt);

			m_view.setDefaultColWidth(3000);//自定义设定单元格行宽
			//标题 setTextAsValue(行,列,值);
			m_view.setTextAsValue(1,1,"客户");
			m_view.setTextAsValue(1,2,"服务经理");
			m_view.setTextAsValue(1,3,"项目编码");
			m_view.setTextAsValue(1,4,"项目名称");
			m_view.setTextAsValue(1,5,"项目概述");
			m_view.setTextAsValue(1,6,"项目进展");
		
				m_view.setTextAsValue((2+i),1,customName);//客户名字
				m_view.setTextAsValue((2+i),2,fullname);//服务经理
				m_view.setTextAsValue((2+i),3,projsetPnum);
				m_view.setTextAsValue((2+i),4,projsetPname);
				m_view.setTextAsValue((2+i),5,projsetContent);
				m_view.setTextAsValue((2+i),6,stateName);//项目进展

			String fname = "项目列表.xls";
//文件名中文处理
			if(request.getHeader("User-Agent").toLowerCase().contains("firefox")){
				//火狐浏览器需要用base64编码
				fname = "=?utf-8?b?"+Base64.getEncoder().encodeToString(fname.getBytes("utf-8"))+"?=";
			}else {
				//其他浏览器utf-8即可
				fname = URLEncoder.encode(fname , "utf-8");
			}
			
			//清空response  
			response.reset();  
			//设置response的Header  
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			response.addHeader("Content-Disposition", "attachment;filename="+ fname);  
			OutputStream os = response.getOutputStream();
			m_view.write(os);
			os.flush();
		}catch (Exception e) {
			e.printStackTrace();
		}

猜你喜欢

转载自blog.csdn.net/qq_43137849/article/details/86489162