免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4271 | 回复: 0
打印 上一主题 下一主题

使用JXL JExcelApi 生成Excel文档,并且设置字体、颜色、背景等 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-07 16:16 |只看该作者 |倒序浏览
    jxl挺好用,代码学习留用!
package com.test.excel;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.BorderLineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class TestExcel {
    public static void writeExcel(File file) {
        try {
            //创建一个Excel文档
            WritableWorkbook workbook = Workbook.createWorkbook(file);
            //创建一个Sheet
            WritableSheet sheet = workbook.createSheet("Report-1", 0);
            //是否显示网格
            sheet.getSettings().setShowGridLines(true);
            //设置列宽
            sheet.getSettings().setDefaultColumnWidth(9);
            //设置行高
            sheet.getSettings().setDefaultRowHeight(500);
            Label label = new Label(1, 1, "This is a Lable");
            sheet.addCell(label);
            //设置字体
            WritableFont font = new WritableFont(WritableFont.ARIAL, 20,
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    Colour.RED);
            WritableCellFormat cFormat = new WritableCellFormat(font);
            //设置背景色
            cFormat.setBackground(Colour.LIGHT_BLUE);
            Label label2 = new Label(5, 13, "hello new Label", cFormat);
            sheet.addCell(label2);
            WritableSheet sheet2 = workbook.createSheet("report-2", 1);
            WritableCellFormat newFormat = new WritableCellFormat();
            newFormat.setBorder(Border.ALL, BorderLineStyle.DOUBLE);
            Label label3 = new Label(3, 6, "border", newFormat);
            sheet.addCell(label3);
            Formula l3 = new Formula(10, 10, "=SUM(C4:C5)");
            //公式暂未成功,本来准备计算l1与l2的和,并显示到l3,未成功
            //WritableCell cell = l3.copyTo(2, 5);
            Number l1 = new Number(2, 3, 3);
            Number l2 = new Number(2, 4, 4);
            sheet.addCell(l1);
            sheet.addCell(l2);
            //sheet.addCell(cell);
            workbook.write();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }
    public static void main(String args[]) {
        File file = new File("d:/test.xls");
        writeExcel(file);
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/21344/showart_2112944.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP