package com.execl;
import java.io.File;
import java.io.IOException;
import jdk.nashorn.api.tree.NewTree;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class modifyExecl {
public static void main(String[] args) throws BiffException, IOException, WriteException {
// TODO Auto-generated method stub
//修改execl
changeexecl("D:\\学习笔记\\linux\\test1.xls",0, 0, "xiugai");
//读取execl
readexecl("D:\\学习笔记\\linux\\test1.xls", 1, 1);
//创建execl
createxecl("D:\\学习笔记\\linux\\test2.xls", 0, 0, "username");
}
private static void changeexecl(String filename,int n,int r,String string ) throws BiffException, IOException, WriteException {
// TODO Auto-generated method stub
File file=new File(filename);
//打开execl文件
Workbook rwb = Workbook.getWorkbook(file);
//打开一个文本的副本,并指定数据写回到源文件
WritableWorkbook wwb = Workbook.createWorkbook(file, rwb);
//获取工作表
WritableSheet sheet = wwb.getSheet(0);
Label lable=null;
lable=new Label(n, r, string);
sheet.addCell(lable);
System.out.println(sheet.getCell(r, n).getContents());
wwb.write();
wwb.close();
rwb.close();
}
private static void readexecl(String filename,int n,int r) throws BiffException, IOException {
Workbook wk=Workbook.getWorkbook(new File(filename));
Sheet sheet0=wk.getSheet(0);
System.out.println(sheet0.getRows());
System.out.println(sheet0.getColumns());
Cell cell=null;
cell=sheet0.getCell(n,r);
System.out.print(cell.getContents());
}
private static void createxecl(String filename,int n,int r,String string) throws BiffException, IOException, WriteException {
File file=new File(filename);
file.createNewFile();
//创建工作簿
WritableWorkbook workbook=Workbook.createWorkbook(file);
//创建sheet
WritableSheet sheet=workbook.createSheet("用户管理", 0);
Label lable=null;
lable=new Label(n, r, string);
sheet.addCell(lable);
workbook.write();
workbook.close();
}
}