首页 > 代码库 > JAVA操作Excle之Poi(一)

JAVA操作Excle之Poi(一)

本章内容:

1.创建Excel以及单元格的不同类型

2.读取Excel

3.文本抽取

1.

package com.maya.poi;

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo3 {

    public static void main(String[] args) throws Exception {        
        
        Workbook wb=new HSSFWorkbook();//定义一个工作簿
        /*Sheet sheet=wb.createSheet("第一个sheet页");//创建一个sheet页
        Row row=sheet.createRow(0);//创建一行
        Cell cell=row.createCell(0);//创建一个单元格
        cell.setCellValue("这是第一个单元格");
        FileOutputStream out=new FileOutputStream("c:\\这是用Poi搞出来的Cell.xls");*/
        
        //模拟循环
        Sheet sheet=wb.createSheet("第一个sheet页");//创建一个sheet页
        for(int i=0;i<3;i++){
            Row row=sheet.createRow(i);//创建一行
            for(int j=0;j<10;j++){
                Cell cell=row.createCell(j);//创建一个单元格
                cell.setCellValue("第"+(i+1)+"行"+(j+1)+"列");            
            }
        }
        FileOutputStream out=new FileOutputStream("c:\\这是用Poi搞出来的Cell2.xls");
        
        wb.write(out);
        out.close();
    }
}

2.

package com.maya.poi2;

import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.poifs.filesystem.POIFSFileSystem;

public class Demo4 {
    //读取Excel
    public static void main(String[] args) throws Exception {
        InputStream in=new FileInputStream("f:\\张三李四.xls");
        POIFSFileSystem fs=new POIFSFileSystem(in);
        HSSFWorkbook wb=new HSSFWorkbook(fs);
        HSSFSheet hssfSheet=wb.getSheetAt(0);//获取第一个sheet页
        if(hssfSheet==null){
            return ;
        }
        //遍历Row
        for(int rowNum=0;rowNum<=hssfSheet.getLastRowNum();rowNum++){
            HSSFRow hssfRow=hssfSheet.getRow(rowNum);
            if(hssfRow==null){
                continue;
            }
            //遍历列
            for(int cellNum=0;cellNum<=hssfRow.getLastCellNum();cellNum++){
                HSSFCell hssfCell=hssfRow.getCell(cellNum);
                if(hssfCell==null){
                    continue;
                }
                System.out.print("  "+getValue(hssfCell));
            }
            System.out.println("");
        }        
    }
    
    public static String getValue(HSSFCell hssfCell){
        if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){
            return String.valueOf(hssfCell.getBooleanCellValue());
        }else if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
            return String.valueOf(hssfCell.getNumericCellValue());
        }else if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_FORMULA){
            return String.valueOf(hssfCell.getCachedFormulaResultType());
        }else{
            return String.valueOf(hssfCell.getStringCellValue());
        }
    }
}

3.

package com.maya.poi2;

import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class Demo5 {
    
    //文件抽取
    public static void main(String[] args) throws Exception {
        InputStream in=new FileInputStream("f:\\张三李四.xls");
        POIFSFileSystem fs=new POIFSFileSystem(in);
        HSSFWorkbook wb=new HSSFWorkbook(fs);
        
        ExcelExtractor ex=new ExcelExtractor(wb);//文件抽取
        //ex.setIncludeSheetNames(false);//不抽取sheet页名
        System.out.println(ex.getText());
    }

}

 

JAVA操作Excle之Poi(一)