public class CreateExcel {
@Test
public void test() {
// 创建xml
try {
WritableWorkbook book = Workbook.createWorkbook(new File(
"ahkoun.xls"));
// 生成第一页工作表,"第一页"表示工作表的名称,参数0代表第一页,
WritableSheet sheet1 = book.createSheet("第一页", 0);
// 生成第二页工作表
CellView rv=new CellView();
rv.setSize(15*256);
sheet1.setRowView(18,2000);
sheet1.setColumnView(18,rv);
WritableSheet sheet2 = book.createSheet("第二页", 1);
// 创建label类型的单元格,arg0表示列,arg1表示行,arg3为单元格存储的内容
Label label = new Label(0, 0, "label类型的单元格");
// 往第一页工作表添加单元格
sheet1.addCell(label);
//设置列的宽度
// 创建保存数字的单元格,Number类为jxl.write.Number,非java.lang中的Number
Number number = new Number(1, 0, 121);
sheet1.addCell(number);
//添加格式化的number单元格
NumberFormat nf=new NumberFormat("############.####");
WritableCellFormat nfCellFormat=new WritableCellFormat(nf);
Number f_number =new Number(12,0,232323.3456654);
sheet1.mergeCells(12,0, 13,0);
sheet1.addCell(f_number);
//添加boolean单元格
jxl.write.Boolean b=new jxl.write.Boolean(14,0,true);
sheet1.addCell(b);
//添加超链接单元格
WritableHyperlink link_cell=new WritableHyperlink(2,0,2,0,new URL("http://site.baidu.com"),"百度网址导航");
sheet1.addHyperlink(link_cell);
//添加格式化日期的单元格
jxl.write.DateFormat dateFormat=new jxl.write.DateFormat("yyyy-dd-MM hh:mm:ss");
WritableCellFormat dateFormatCell=new WritableCellFormat(dateFormat);
DateTime dtime=new DateTime(10,0,new GregorianCalendar(1988,06,17,11,2,45).getTime(),dateFormatCell);
sheet1.mergeCells(10,0, 11,0);
sheet1.addCell(dtime);
/*
* 创建格式化的label类型单元格
*/
// 创建格式化字体---自带字体.arg0字体,arg1字体大小,arg2是否加粗,arg3是否为斜体,arg4是否有下划线,arg5字体颜色
WritableFont font = new WritableFont(WritableFont.TIMES, 14,
WritableFont.NO_BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLUE);
// 创建单元格样式
WritableCellFormat format = new WritableCellFormat(font);
//设置背景颜色
format.setBackground(jxl.format.Colour.RED);
// 设置水平对齐样式
format.setAlignment(jxl.format.Alignment.CENTRE);
// 设置垂直对齐样式
format.setVerticalAlignment(jxl.format.VerticalAlignment.JUSTIFY);
Label format_label = new Label(1, 1, "格式的label", format);
sheet1.addCell(format_label);
// 创建自定义字体
WritableFont self_Font = new WritableFont(
WritableFont.createFont("华文彩云"), 14, WritableFont.NO_BOLD);
WritableCellFormat self_font_format = new WritableCellFormat(
self_Font);
self_font_format.setAlignment(jxl.format.Alignment.JUSTIFY);
Label self_font_label = new Label(0, 2, "自定义字体的label",
self_font_format);
sheet1.addCell(self_font_label);
// 合并单元格(多列),arg0,arg2代表列,arg1,arg3代表行
sheet1.mergeCells(3, 0, 9, 0);
Label meLabel = new Label(3, 0, "合并多列的单元格");
sheet1.addCell(meLabel);
// Label test1=new Label(4,0,"合并右");//这样插入将出现错误,单元格已被合并
Label test1 = new Label(1, 0, "合并右");
sheet1.addCell(test1);
// 合并单元格(多行),arg0,arg2代表列,arg1,arg3代表行
WritableCellFormat dh_format = new WritableCellFormat();
dh_format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
sheet1.mergeCells(3, 1, 3, 5);
Label dh_label = new Label(3, 1, "多行label", dh_format);
sheet1.addCell(dh_label);
Label test2 = new Label(3, 6, "多行label下");
sheet1.addCell(test2);
// 合并单元格(多行,多列),arg0,arg2代表列,arg1,arg3代表行
WritableCellFormat dh_dl_format = new WritableCellFormat();
dh_dl_format.setAlignment(jxl.format.Alignment.CENTRE);
dh_dl_format
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
sheet1.mergeCells(10, 7, 14, 14);
Label dh_dl_label = new Label(10, 7, "多行多列label", dh_dl_format);
sheet1.addCell(dh_dl_label);
book.write();
book.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Test
public void test() {
// 创建xml
try {
WritableWorkbook book = Workbook.createWorkbook(new File(
"ahkoun.xls"));
// 生成第一页工作表,"第一页"表示工作表的名称,参数0代表第一页,
WritableSheet sheet1 = book.createSheet("第一页", 0);
// 生成第二页工作表
CellView rv=new CellView();
rv.setSize(15*256);
sheet1.setRowView(18,2000);
sheet1.setColumnView(18,rv);
WritableSheet sheet2 = book.createSheet("第二页", 1);
// 创建label类型的单元格,arg0表示列,arg1表示行,arg3为单元格存储的内容
Label label = new Label(0, 0, "label类型的单元格");
// 往第一页工作表添加单元格
sheet1.addCell(label);
//设置列的宽度
// 创建保存数字的单元格,Number类为jxl.write.Number,非java.lang中的Number
Number number = new Number(1, 0, 121);
sheet1.addCell(number);
//添加格式化的number单元格
NumberFormat nf=new NumberFormat("############.####");
WritableCellFormat nfCellFormat=new WritableCellFormat(nf);
Number f_number =new Number(12,0,232323.3456654);
sheet1.mergeCells(12,0, 13,0);
sheet1.addCell(f_number);
//添加boolean单元格
jxl.write.Boolean b=new jxl.write.Boolean(14,0,true);
sheet1.addCell(b);
//添加超链接单元格
WritableHyperlink link_cell=new WritableHyperlink(2,0,2,0,new URL("http://site.baidu.com"),"百度网址导航");
sheet1.addHyperlink(link_cell);
//添加格式化日期的单元格
jxl.write.DateFormat dateFormat=new jxl.write.DateFormat("yyyy-dd-MM hh:mm:ss");
WritableCellFormat dateFormatCell=new WritableCellFormat(dateFormat);
DateTime dtime=new DateTime(10,0,new GregorianCalendar(1988,06,17,11,2,45).getTime(),dateFormatCell);
sheet1.mergeCells(10,0, 11,0);
sheet1.addCell(dtime);
/*
* 创建格式化的label类型单元格
*/
// 创建格式化字体---自带字体.arg0字体,arg1字体大小,arg2是否加粗,arg3是否为斜体,arg4是否有下划线,arg5字体颜色
WritableFont font = new WritableFont(WritableFont.TIMES, 14,
WritableFont.NO_BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLUE);
// 创建单元格样式
WritableCellFormat format = new WritableCellFormat(font);
//设置背景颜色
format.setBackground(jxl.format.Colour.RED);
// 设置水平对齐样式
format.setAlignment(jxl.format.Alignment.CENTRE);
// 设置垂直对齐样式
format.setVerticalAlignment(jxl.format.VerticalAlignment.JUSTIFY);
Label format_label = new Label(1, 1, "格式的label", format);
sheet1.addCell(format_label);
// 创建自定义字体
WritableFont self_Font = new WritableFont(
WritableFont.createFont("华文彩云"), 14, WritableFont.NO_BOLD);
WritableCellFormat self_font_format = new WritableCellFormat(
self_Font);
self_font_format.setAlignment(jxl.format.Alignment.JUSTIFY);
Label self_font_label = new Label(0, 2, "自定义字体的label",
self_font_format);
sheet1.addCell(self_font_label);
// 合并单元格(多列),arg0,arg2代表列,arg1,arg3代表行
sheet1.mergeCells(3, 0, 9, 0);
Label meLabel = new Label(3, 0, "合并多列的单元格");
sheet1.addCell(meLabel);
// Label test1=new Label(4,0,"合并右");//这样插入将出现错误,单元格已被合并
Label test1 = new Label(1, 0, "合并右");
sheet1.addCell(test1);
// 合并单元格(多行),arg0,arg2代表列,arg1,arg3代表行
WritableCellFormat dh_format = new WritableCellFormat();
dh_format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
sheet1.mergeCells(3, 1, 3, 5);
Label dh_label = new Label(3, 1, "多行label", dh_format);
sheet1.addCell(dh_label);
Label test2 = new Label(3, 6, "多行label下");
sheet1.addCell(test2);
// 合并单元格(多行,多列),arg0,arg2代表列,arg1,arg3代表行
WritableCellFormat dh_dl_format = new WritableCellFormat();
dh_dl_format.setAlignment(jxl.format.Alignment.CENTRE);
dh_dl_format
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
sheet1.mergeCells(10, 7, 14, 14);
Label dh_dl_label = new Label(10, 7, "多行多列label", dh_dl_format);
sheet1.addCell(dh_dl_label);
book.write();
book.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}