importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.Iterator;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Row;importjxl.Sheet;importjxl.Workbook;importjxl.read.biff.BiffException;importjxl.write.Label;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;importjxl.write.WriteException;importjxl.write.biff.RowsExceededException;public class JavaExcelImpl implementsJavaExcel{
@Overridepublic voidwriteJxlExcel() {
File xlsFile= new File("jxl.xls");
WritableWorkbook workbook= null;try{//创建一个工作簿
workbook =Workbook.createWorkbook(xlsFile);//创建一个工作表
WritableSheet sheet = workbook.createSheet("sheet", 0);for (int row = 0; row < 10; row++) {for (int col = 0; col < 10; col++) {
sheet.addCell(new Label(col, row, "肆季"+row+col));
}
}
workbook.write();
}catch(IOException e) {
e.printStackTrace();
}catch(RowsExceededException e) {
e.printStackTrace();
}catch(WriteException e) {
e.printStackTrace();
}finally{if(workbook != null){try{
workbook.close();
}catch (WriteException |IOException e) {
e.printStackTrace();
}
}
}
}
@Overridepublic voidreadJxlExcel() {
File xlsFile= new File("jxl.xls");
Workbook workBook= null;try{//获得工作簿对象
workBook =Workbook.getWorkbook(xlsFile);//获得工作表
Sheet[] sheet =workBook.getSheets();for(Sheet sheet2 : sheet) {int rows =sheet2.getRows();int cols =sheet2.getColumns();for (int row = 0; row < rows; row++) {for (int col = 0; col < cols; col++) {
System.out.print(sheet2.getCell(col, row).getContents()+"\t");
}
System.out.println();
}
}
}catch (BiffException |IOException e) {
e.printStackTrace();
}
}
@Overridepublic voidwritePoiExcel() {//创建工作簿
HSSFWorkbook workBook = newHSSFWorkbook();//创建工作表
HSSFSheet sheet = workBook.createSheet("肆季");for (int row = 0; row < 10; row++) {
HSSFRow rows=sheet.createRow(row);for (int col = 0; col < 10; col++) {
rows.createCell(col).setCellValue("肆季"+row+col);
}
}
File file= new File("poi.xls");
FileOutputStream fos= null;try{
fos= newFileOutputStream(file);
workBook.write(fos);
}catch(IOException e) {
e.printStackTrace();
}finally{try{if(workBook != null) {
workBook.close();
}if(fos != null) {
fos.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
@Overridepublic voidreadPoiExcel() {
HSSFWorkbook swb= null;try{//获取工作簿对象
swb = new HSSFWorkbook(new FileInputStream("poi.xls"));//获取工作表对象
HSSFSheet sheet = swb.getSheetAt(0);//使用迭代器遍历Sheet对象
Iterator row =sheet.iterator();//原始数据总行数
int index =sheet.getLastRowNum();while(row.hasNext()){
Row rows=row.next();for (int i = 0; i <= index; i++) {
System.out.println(rows.getCell(i).getStringCellValue());
}
}
}catch(IOException e) {
e.printStackTrace();
}finally{if(swb != null){try{
swb.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}