导出为excel

   	boolean isCreate = false;
    	String[] pathInfo = classPath("S_CLASS");
    	String uploadPath = pathInfo[1]+"tmpFile/";                      //文件临时存储路径
    	
		String[] classInfo = getClassInfo(classInfoId);
		List planList = getPlanList(classInfoId);
		String[] planTitle = new String[]{"日期","时间","课程名称","讲师","单位","职务/职称","学习方式","上课地点"};
		planList.add(0,planTitle);
    	//构建临时的Excel文件
		String tmpFilePath=uploadPath+"plan"+classInfo[0]+".xls";
		File file = new File(tmpFilePath);

		isCreate = writeExcel(file,classInfo[1],planList);
		if(isCreate){
			try{
				SmartUpload myup = new SmartUpload();
				myup.initialize(pageContext);
				myup.setContentDisposition(null);
				myup.downloadFile(tmpFilePath,"","“"+classInfo[1]+"”学习计划.xls");
			}catch(Exception e){
				Date date = new Date();
				SimpleDateFormat sDFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				System.out.println(sDFormat.format(date)+"\t文件下载异常");
				e.printStackTrace();
				out.print("<script language=\"javascript\">alert(\"导出文件下载异常\");window.location.href=\"mag_class_list.jsp?class_info_id="+classInfoId +"\";</script>");
			}			
		}else{
			out.print("<script language=\"javascript\">alert(\"导出文件创建失败\");window.location.href=\"mag_class_list.jsp?class_info_id="+classInfoId +"\";</script>");	
		}


代码片段下

	/** Excel写文件功能,作为demo
	 * 	file             导出文件的名称
	 *  sheetName        Excel第一个sheet的名称
	 *  fileContent      文件内容(第一行为表头信息)
	 */
	private boolean writeExcel(File file,String SheetName,List fileContent){
		boolean ret = false;
		try {
			WritableWorkbook workbook = Workbook.createWorkbook(file);  //以Excel表格打开文件
			WritableSheet worksheet=workbook.createSheet(SheetName,0);  //创建Sheet
			//定义表头样式
				WritableFont titlef = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
				WritableCellFormat TitleFormat = new WritableCellFormat (titlef);				
				TitleFormat.setAlignment(jxl.format.Alignment.CENTRE);								//设置单元格水平对齐方式
				TitleFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);				//设置单元格垂直对齐方式
				TitleFormat.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);		//设置单元格顶部边框样式
				TitleFormat.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);		//设置单元格左侧边框样式
				TitleFormat.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN);		//设置单元格右部边框样式
				TitleFormat.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);	//设置单元格底部边框样式
				TitleFormat.setBackground(jxl.format.Colour.ICE_BLUE);								//设置单元格背景颜色
				
			//定义单元格样式
				WritableFont tbFont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); 
				WritableCellFormat ContentFormat = new WritableCellFormat (tbFont); 
				ContentFormat.setAlignment(jxl.format.Alignment.CENTRE);
				ContentFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
				ContentFormat.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
				ContentFormat.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);
				ContentFormat.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN);
				ContentFormat.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);

					CellView cellViewCol = new CellView();  
						cellViewCol.setSize(11*256); //设置列宽  

					CellView cellViewRowOne = new CellView();  
						cellViewRowOne.setSize(2*256); //设置第一行行高 

					CellView cellViewRow = new CellView();  
						cellViewRow.setSize(3*230); //设置行高 
			
			int allNum = fileContent.size();
			Label label = null;
			for(int i=0;i<allNum;i++){
				String[] strArr = (String[])fileContent.get(i);
				if(i == 0){
					for(int j=0;j<strArr.length;j++){
						label = new Label(j,i,strArr[j],TitleFormat);
						worksheet.addCell(label);
						worksheet.setColumnView(j, cellViewCol);//设置列宽
					  worksheet.setRowView(i,cellViewRowOne);//第一行独有行高
					}
				}else{
					if("Y".equals(strArr[0])){
						label = new Label(0,i,"自由",ContentFormat);
						worksheet.addCell(label);
						label = new Label(1,i,"自由",ContentFormat);
						worksheet.addCell(label);
						worksheet.setColumnView(0,cellViewCol);//设置列宽
						worksheet.setColumnView(1,cellViewCol);//设置列宽
						worksheet.setRowView(i,cellViewRow);//设置行高						
					}else if("N".equals(strArr[0])){
						label = new Label(0,i,strArr[1],ContentFormat);
						worksheet.addCell(label);
						label = new Label(1,i,strArr[2]+"-"+strArr[3],ContentFormat);
						worksheet.addCell(label);
						worksheet.setColumnView(0,cellViewCol);//设置列宽
						worksheet.setColumnView(1,cellViewCol);//设置列宽
						worksheet.setRowView(i,cellViewRow);//设置行高						
					}
					for(int j=4;j<strArr.length;j++){
						if(j == 8){
							label = new Label(j-2,i,learnStyle(strArr[j]),ContentFormat);
						}else{
						    label = new Label(j-2,i,strArr[j],ContentFormat);
						}
						worksheet.addCell(label);
						worksheet.setColumnView(j,cellViewCol);//设置列宽
						worksheet.setRowView(i,cellViewRow);//设置行高						
					}
				}
				label = null;
			}
			workbook.write();
			workbook.close();
			ret = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值