java 操作execl


package tools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Excel {

public static void main(String[] args) {
File file = new File("src/tools/test.xls");
try {
InputStream is = new FileInputStream(file);
Workbook wb = Workbook.getWorkbook(is);
Sheet s = wb.getSheet("insert");
Cell c = s.getCell(0, 0);
System.out.println(c.getContents());

WritableWorkbook wwb = Workbook.createWorkbook(new FileOutputStream(file));
WritableSheet ws = wwb.createSheet("insert", 0);
//**************往工作表中添加数据*****************

//1.添加Label对象
Label label = new Label(0,0,"this is a label test");
ws.addCell(label);

//添加带有字型Formatting对象
WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
WritableCellFormat wcf = new WritableCellFormat(wf);
Label labelcf = new Label(0,1,"this is a label test",wcf);
ws.addCell(labelcf);

//添加带有字体颜色的Formatting对象
WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,
UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label labelCF = new Label(0,2,"This is a Label Cell",wcfFC);
ws.addCell(labelCF);

//2.添加Number对象
jxl.write.Number labelN = new jxl.write.Number(0,3,3.1415926);
ws.addCell(labelN);

//添加带有formatting的Number对象
NumberFormat nf = new NumberFormat("#.##");
WritableCellFormat wcfN = new WritableCellFormat(nf);
jxl.write.Number labelNF = new jxl.write.Number(0,4,3.1415926,wcfN);
ws.addCell(labelNF);

//3.添加Boolean对象
jxl.write.Boolean labelB = new jxl.write.Boolean(0,5,false);
ws.addCell(labelB);

//4.添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,6,new java.util.Date());
ws.addCell(labelDT);

//添加带有formatting的DateFormat对象
DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
WritableCellFormat wcfDF = new WritableCellFormat(df);
DateTime labelDTF = new DateTime(0,7,new java.util.Date(),wcfDF);
ws.addCell(labelDTF);

//添加图片对象,jxl只支持png格式图片
File image = new File("f:/2.png");
WritableImage wimage = new WritableImage(0,8,2,2,image);//0,1分别代表x,y.2,2代表宽和高占的单元格数
ws.addImage(wimage);
//写入工作表
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值