写excel(二)

        String path = getServletContext().getRealPath("/excel");
        // Method1: 创建可写入excel的工作簿
        WritableWorkbook ww = Workbook.createWorkbook(new File(path+"/abc.xls"));
        // Method2: 直接写入到工作流
        // OutputStream os = new FileOutputStream("abc.xls");
        // WritableWorkbook ww = Workbook.createWorkbook(os);
        // 创建Excel工作表
        WritableSheet ws = ww.createSheet("Test Sheet 1", 0);
        // 添加Label对象
        try {
            //1. 添加Label对象
            Label label0 = new Label(0,0,"This is Label cell");
            ws.addCell(label0);
            // 创建一个Times类型,18号,粗体,斜字体
            WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
            WritableCellFormat wcf = new WritableCellFormat(wf);
            Label label1 = new Label(0,1,"This is lable cell font format",wcf);
            ws.addCell(label1);
            // 添加带有字体颜色 Formatting 的对象
            WritableFont wf2 = new WritableFont(WritableFont.ARIAL, 10,WritableFont.NO_BOLD, false,
                                   UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
            WritableCellFormat wcf2 = new WritableCellFormat(wf2);
            Label label2 = new jxl.write.Label(1, 0, "This is a Label Cell", wcf2);
            ws.addCell(label2);
            //2. 添加 Number 对象
            jxl.write.Number labelN = new jxl.write.Number(0, 1, 3.1415926);
            ws.addCell(labelN);
            // 添加带有 formatting 的 Number 对象
            jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
            jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
            jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
            ws.addCell(labelNF);
            //3. 添加 Boolean 对象
            jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
            ws.addCell(labelB);
            //4. 添加 DateTime 对象
            jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
            ws.addCell(labelDT);
            // 添加带有 formatting 的 DateFormat 对象
            jxl.write.DateFormat df = new jxl.write.DateFormat("dd MM yyyy hh:mm:ss");
            jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
            jxl.write.DateTime labelDTF = new jxl.write.DateTime(1, 3, new java.util.Date(),
            wcfDF);
            ws.addCell(labelDTF);
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        } finally{
            if(ww != null){
                try {
                    ww.close();
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值