首页 > 技术文章 > Jxl操作Excel设置背景、字体颜色、对齐方式、列的宽度(转)

m3Lee 2014-04-18 17:52 原文

原文出处:http://zhouhaitao.iteye.com/blog/1842769

 

最近项目中需要用到导出Excel文件,以下是我写了一个通过jxl操作Excel的例子,熟悉Jxl的使用。

有一个比较难以处理的问题就是自动适应文本宽度的问题。

 

以下我也在网上找了一下 :有如下的方式处理:

CellView cellView = new CellView();  cellView.setAutosize(true); //设置自动大小  
sheet.setColumnView(1, cellView);//根据内容自动设置列宽  
label = new Label(1, 0, "单元格内容.");  
sheet.addCell(label);  

以下是Excel的基本操作:设置背景、字体颜色、对齐方式、列的宽度等等:

请看代码:

 1 package com.java.demo;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import jxl.Workbook;
 6 import jxl.format.Alignment;
 7 import jxl.format.Border;
 8 import jxl.format.BorderLineStyle;
 9 import jxl.format.Colour;
10 import jxl.format.UnderlineStyle;
11 import jxl.format.VerticalAlignment;
12 import jxl.write.Label;
13 import jxl.write.WritableCellFormat;
14 import jxl.write.WritableFont;
15 import jxl.write.WritableSheet;
16 import jxl.write.WritableWorkbook;
17 import jxl.write.WriteException;
18 import jxl.write.biff.RowsExceededException;
19 
20 /**
21  * 导出Excel实例:
22  * @author Administrator
23  *
24  */
25 public class ExcelDemo {
26 
27     /**
28      * @param args
29      * @throws IOException 
30      * @throws WriteException 
31      * @throws RowsExceededException 
32      */
33     public static void main(String[] args) throws IOException, RowsExceededException, WriteException {
34         //创建Excel工作簿;
35         WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/ExcelDemo.xls"));
36 
37         //创建Excel电子薄;
38         WritableSheet sheet = workbook.createSheet("第一个Sheet", 0);
39         //分别给2,3,4列设置不同的宽度;
40         sheet.setColumnView(1, 40);
41         sheet.setColumnView(1, 30);
42         sheet.setColumnView(2, 50);
43         sheet.setColumnView(3, 20);
44 
45         //给sheet电子版中所有的列设置默认的列的宽度;
46         sheet.getSettings().setDefaultColumnWidth(30);
47 
48         //设置字体;
49         WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
50 
51         WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
52         //设置背景颜色;
53         cellFormat1.setBackground(Colour.BLUE_GREY);
54         //设置边框;
55         cellFormat1.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);
56         //设置自动换行;
57         cellFormat1.setWrap(true);
58         //设置文字居中对齐方式;
59         cellFormat1.setAlignment(Alignment.CENTRE);
60         //设置垂直居中;
61         cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
62         //创建单元格
63         Label label1 = new Label(0, 0, "第一行第一个单元格(测试是否自动换行!)",cellFormat1);
64         Label label2 = new Label(1, 0, "第一行第二个单元格",cellFormat1);
65         Label label3 = new Label(2, 0, "第一行第三个单元格",cellFormat1);
66         Label label4 = new Label(3, 0, "第一行第四个单元格",cellFormat1);
67         //添加到行中;
68         sheet.addCell(label1);
69         sheet.addCell(label2);
70         sheet.addCell(label3);
71         sheet.addCell(label4);
72         
73         //给第二行设置背景、字体颜色、对齐方式等等;
74         WritableFont font2 = new WritableFont(WritableFont.ARIAL,14,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLUE2);
75         WritableCellFormat cellFormat2 = new WritableCellFormat(font2);
76         cellFormat2.setAlignment(Alignment.CENTRE);
77         cellFormat2.setBackground(Colour.PINK);
78         cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);
79         cellFormat2.setWrap(true);
80 
81         //创建单元格;
82         Label label11= new Label(0,  1, "第二行第一个单元格(测试是否自动换行!)",cellFormat2);
83         Label label22 = new Label(1, 1, "第二行第二个单元格",cellFormat2);
84         Label label33 = new Label(2, 1, "第二行第三个单元格",cellFormat2);
85         Label label44 = new Label(3, 1, "第二行第四个单元格",cellFormat2);
86 
87         sheet.addCell(label11);
88         sheet.addCell(label22);
89         sheet.addCell(label33);
90         sheet.addCell(label44);
91 
92         //写入Excel表格中;
93         workbook.write();
94         //关闭流;
95         workbook.close();
96     }
97 }

结果如下:

推荐阅读