Manifest-Version: 1.0
Ant-Version: Apache Ant 1.5.3
Created-By: 1.4.2_02-b03 (Sun Microsystems Inc.)
Main-Class: jxl.demo.Demo
import java.io.File;
import java.io.IOException;
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;
import jxl.write.biff.Row***ceededException;
/**
* @author roothomes
*/
public class RootHomesTestJXL {
static final String roothome***celName = "D:/roothome***cel.xls";
static final String roothomesNewExcelName = "D:/roothomesNewExcel.xls";
public static void main(String[] args) {
RootHomesTestJXL excel = new RootHomesTestJXL();
excel.createExcel();
excel.readExcel();
excel.updateExcel();
}
public void createExcel(){
try {
WritableWorkbook book = Workbook.createWorkbook(new File(roothome***celName)); // 打开Excel文件
WritableSheet sheet = book.createSheet("roothomesSheet名字", 0); //定义填充Excel的sheet内容,0是第一页
Label label = new Label(0, 0, "this is create info.(roothomes)"); // 定义填充单元格的内容,Label 类型 ,存放在 第一列第一行(0,0)
sheet.addCell(label);// 向sheet里面添加单元格
//在2列1行的位置定义一个保存数字的单元格,值是 123456.789 (必须使用Number的完整包路径,否则Excel里面有语法歧义)
jxl.write.Number number = new jxl.write.Number(1, 0, 123456.789);
sheet.addCell(number);
// 写入数据并关闭文件
book.write();
book.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Row***ceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
public void readExcel(){
try {
Workbook book = Workbook.getWorkbook(new File(roothome***celName)); //获取Excel文件
Sheet sheet = book.getSheet(0); // 获得第1个sheet对象
Cell cell1 = sheet.getCell(0, 0); // 得到第1列第1行的单元格
String result = cell1.getContents();
System.out.println(result);
book.close();
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}
public void updateExcel(){
try {
Workbook wb = Workbook.getWorkbook(new File(roothome***celName)); // 获得 Excel 文件
// 打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook book = Workbook.createWorkbook(new File(roothomesNewExcelName), wb);
WritableSheet sheet = book.createSheet("roothomes Sheet2", 1); // 添加一个工作表
sheet.addCell(new Label(0, 0, "测试数据"));
book.write();
book.close();
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (Row***ceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
阅读(874) | 评论(0) | 转发(0) |