81,092
社区成员
发帖
与我相关
我的任务
分享
import java.io.File;
import java.io.IOException;
import java.util.Date;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
// 老紫竹修改 www.java2000.net
public class TestJxl {
public static void main(String[] args) throws Exception {
File excel = new File("d:/aming.xls");
if (!excel.exists()) {
excel.createNewFile();
}
WritableWorkbook wwb = Workbook.createWorkbook(excel);
WritableSheet ws = wwb.createSheet("testexcel", 0);
WritableSheet ws1 = wwb.createSheet("testexce2", 1);
Label lable = null;
// 对中文的支持非常好
lable = new Label(0, 0, "我的中国心");
ws.addCell(lable);
Label lable1 = new Label(0, 0, "我的中国心2");
ws1.addCell(lable1);
// 可以定义模板格式化你的cell
WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setBackground(Colour.WHITE);
lable = new Label(0, 1, "fdsl", wcf);
lable1 = new Label(0, 1, "fds1", wcf);
ws.addCell(lable);
ws1.addCell(lable1);
wf = new WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
wcf = new WritableCellFormat(wf);
lable = new Label(0, 2, "aming", wcf);
ws.addCell(lable);
lable1 = new Label(0, 2, "aming", wcf);
ws1.addCell(lable1);
// cell的类型同样可以定义为数字类型
;
// cell的类型同样可以为日期,时间
DateTime dt = new DateTime(0, 6, new Date());
ws.addCell(dt);
lable1 = new Label(0, 2, "aming", wcf);
ws1.addCell(lable1);
// 并且可以很好格式化你的日期格式
DateFormat df = new DateFormat("MM dd yyyy hh:mm:ss");
wcf = new WritableCellFormat(df);
dt = new DateTime(0, 7, new Date(), wcf);
ws.addCell(dt);
lable1 = new Label(0, 2, "aming", wcf);
ws1.addCell(lable1);
// 开始写文件了
wwb.write();
wwb.close();
}
}