`

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

 
阅读更多

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

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

 

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

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

 

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

请看代码:

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();
	}
}

 

结果如下:



 

 

  • 大小: 48.6 KB
分享到:
评论
1 楼 贝塔ZQ 2017-07-06  
也可以试试PageOffice插件,觉得更简单点

相关推荐

Global site tag (gtag.js) - Google Analytics