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")); // 第一步
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, ""));
sheet.addCell(new Label(1, 1, "action"));
sheet.addCell(new Label(2, 1, "time"));
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); // 合併單元格
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); // 設置圖片顯示位置
// 4,4代表圖片的高和寬佔4個單元格
sheet.addImage(image); // 加載圖片
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();
}
}