您的位置:首页 > 其它

Jxl操作Excel设置背景、字体颜色、对齐方式、列的宽度(二)

2017-02-08 15:43 846 查看
package com.java.demo;  
  
import java.io.File;  
import java.io.IOException;  
import jxl.Workbook;  
import jxl.format.Alignment;  
import jxl.format.Border;  
import jxl.format.BorderLineStyle;  
import jxl.format.Colour;  
import jxl.format.UnderlineStyle;  
import jxl.format.VerticalAlignment;  
import jxl.write.Label;  
import jxl.write.WritableCellFormat;  
import jxl.write.WritableFont;  
import jxl.write.WritableSheet;  
import jxl.write.WritableWorkbook;  
import jxl.write.WriteException;  
import jxl.write.biff.RowsExceededException;  
  
/** 
 * 导出Excel实例: 
 * @author Administrator 
 * 
 */  
public class ExcelDemo {  
  
    /** 
     * @param args 
     * @throws IOException  
     * @throws WriteException  
     * @throws RowsExceededException  
     */  
    public static void main(String[] args) throws IOException, RowsExceededException, WriteException {  
        //创建Excel工作簿;  
        WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/ExcelDemo.xls"));  
  
        //创建Excel电子薄;  
        WritableSheet sheet = workbook.createSheet("第一个Sheet", 0);  
        //分别给2,3,4列设置不同的宽度;  
        sheet.setColumnView(1, 40);  
        sheet.setColumnView(1, 30);  
        sheet.setColumnView(2, 50);  
        sheet.setColumnView(3, 20);  
  
        //给sheet电子版中所有的列设置默认的列的宽度;  
        sheet.getSettings().setDefaultColumnWidth(30);  
  
        //设置字体;  
        WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);  
  
        WritableCellFormat cellFormat1 = new WritableCellFormat(font1);  
        //设置背景颜色;  
        cellFormat1.setBackground(Colour.BLUE_GREY);  
        //设置边框;  
        cellFormat1.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);  
        //设置自动换行;  
        cellFormat1.setWrap(true);  
        //设置文字居中对齐方式;  
        cellFormat1.setAlignment(Alignment.CENTRE);  
        //设置垂直居中;  
        cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);  
        //创建单元格  
        Label label1 = new Label(0, 0, "第一行第一个单元格(测试是否自动换行!)",cellFormat1);  
        Label label2 = new Label(1, 0, "第一行第二个单元格",cellFormat1);  
        Label label3 = new Label(2, 0, "第一行第三个单元格",cellFormat1);  
        Label label4 = new Label(3, 0, "第一行第四个单元格",cellFormat1);  
        //添加到行中;  
        sheet.addCell(label1);  
        sheet.addCell(label2);  
        sheet.addCell(label3);  
        sheet.addCell(label4);  
          
        //给第二行设置背景、字体颜色、对齐方式等等;  
        WritableFont font2 = new WritableFont(WritableFont.ARIAL,14,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLUE2);  
        WritableCellFormat cellFormat2 = new WritableCellFormat(font2);  
        cellFormat2.setAlignment(Alignment.CENTRE);  
        cellFormat2.setBackground(Colour.PINK);  
        cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);  
        cellFormat2.setWrap(true);  
  
        //创建单元格;  
        Label label11= new Label(0,  1, "第二行第一个单元格(测试是否自动换行!)",cellFormat2);  
        Label label22 = new Label(1, 1, "第二行第二个单元格",cellFormat2);  
        Label label33 = new Label(2, 1, "第二行第三个单元格",cellFormat2);  
        Label label44 = new Label(3, 1, "第二行第四个单元格",cellFormat2);  
  
        sheet.addCell(label11);  
        sheet.addCell(label22);  
        sheet.addCell(label33);  
        sheet.addCell(label44);  
  
        //写入Excel表格中;  
        workbook.write();  
        //关闭流;  
        workbook.close();  
    }  
}

结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐