一.生成Excel文件格式: 生成Excel文件要用到jxl.jar包,可以在晚上搜索到并下载下来放到WEB-INF/lib下面 /** * 保存Excel文件方法 * @param args Excel文件名称 * @param title 标题 * @param gh 操作工号 * @param colCommList 列描述信息 * @param columnList 列信息 * @param valueList 值信息 * @param HashMap columnInfoHash 列类型和长度信息 * return boolean true=good;false=error; */ public boolean creatExcelWriting(String args,String title,List colCommList,List columnList,List valueList,HashMap columnInfoHash){ WritableFont NormalFont; WritableFont BolDFont; WritableCellFormat wcf_centre,wcf_left,wcf_right,wcf_left_top,wcf_left_bottom,wcf_rigth_bottom,wcf_rigth_top,wcf_title;
try{ //**************设置单元格字体(本例中设置了2种字体您可以继续定制)***************/ //字体为ARIAL大小为10号 NormalFont = new WritableFont(WritableFont.ARIAL,10); //字体为ARIAL大小为14粗体 BolDFont = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD); /**************以下设置几种格式的单元格*************/ //用于标题 wcf_title = new WritableCellFormat(BolDFont); wcf_title.setBorder(Border.NONE, BorderLineStyle.THIN); //线条 wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直居中 wcf_title.setAlignment(Alignment.CENTRE); //水平居中 wcf_title.setWrap(false); //是否换行 //用于正文居中 wcf_centre = new WritableCellFormat(NormalFont); wcf_centre.setBorder(Border.ALL, BorderLineStyle.THIN); //线条 wcf_centre.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直居中 wcf_centre.setAlignment(Alignment.CENTRE); //水平居中 wcf_centre.setWrap(true); //是否换行
//用于正文居左 wcf_left = new WritableCellFormat(NormalFont); wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN); //线条 wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直对齐 wcf_left.setAlignment(Alignment.LEFT);//水平对齐 wcf_left.setWrap(true); //是否换行 //用于正文居右 wcf_right = new WritableCellFormat(NormalFont); wcf_right.setBorder(Border.ALL, BorderLineStyle.THIN); //线条 wcf_right.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直对齐 wcf_right.setAlignment(Alignment.RIGHT);//水平右 wcf_right.setWrap(false); //是否换行 //有于正文在左顶部(一般用于跨行) wcf_left_top = new WritableCellFormat(NormalFont); wcf_left_top.setBorder(Border.ALL, BorderLineStyle.THIN); //线条 wcf_left_top.setVerticalAlignment(VerticalAlignment.TOP); //垂直在顶部 wcf_left_top.setAlignment(Alignment.LEFT);//水平在左 wcf_left_top.setWrap(true); //是否换行
//有于正文在左底部(一般用于跨行) wcf_left_bottom = new WritableCellFormat(NormalFont); wcf_left_bottom.setBorder(Border.ALL, BorderLineStyle.THIN); //线条 wcf_left_bottom.setVerticalAlignment(VerticalAlignment.BOTTOM); //垂直在顶部 wcf_left_bottom.setAlignment(Alignment.LEFT);//水平在左 wcf_left_bottom.setWrap(false); //是否换行 //有于正文在右顶部(一般用于跨行) wcf_rigth_top = new WritableCellFormat(NormalFont); wcf_rigth_top.setBorder(Border.ALL, BorderLineStyle.THIN); //线条 wcf_rigth_top.setVerticalAlignment(VerticalAlignment.TOP); //垂直在顶部 wcf_rigth_top.setAlignment(Alignment.RIGHT);//水平在右 wcf_rigth_top.setWrap(true); //是否换行 //有于正文在右底部(一般用于跨行) wcf_rigth_bottom = new WritableCellFormat(NormalFont); wcf_rigth_bottom.setBorder(Border.ALL, BorderLineStyle.THIN); //线条 wcf_rigth_bottom.setVerticalAlignment(VerticalAlignment.BOTTOM); //垂直在顶部 wcf_rigth_bottom.setAlignment(Alignment.RIGHT);//水平在左 wcf_rigth_bottom.setWrap(false); //是否换行
//判断文件名参数是否为空 if (args==null || args.equals("")) { PubServers.getInstance().getMyLogInterface().myError("生成Excel文件出错:方法中文件名参数为空!"); return false; } //判断标题是否为空 if (title==null || title.equals("")) { PubServers.getInstance().getMyLogInterface().myError("生成Excel文件出错:方法中标题参数为空!"); return false; } //判断参数列描述信息是否为空 if (colCommList==null || colCommList.size()<1) { PubServers.getInstance().getMyLogInterface().myError("生成Excel文件出错:方法中列描述信息为NULL!"); return false; } //判断参数列信息是否为空 if (columnList==null || columnList.size()<1) { PubServers.getInstance().getMyLogInterface().myError("生成Excel文件出错:方法名中列信息为空!"); return false; } //判断参数中列值信息是否为空 if (valueList==null || valueList.size()<1) { PubServers.getInstance().getMyLogInterface().myError("生成Excel文件出错:方法名中列值信息为空!"); return false; } //判断参数列长度信息是否为空 if (columnInfoHash == null || columnInfoHash.size()<1) { PubServers.getInstance().getMyLogInterface().myError("生成Excel文件出错:方法中列长度信息为空!"); return false; }
//得到表头数组长度 int li_ii=0; li_ii = colCommList.size(); //得到结果集表字段长度 int li_jj=0; int li_long = 0; //得到要创建的文件名 String targetfile = args;
//创建可写入的Excel工作薄 WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile)); //创建Excel工作表 WritableSheet ws = wwb.createSheet("协同工作平台", 0); /****************设置打印方向*************/ //默认是水平打印 ws.setPageSetup(PageOrientation.LANDSCAPE.LANDSCAPE,1,1); //设置列宽度(每列为13)也可以单独定制某列宽度 String columnInfoName=""; //列名称 int findColumn=0; //查找列中表信息 String columnType=""; //列类型 String columnSizeTemp=""; int columnSize=0; //列长度 List columnInfoList=null; for(int i = 0;i < columnList.size();i++){ columnInfoName = (String)columnList.get(i); if (columnInfoName != null && !columnInfoName.equals("")) { findColumn = columnInfoName.indexOf("."); findColumn = findColumn+1; columnInfoName = columnInfoName.substring(findColumn); } columnInfoList = (LinkedList)columnInfoHash.get(columnInfoName); if (columnInfoList!=null && columnInfoList.size()>1) { columnType = (String) columnInfoList.get(0); //得到列类型 columnSizeTemp = (String)columnInfoList.get(1); if (columnSizeTemp != null) { columnSize = new Integer(columnSizeTemp).intValue();//得到列长度 } } if (columnType!=null && columnType.equals("text")) columnSize=50; if (columnSize == 0) columnSize = 20; ws.setColumnView(i,columnSize);//设置列宽度 ws.setRowView(i,20); //设置行高 } ws.setHeader("","","第 &P 页,共 &N 页"); //设置页眉 ws.setFooter("","","&D &T"); //设置页脚 //打印表头 ws.mergeCells(0, 0,li_ii, 0); //合并单元格 ws.addCell(new Label(0,0,title,wcf_title));
//for循环得到表字段文字 for (int i=0;i<li_ii;i++) { ws.addCell(new Label(i,1,(String)colCommList.get(i),wcf_centre)); } //添加数据 //定义汉字翻译类 String columnTemp=""; int column=0; for(int a=0;a<valueList.size();a++) { li_long = a+2; //当前结果集号+3 HashMap hashmap = new HashMap(); hashmap = (HashMap)valueList.get(a); for (int rs_jj=0;rs_jj<li_ii;rs_jj++) { column = ((String)columnList.get(rs_jj)).indexOf("."); column = column+1; columnTemp = ((String)columnList.get(rs_jj)).substring(column); ws.addCell(new Label(rs_jj,li_long,(String)hashmap.get(columnTemp),wcf_centre)); } }
//写入Exel工作表 wwb.write(); //关闭Excel工作薄对象 wwb.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }