java编辑excel文件内容_java对excel文件内容读写修改操作

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值