package com.li.excel;
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.RowsExceededException;
public class ExcelOperater {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// writeExcel();
readExcel();
}
private static void writeExcel() throws IOException, RowsExceededException,
WriteException {
File xlsFile = new File("jxl.xls");
// 创建一个工作簿
WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
// 创建一个工作表
WritableSheet sheet = workbook.createSheet("sheet1", 0);
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
// 向工作表中添加数据
sheet.addCell(new Label(col, row, "data" + row + col));
}
}
workbook.write();
workbook.close();
}
private static void readExcel() throws BiffException, IOException {
File xlsFile = new File("students.xls");
// 获得工作簿对象
Workbook workbook = Workbook.getWorkbook(xlsFile);
// 获得所有工作表
Sheet[] sheets = workbook.getSheets();
// 遍历工作表
if (sheets != null) {
for (Sheet sheet : sheets) {
// 获得行数
int rows = sheet.getRows();
// 获得列数
int cols = sheet.getColumns();
// 读取数据
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
// System.out.printf("%10s", sheet.getCell(col, row)
// .getContents() + " ");
Cell cell = sheet.getCell(col, row);
System.out.print(cell.getContents() + " ");
}
System.out.println();
}
}
}
workbook.close();
}
}