標籤:
package com.email.jav;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableHyperlink;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class TestExl {
public static void main(String args[]) throws IOException,
RowsExceededException, WriteException {
WritableWorkbook book = Workbook.createWorkbook(new File("f://aa.xls")); // 第一步
/**
* 定義與設定Sheet
*/
WritableSheet sheet = book.createSheet("sheet", 0); // 建立Sheet
sheet.setColumnView(0, 30); // 設定列的寬度
sheet.setColumnView(1, 30); // 設定列的寬度
sheet.setColumnView(2, 30); // 設定列的寬度
sheet.setRowView(6, 1000); // 設定行的高度
sheet.setRowView(4, 1000); // 設定行的高度
sheet.setRowView(5, 1000); // 設定行的高度
/**
* 定義儲存格樣式
*/
WritableFont wf = new WritableFont(WritableFont.ARIAL, 15,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.CORAL); // 定義格式 字型 底線 斜體 粗體 顏色
WritableCellFormat wcf = new WritableCellFormat(wf); // 儲存格定義
wcf.setBackground(jxl.format.Colour.BLACK); // 設定儲存格的背景顏色
wcf.setAlignment(jxl.format.Alignment.CENTRE); // 設定對齊
/**
* 使用樣式的儲存格
*/
sheet.addCell(new Label(0, 0, "郵箱asasasasa", wcf)); // 普通的帶有定義格式的儲存格
sheet.addCell(new Label(1, 0, "動作asasasasa", wcf));
sheet.addCell(new Label(2, 0, "時間asasasasa", wcf));
sheet.addCell(new Label(0, 1, "[email protected]"));
sheet.addCell(new Label(1, 1, "action"));
sheet.addCell(new Label(2, 1, "time"));
/**
* excel合併儲存格
*/
sheet.addCell(new Label(4, 0, "合併儲存格", wcf)); // 合併儲存格
sheet.addCell(new Label(4, 1, "測試1"));
sheet.addCell(new Label(5, 1, "測試2"));
sheet.addCell(new Label(6, 1, "測試3"));
sheet.mergeCells(4, 0, 6, 0); // 合併儲存格
/**
* excel圖片
*/
sheet.addCell(new Label(0, 3, "展示圖片 jxl只支援png格式的", wcf)); // 展示圖片標題
sheet.mergeCells(0, 3, 3, 3); // 合并圖片標題儲存格
File file = new File("f://1.png"); // 獲得圖片
WritableImage image = new WritableImage(0, 4, 3, 3, file); // 設定圖片顯示位置
// //0代表第幾列,4代表第幾行,3代表圖片的高和寬佔3個儲存格
sheet.addImage(image); // 載入圖片
/**
* excel連結
*/
sheet.addCell(new Label(0, 8, "excel連結測試", wcf)); // 連結標題
sheet.mergeCells(0, 8, 2, 8);
WritableHyperlink link = new WritableHyperlink(0, 9, new URL(
"http://www.google.com"));
link.setDescription("連結使用 連結到Google");
sheet.mergeCells(0, 9, 1, 9);
sheet.addHyperlink(link);
book.write();
book.close();
}
}
轉載地址:http://blog.csdn.net/is_lie/article/details/6276361
【轉載】jxl操作excel 字型 背景色 合併儲存格 列寬等 .