您的位置:首页 > 其它

JXL 生成EXCEL文件格式说明

2013-03-26 13:41 495 查看
JXL 生成EXCEL文件格式说明

1、 合并单元格

  WritableSheet.mergeCells(int m,int n,int p,int q);

  作用是从(m,n)到(p,q)的单元格全部合并,比如:

  WritableSheet sheet=book.createSheet(“第一页”,0);

  //合并第一列第一行到第六列第一行的所有单元格

  sheet.mergeCells(0,0,5,0);

  合并既可以是横向的,也可以是纵向的。合并后的单元格不能再次进行合并,否则会触发异常。

  2、 行高和列宽

  WritableSheet.setRowView(int i,int height);

  作用是指定第i+1行的高度,比如:

  //将第一行的高度设为200

  sheet.setRowView(0,200);

  WritableSheet.setColumnView(int i,int width);

  作用是指定第i+1列的宽度,比如:

  //将第一列的宽度设为30

  sheet.setColumnView(0,30);

//jxl
 public static String explorExcelAllProject() throws IOException, WriteException {

    		Date today = new Date();
    		String filename = EIBProperties.getProperty("cordys.home", "")
    				+ System.getProperty("file.separator") + "Web"
    				+ System.getProperty("file.separator") + "upladFiles"
    			    + System.getProperty("file.separator") + "Bidding_Document"
    				+ System.getProperty("file.separator") + "ExplorExcelAllProject_" // w
    				+ today.getTime() + ".xls";
    		String url = "/cordys" + System.getProperty("file.separator")
    				+ "upladFiles" + System.getProperty("file.separator")
    			    + "Bidding_Document" + System.getProperty("file.separator")
    				+ "ExplorExcelAllProject_" + today.getTime() + ".xls"; // w
    		File excelFile = new File(filename);
    		if (!excelFile.exists()) {
    			excelFile.createNewFile();
    		}
    		FileOutputStream os = new FileOutputStream(excelFile);
    		
    		String message = "中国商飞设计研发中心招标工作一览表";

    		WritableWorkbook wwb = Workbook.createWorkbook(os);
    		//设置表头
    		WritableSheet ws = wwb.createSheet(message, 0);
            //设置行高
    		ws.setRowView(行号,高度(200=10));
    		//设置字体
    		WritableCellFormat wcf = new WritableCellFormat();
    		wcf.setAlignment(jxl.format.Alignment.CENTRE);
    		wcf.setFont(new jxl.write.WritableFont(WritableFont.TIMES, 16,
    				WritableFont.BOLD, true));
    		wcf.setBorder(Border.TOP, BorderLineStyle.THICK, Colour.BLACK);
    		wcf.setBorder(Border.LEFT, BorderLineStyle.THICK, Colour.BLACK);
    		wcf.setBorder(Border.RIGHT, BorderLineStyle.THICK, Colour.BLACK);
    		//wcf.setBorder(Border.BOTTOM, BorderLineStyle.THICK, Colour.BLACK);
    		//设置列数
    		ws.mergeCells(0, 0, 20, 0); // 2012/7/24 7/7
    		ws.addCell(new Label(0, 0, message, wcf));
    		//表头设置
    		WritableCellFormat wcf1 = new WritableCellFormat();
    		wcf1.setFont(new jxl.write.WritableFont(WritableFont.TIMES, 10,WritableFont.BOLD, false));
    		wcf1.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
    		//wcf1.setBorder(Border.BOTTOM, BorderLineStyle.THIN, Colour.BLACK);
    		wcf1.setAlignment(jxl.format.Alignment.CENTRE);
    		//增加表头设置多少列
    		ws.addCell(new Label(0, 1, "序号", wcf1));
    		ws.addCell(new Label(1, 1, "项目名称", wcf1));
    		ws.addCell(new Label(2, 1, "批复概算(万元)", wcf1));
    		ws.addCell(new Label(3, 1, "批复面积", wcf1));
    		ws.addCell(new Label(4, 1, "招标编号", wcf1));

    		//小计和总计样式
    		WritableCellFormat wcf5 = new WritableCellFormat();
    		//设置字体红色加粗无下划线
    		wcf5.setFont(new jxl.write.WritableFont(WritableFont.TIMES, 10,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    Colour.RED));
    		wcf5.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
    		wcf5.setBackground(Colour.YELLOW);
//设置列宽
    		ws.setColumnView(0, 5);
    		ws.setColumnView(1, 15);
    		ws.setColumnView(2, 17);
    		ws.setColumnView(3, 12);
    		ws.setColumnView(4, 12);
    		wwb.write();
    		wwb.close();
    		os.close();
    		
            return url;
    }
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: