导出数据到Excel文件中

导出数据到Excel文件中:

	
    /**
     * 导出数据到Excel文件中
     * 下面的例子,设置了数字、日期的格式,还有字体,颜色等。
     * @throws Exception
     * @throws WriteException
     */
    public void test() throws Exception, WriteException{
    	
        // 创建指定名称的Excel
    	WritableWorkbook workbook = Workbook.createWorkbook(new File("d:/ExcelTest/output.xls"));
    	// 创建工作表
    	WritableSheet sheet = workbook.createSheet("TestCreateExcel", 1);

    	//一些临时变量,用于写到excel中
    	Label l=null;
    	jxl.write.Number n=null;
    	jxl.write.DateTime d=null;

    	//预定义的一些字体和格式,同一个Excel中最好不要有太多格式
    	WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD,false,                    
    			UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLUE);
    	WritableCellFormat headerFormat = new WritableCellFormat(headerFont);

    	// 设置字体和格式
    	WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
    			UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
    	WritableCellFormat titleFormat = new WritableCellFormat(titleFont);

        // 设置字体和格式
    	WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10,WritableFont.NO_BOLD, false, 
    			UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
    	WritableCellFormat detFormat = new WritableCellFormat(detFont);

    	
    	//用于Number的格式
    	NumberFormat nf=new NumberFormat("0.00000");  
    	WritableCellFormat priceFormat = new WritableCellFormat(detFont, nf);

    	// 用于日期的
    	DateFormat df=new DateFormat("yyyy-MM-dd");
    	WritableCellFormat dateFormat = new WritableCellFormat(detFont, df);

    	//剩下的事情,就是用上面的内容和格式创建一些单元格,再加到sheet中
    	l=new Label(0, 0, "用于测试的Excel文件", headerFormat);
    	sheet.addCell(l);
    	//add Title
    	int column=0;
    	l=new Label(column++, 2, "标题", titleFormat);
    	sheet.addCell(l);
    	l=new Label(column++, 2, "日期", titleFormat);
    	sheet.addCell(l);
    	l=new Label(column++, 2, "货币", titleFormat);
    	sheet.addCell(l);
    	l=new Label(column++, 2, "价格", titleFormat);
    	sheet.addCell(l);

    	//add detail
    	int i=0;
    	column=0;
    	l=new Label(column++, i+3, "标题 "+i, detFormat);
    	sheet.addCell(l);
    	d=new DateTime(column++, i+3, new Date(), dateFormat);
    	sheet.addCell(d);
    	l=new Label(column++, i+3, "CNY", detFormat);
    	sheet.addCell(l);
    	n=new Number(column++, i+3, 5.678, priceFormat);
    	sheet.addCell(n);

    	i++;
    	column=0;
    	l=new Label(column++, i+3, "标题 "+i, detFormat);
    	sheet.addCell(l);
    	d=new DateTime(column++, i+3, new Date(),dateFormat);
    	sheet.addCell(d);
    	l=new Label(column++, i+3, "SGD", detFormat);
    	sheet.addCell(l);
    	n=new Number(column++, i+3, 98832,priceFormat);
    	sheet.addCell(n);

    	//设置列的宽度
    	column=0;
    	sheet.setColumnView(column++, 20);
    	sheet.setColumnView(column++, 20);
    	sheet.setColumnView(column++, 10);
    	sheet.setColumnView(column++, 20);

    	workbook.write();
    	workbook.close();
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值