收藏 分销(赏)

JXL设置字体、颜色、背景等.doc

上传人:仙人****88 文档编号:11786947 上传时间:2025-08-13 格式:DOC 页数:6 大小:104.50KB 下载积分:10 金币
下载 相关 举报
JXL设置字体、颜色、背景等.doc_第1页
第1页 / 共6页
JXL设置字体、颜色、背景等.doc_第2页
第2页 / 共6页


点击查看更多>>
资源描述
使用JXL JExcelApi 生成Excel文档,并且设置字体、颜色、背景等 2009-05-20 11:38 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); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// jxl写入excel时格式处理 右对齐 粗体颜色等等 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// File tempFile=new File("d:/temp/output.xls"); WritableWorkbook workbook = Workbook.createWorkbook(tempFile);    WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);        //一些临时变量,用于写到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);          NumberFormat nf=new NumberFormat("0.00000"); //用于Number的格式     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 java.util.Date(), dateFormat);     sheet.addCell(d);     l=new Label(column++, i+3, "CNY", detFormat);     sheet.addCell(l);     n=new jxl.write.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 java.util.Date(), dateFormat);     sheet.addCell(d);     l=new Label(column++, i+3, "SGD", detFormat);     sheet.addCell(l);     n=new jxl.write.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();    我正在做的项目里定义了很多样式,好像速度也不算慢,还有一些其他方面的定义,补充到下面:     //构造格式:ARIAL字体、10号、粗体、非斜体、无下划线、黑色     WritableFont fmtx2TotalCaption = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,         false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);      WritableCellFormat totalx2Format = new WritableCellFormat(fmtx2TotalCaption);    //文字垂直居中对齐     totalx2Format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);     //文字水平居中对齐      totalx2Format.setAlignment(jxl.format.Alignment.CENTRE);        //边框深蓝色     totalx2Format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN,         jxl.format.Colour.DARK_BLUE);     //设置底色为冰蓝     totalx2Format.setBackground(jxl.format.Colour.ICE_BLUE);        sheet.mergeCells(0, row, 8, row);  //合并单元格,row 到 row 行,0 到 8 列     sheet.setRowView(row, 600);// 第 row 行的高度 另外就是要在 Action(servlet) 中这么写,IE就会直接弹出保存文件的对话框(注意要占用当前窗口):     String title = "XXXX统计";     OutputStream out = response.getOutputStream();     WritableWorkbook wb = Workbook.createWorkbook(out);     response.setContentType("aplication/vnd.ms-excel")s;     response.addHeader("Content-Disposition","inline; filename=" + new String(title.getBytes("GB2312"),"ISO8859_1") + ".xls");  //有中文必须转码
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服