java Jxl 操作Excel(三)

2014-11-24 03:29:11 · 作者: · 浏览: 6

// 在B32处添加图片,图片大小占10行3列,只支持png格式
File file = new File("d:\\shu05.png");
WritableImage image = new WritableImage(1, 31, 3, 10, file);
sheet.addImage(image);

// 在A44出添加内容"Added drop down validation",并为其添加注释
label = new Label(0, 43, "Added drop down validation");
wcfeatures = new WritableCellFeatures();
wcfeatures.setComment("右边列是个下拉列表");
label.setCellFeatures(wcfeatures);
sheet.addCell(label);

// 在B44处添加一个下拉列表并添加注释
Blank b = new Blank(1, 43);
wcfeatures = new WritableCellFeatures();
ArrayList al = new ArrayList();
al.add("why");
al.add("landor");
al.add("tjm");
wcfeatures.setDataValidationList(al);
wcfeatures.setComment("这是一个注释");
b.setCellFeatures(wcfeatures);
sheet.addCell(b);

// 为A46添加注释。
// 此处比较麻烦,试了多次发现必须将cell强制类型转换、添加CellFeatures再修改注释才可用,不知有没有更好的办法。
cell = sheet.getWritableCell(0,45);
wcfeatures = new WritableCellFeatures();
wcfeatures.setComment("这个注释不会被显示,删了这行还不行,MD");
cell.setCellFeatures(wcfeatures);

label = (Label) cell;
// label.setCellFeatures(wcfeatures);// 直接这样写会报一个警告(“注释已存在”),但那个注释仍会被显示。
label.addCellFeatures();
label.getWritableCellFeatures().setComment("终于加上注释了,哈哈哈哈");


// if (cell instanceof Number) {
// Number num = (Number) cell;
// num.setCellFeatures(wcfeatures);
// } else if (cell instanceof jxl.write.Boolean) {
// jxl.write.Boolean bool = (jxl.write.Boolean) cell;
// bool.setCellFeatures(wcfeatures);
// } else if (cell instanceof jxl.write.DateTime) {
// jxl.write.DateTime dt = (jxl.write.DateTime) cell;
// dt.setCellFeatures(wcfeatures);
// } else {
// Label _label = (Label) cell;
// _label.setCellFeatures(wcfeatures);
// }

workbook.write();
workbook.close();
wb.close();
}

}

http://www.andykhan.com/jexcelapi/download.html Jxl--jar包下载地址

摘自 那年那月那天