java操作Excel(从我的CSDN搬过来的)(二)

2014-11-24 11:03:46 · 作者: · 浏览: 3
}
for(int i=0;i
String person="";//报修人
if(null!=stuList.get(i).getBxr()){
person=stuList.get(i).getBxr().toString();
}
Label labelBxr = new Label(0,i+1,person);
String dept="";//所在部门
if(null!=stuList.get(i).getBm()){
dept=stuList.get(i).getBm().toString();
}
Label labelBm=new Label(1,i+1,dept);
String date="";//报修日期
if(null!=stuList.get(i).getBdate()){
date=stuList.get(i).getBdate().toString();
}
Label labelBdate=new Label(2,i+1,date);
String name="";//消耗品名称
if(null!=stuList.get(i).getExpendablesname()){
name=stuList.get(i).getExpendablesname().toString();
}
Label labelName=new Label(3,i+1,name);
String descrition="";//消耗品报修描述
if(null!=stuList.get(i).getBxzk()){
descrition=stuList.get(i).getBxzk().toString();
}
Label labelDescrition=new Label(4,i+1,descrition);
/*
* 统一添加到列表中
* */
try{
ws.addCell(labelBxr);
ws.addCell(labelBm);
ws.addCell(labelBdate);
ws.addCell(labelName);
ws.addCell(labelDescrition);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
写入数据的时候注意的格式
(1)添加的字体样式
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
WritableFont()方法里参数说明:
这个方法算是一个容器,可以放进去好多属性
第一个: TIMES是字体大小,他写的是18
第二个: BOLD是判断是否为斜体,选择true时为斜体
第三个: ARIAL
第四个: UnderlineStyle.NO_UNDERLINE 下划线
第五个: jxl.format.Colour.RED 字体颜色是红色的
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);
jxl.write.Label labelC = new jxl.write.Label(0, 0, "This is a Label cell",wcfF);
ws.addCell(labelC);
在Label()方法里面有三个参数
第一个是代表列数,
第二是代表行数,
第三个代表要写入的内容
第四个是可选项,是输入这个label里面的样式
然后通过写sheet的方法addCell()把内容写进sheet里面。
(2)添加带有formatting的Number对象
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
(3)添加Number对象
(3.1)显示number对象数据的格式
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);
jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);
Number()方法参数说明:
前两上表示输入的位置
第三个表示输入的内容
(4)添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false);
ws.addCell(labelB);
(5)添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0, 3, new java.util.Date());
ws.addCell(labelDT);
DateTime()方法的参数说明
前两个表示输入的位置
第三个表示输入的当前时间
(6)添加带有formatting的DateFormat对象
这个显示当前时间的所有信息,包括年月日小时分秒
jxl.write.DateFormat df = new jxl.write.DateFormat("dd MM yyyy hh:mm:ss");
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
jxl.write.DateTime labelDTF = new jxl.write.DateTime