package yz.zrk;
import java.io.File;
import jxl.Workbook;
import jxl.write.*;
public class CreateExcle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//创建一个文件对象
WritableWorkbook book = Workbook.createWorkbook(new File("test.xls"));
//创建sheet,也就是左下方的页码
WritableSheet sheet = book.createSheet("第一页", 0);
@SuppressWarnings("unused")
WritableSheet s = book.createSheet("第二页", 1);
System.out.println(book.getNumberOfSheets());
//向单元格(第一行第一列)写入字符串
Label label = new Label(0,0,"test");
//将单元格添加到工作表中
sheet.addCell(label);
//向单元格写入数字
jxl.write.Number num = new jxl.write.Number(1,0,100.19);
//将单元格添加到工作表中
sheet.addCell(num);
//写入数据
book.write();
//关闭
book.close();
}
catch(Exception e ) {
e.printStackTrace();
}
System.out.println("the excel has been created...");
}
}
package yz.zrk;
import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ReadExcel {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//创建一个文件对象
File f = new File("test.xls");
//当文件存在时,进行读取操作
if(f.exists()) {
Workbook book = Workbook.getWorkbook(f);
Sheet sheet = book.getSheet(0);
for(int i=0;i<sheet.getRows();i++) {
for(int j=0;j<sheet.getColumns();j++) {
//得到单元格对象
Cell cell = sheet.getCell(j,i);
//得到该单元格的内容
String content = cell.getContents();
System.out.println(content);
}
}
book.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Java创建与读取Excel
最新推荐文章于 2024-04-23 20:09:56 发布