好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

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

5页
  • 卖家[上传人]:gg****m
  • 文档编号:231822937
  • 上传时间:2021-12-29
  • 文档格式:DOC
  • 文档大小:63.84KB
  • / 5 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 使用JXL JExcelApi生成Excel文档,并且设置字体、颜色、背景等2009-05-20 11:38package com. tcst. excel;import ja\aio.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. writc. Number;import jxl. write. WritableCell;import jxl. write. Writab1eCe11Format;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文档Writab1eWorkbook workbook = Workbook. createWorkbook(file);//创建一个SheetWritabl eSheet sheet = workbook. createSheet (Z/Report-1z,, 0);//是否显示网格sheet.gctSettings()・ sctShowGridLines(true);//设置列宽sheet.getSettings (). setDefaultColumnWidth(9);〃设置行高sheet. getSettings(). setDefaultRowIleight (500);Label label = new Label (1, 1, "This is a Lablez,);sheet. addCell (label);〃设置字体WritableFont font = newWritableFont (WritableFont. ARIAL, 20, WritableFont. BOLD, false, Underlines NOJJNDERLINE, Colour. RED);WritableCellFormat cFormat 二 new WritableCellFormat(font);〃设置背景色eFormat. sctBackground (Colour. LIGI1T_BLUE);Label label2 = new Label (5, 13, "hello new LabeT7, eFormat); sheet. addCell(label2);WritableSheet sheet2 二 workbook. createSheet (/zreport-2/z, 1);WritablcCc11Format ncwFonnat 二 new WritablcCc11Format(); newFormat. setBorder(Border. ALL, BorderLineStyle. DOUBLE); Label label3 = new Label (3, 6, /zborderz/, newFormat); sheet. addCell(label3);Formula 13 = new Formula(10, 10, 〃二SUM(C4:C5)〃);//公式暂未成功,本来准备计算11与12的和,并显示到13,未成功 //WritableCell cell = 13.copyTo(2, 5);Number 11 = new Number(2, 3, 3);Number 12 = new Number(2, 4, 4);sheet.addCell (11);sheet.addCell(12); //sheet. addCel1(cel 1);workbook, write (); workbook. closeO ;} catch (lOException e) {e. printstackTrace();} catch (WriteException e) {e. printStackTrace ();public static void main(String args[]){File file = new File(z,d:/test.xls"); writeExcel(file);jxl写入excel时格式处理右对齐粗 体颜色等等//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////File tempFile=new File(z,d:/temp/output・ xls");Wr i tab1eWorkbook workbook = Workbook・createWorkbook(tempFile);Wri tabl eSheet sheet 二 workbook. createSheet (^TestCreateExcel0);//一些临时变量,用于写到excel中Label 1二null;jxl. write. Number n二null;jxl. write. DateTime d二null;//预定义的一些字体和格式,同一个Excel中最好不要有太多格式 WritableFont headerFont 二 new Writab1eFont(Writab1eFont. ARIAL ,12, Wri tableFont. BOLD,false, Underlincstyle.NO_UNDERLINE, jxl. format. Colour. BLU E);Wr i tab1eCe11Format headerFormat = new Wr i tab1eCe11Format (he aderFont);WritableFont titlcFont = new WritableFont(WritableFont. ARIAL, 10, WritableFont. N0_B0LD,false, Underlinestyle. NO UNDERLINE, jxl.format. Colour. R ED);Wr i tab1eCe11Format titleFormat 二 new WritableCellFormat (tit 1eFont);WritableFont detFont = new WritableFont(WritableFont. ARIAL,10, WritableFont. N0_B0LD,false, Underlinestyle. NO_UNDERLINE, jxl. format. Colour. BLA CK);WritableCe11Format detFormat 二 new WritableCellFormat (detFo nt);NumberFormat nf二new NumberFormat (zz0. 00000〃); //用于 Number 的格 式WritablcCc11Format priccFormat 二 new Wr i tab1cCc11Format (det Font, nf);DateFormat df二new DateFormat (〃yyyy-MM-dd〃);//用于日期的Wri tab 1 eCel IFormeit dateFormeit = new Wri tab 1 eCel IFormeit (detF ont, df);//剩下的事情,就是用上面的内容和格式创建一些单元格,再加到sheet 中1 二new Label (0, 0, 〃用于测试的 Excel 文件〃,headerFormat);sheet. addCel 1(1);1二new Label(column++, 2,〃标题〃,titleFormat);sheet. addCel 1(1);1二new Label(column++, sheet・ acldCell (1);2,〃口 期〃,titlcFormat);l=new Label(column++, sheet. addCel1 (1);2,〃货币〃,titleFormat);l=new Label(column++, sheet. addCcll (1);2,〃价格〃,titleFormdt);//add detailint i二0;column二0;1 二 new Label(column++, sheet・ acldCell (1);i+3,〃标题〃+i, detFormat);d=new DateTime(column++, i+3, newjava. util. Date(),at);sheet. addCel1 (d);1二new Label(column++,i+3,〃CNY〃,dctFormat);dateForm//add Title int column二0;sheet. acldCell (1);n=new jxl. write. Number(column++, i+3, 5.678, priceFormat);sheet. addCel1 (n);i++;column=0;1 二new Label (column++, i+3, 〃标题 〃+i, detFormat);sheet. addCel 1(1);d=new DateTime(column++, i+3, new java. util. Date(), dateForm at);sheet. addCell (d); l=new Label(column++, i+3, 〃SGD〃, detFormat); sheet. addCell (1);n=new jxl. write. Numbet(column++, i+3, 98832, priceFormat);sheet. addCell (n);//设置列的宽度column=0; sheet. setColumnView(column++, 20);sheet, setColumnView(column++, 20); sheet.setColumnV i ew(column++, 10); shect. setColumnVicw(column++, 20);workbook, write ();workbook, close ();我正在做的项目里定义了很多样式,好像速度也不算慢,还有一些其他方面的定 义,补充到下面://构造格式:ARIAL字体、10号、粗体、非斜体、无。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.