packagedomain;importjava.io.File;importjava.io.OutputStream;importjxl.Workbook;importjxl.format.UnderlineStyle;importjxl.write.Boolean;importjxl.write.DateFormat;importjxl.write.DateTime;importjxl.write.Label;importjxl.write.Number;importjxl.write.NumberFormat;importjxl.write.WritableCellFormat;importjxl.write.WritableFont;importjxl.write.WritableImage;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;public classWriteadd {/**输出Excel*/
public voidwriteExcel(OutputStream os)
{try{
WritableWorkbook wwb=Workbook.createWorkbook(os);//创建Excel工作表 指定名称和位置
WritableSheet ws= wwb.createSheet("Test Sheet 1", 0);/************** 往工作表中添加数据 *****************/
//1.添加Label对象
Label label= new Label(5, 0, "测试");
Label labe2= new Label(6, 0, "20163432");
Label labe3= new Label(7, 0, "20163432");
ws.addCell(label);
ws.addCell(labe2);
ws.addCell(labe3);//添加带有字型Formatting对象
WritableFont wf= new WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
WritableCellFormat wcf= newWritableCellFormat(wf);
Label labelcf= new Label(1, 0, "hello word !", wcf);
ws.addCell(labelcf);//添加带有字体颜色的Formatting对象
WritableFont wfc= new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.DARK_YELLOW);
WritableCellFormat wcfFC= newWritableCellFormat(wfc);
Label labelCF= new Label(1, 0, "石家庄铁道大学软件工程系", wcfFC);
ws.addCell(labelCF);//2.添加Number对象
Number labelN= new Number(0, 1, 3.1415926);
ws.addCell(labelN);//添加带有formatting的Number对象
NumberFormat nf= new NumberFormat("#.##");
WritableCellFormat wcfN= newWritableCellFormat(nf);
Number labelNF= new jxl.write.Number(1, 1, 3.1415926, wcfN);
ws.addCell(labelNF);//3.添加Boolean对象
Boolean labelB= new jxl.write.Boolean(0, 2, true);
ws.addCell(labelB);
Boolean labelB1= new jxl.write.Boolean(1, 2, false);
ws.addCell(labelB1);//4.添加DateTime对象
jxl.write.DateTime labelDT= new jxl.write.DateTime(0, 3, newjava.util.Date());
ws.addCell(labelDT);//5.添加带有formatting的DateFormat对象
DateFormat df= new DateFormat("dd MM yyyy hh:mm:ss");
WritableCellFormat wcfDF= newWritableCellFormat(df);
DateTime labelDTF= new DateTime(1, 3, newjava.util.Date(), wcfDF);
ws.addCell(labelDTF);//6.添加图片对象,jxl只支持png格式图片
File image= new File("C:\\Users\\lenovo\\Desktop\\timg.png");
WritableImage wimage= new WritableImage(6, 4, 4, 20, image);
ws.addImage(wimage);//7.写入工作表
wwb.write();
wwb.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}