使用POI对EXCEL 读入写出

一同事让我给他解析60万条数据的excel表格 剔除指定的数据  果断写了 等写了一半 他又不用了 ... 继续研究  由于是第一次使用  瞎弄弄

所需jar包

commons-codec-1.5.jar

commons-logging-1.1.jar

dom4j-1.6.1.jar

junit-3.8.1.jar

log4j-1.2.13.jar

poi-3.9-20121203.jar

poi-examples-3.9-20121203.jar

poi-excelant-3.9-20121203.jar

poi-ooxml-3.9-20121203.jar

poi-scratchpad-3.9-20121203.jar

stax-api-1.0.1.jar

xmlbeans-2.3.0.jar
package com.jokey;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReaderAndWriter {
    
    private POIFSFileSystem fs;
    private  XSSFWorkbook wb;
    private  XSSFSheet sheet;
    private XSSFRow row;
    private XSSFCell cell;
    
    public static void main(String[] args) {
        try {
            // 对读取Excel表格标题测试
            
            
            ExcelReaderAndWriter excelReader = new ExcelReaderAndWriter();
            excelReader.reader();
            //excelReader.writer();
        } catch (FileNotFoundException e) {
            System.out.println("未找到指定路径的文件!");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private void reader() throws IOException{
        FileInputStream is = new FileInputStream("f:\\3.xlsX");
            wb = new XSSFWorkbook(is);
            sheet = wb.getSheetAt(0);
        sheet = wb.getSheetAt(0);
        //获取总行数
        int totalRowNum = sheet.getLastRowNum();
        //这里i<==
        for (int i = 0; i <= totalRowNum; i++) {
            row = sheet.getRow(i);
            for (int j = 0; j < row.getLastCellNum(); j++) {
                System.out.print(row.getCell(j));
            }
            System.out.println("");
        }
    }
    
    private void writer() throws IOException{
        FileOutputStream os = new FileOutputStream("f:\\5.xlsx");
        //创建工作表
        wb = new XSSFWorkbook();
        sheet = wb.createSheet();
        row = sheet.createRow(0);
        cell = row.createCell(0);
        cell.setCellValue("测试的啦");
        try {
            //写入
            wb.write(os);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

posted @ 2013-04-25 09:09  ×jokey  阅读(2484)  评论(0编辑  收藏  举报