java使用jxl,poi解析excel文件

 1 public interface JavaExcel {
 2 
 3 /**
 4 * 使用jxl写excel文件
 5 */
 6 public void writeJxlExcel();
 7 
 8 /**
 9 * 使用jxl读excel文件
10 */
11 public void readJxlExcel();
12 
13 /**
14 * 使用poi写excel文件
15 */
16 public void writePoiExcel();
17 
18 /**
19 * 使用poi读excel文件
20 */
21 public void readPoiExcel();
22 
23 }
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

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 JavaExcelImpl implements JavaExcel{

    @Override
    public void writeJxlExcel() {
        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();
                }
            }
        }
        
    }

    @Override
    public void readJxlExcel() {
        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();
        }
        
    }

    @Override
    public void writePoiExcel() {
        // 创建工作簿
        HSSFWorkbook workBook = new HSSFWorkbook();
        // 创建工作表
        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 = new FileOutputStream(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();
            }
        }
    }

    @Override
    public void readPoiExcel() {
        HSSFWorkbook swb = null;
        try {
            // 获取工作簿对象
            swb =     new HSSFWorkbook(new FileInputStream("poi.xls"));
            // 获取工作表对象
            HSSFSheet sheet = swb.getSheetAt(0);
            // 使用迭代器遍历Sheet对象
            Iterator<Row> 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();
                }
            }
        }
        
    }

}
jxl下载:https://pan.baidu.com/s/17TPW-x4YCVf17RGNWEm_FA
poi下载:https://pan.baidu.com/s/1J6iSpyXAQxyfXcwtMHf7dQ

  

posted @ 2018-08-29 22:01  天葬  阅读(239)  评论(0编辑  收藏  举报