Java——poi读取Excel文件

1.创建文件流,打开EXCEL文件

FileInputStream excelFile = new FileInputStream(excelPath);
XSSFWorkbook workbook = new XSSFWorkbook(excelFile);

2.切换到对应文件名

XSSFSheet excelSheet = workbook.getSheet(sheetName);

3.获取实际行数和列数

int rows = excelSheet.getPhysicalNumberOfRows(); //行数
int cols = excelSheet.getRow(0).getPhysicalNumberOfCells();//列数

4.读取数据

    public static String ReadData(XSSFSheet excelSheet, int row, int col){
        try{
            String CellData= "";
            XSSFCell cell = excelSheet.getRow(row).getCell(col);
            if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                cell.setCellType(Cell.CELL_TYPE_STRING);
                CellData = cell.getStringCellValue();
            }
            else if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){
                CellData = cell.getStringCellValue();
            }
            else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
                CellData = cell.getCellFormula();
            }
            return CellData;
        }catch(Exception e){
            return "";
        }
    }

 

示例:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Excel {
    
    public static void main(String[] args) throws IOException {
        String excelPath = "F:\\login.xlsx";
        String sheetName = "001";
        try{
            FileInputStream excelFile = new FileInputStream(excelPath);
            XSSFWorkbook workbook = new XSSFWorkbook(excelFile);
            XSSFSheet excelSheet = workbook.getSheet(sheetName);
            int rows = excelSheet.getPhysicalNumberOfRows(); //行数
            int cols = excelSheet.getRow(0).getPhysicalNumberOfCells();//列数

            for(int row = 0;row< rows; ++row){
                for (int col =0; col < cols; ++col){
                    System.out.print(ReadData(excelSheet, row, col) + ' ');
                    if(col ==1)
                        System.out.println();
                }
            }
            workbook.close();
        }catch (FileNotFoundException e ){
            System.out.println("找不到该文件");
        }
    }
    
    public static String ReadData(XSSFSheet excelSheet, int row, int col){
        try{
            String CellData= "";
            XSSFCell cell = excelSheet.getRow(row).getCell(col);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            CellData = cell.getStringCellValue();
            return CellData;
        }catch(Exception e){
            return "";
        }
    }
}

 

 输出结果:

 

5.写入excel

同样我们一开始先来想下手工写入数据流程

1:打开EXCEL

2:指定Sheet

3: 指定行号

4:指定列号

5:写入数据

6:保存数据

了解了操作流程,我们就将思想转为成代码其实1,2,3,4

我们在读Excel时就已写过,但这里要注意下,我们在读Excel时是不是已有数据,那也就是说每个单元格中已有内容,我们用getRow(行号)不是空值对吗,如果我们要往一个没有值的单元格写值时一开始那个行号是空值,所以我们第三步应改为创建一个行号,第四步改为创一个列号创建行号

 

我们可以用CreateRow(行号)在创建行号时,我们要想一个问題,我们一开始创建了第一行,并写入一个值到单元格中,比如我想写入一个结果到第一行第二个单元格时也是先创建一行吗,如果再用创建方式生成第一行,那前一次写入的数据会不会不存在呢,实际上是会被删掉的,这个自已可以去试试

 

所以我们代码实现应是这样一开始用getrow(1)如果反回的值是空值,那就用CreateRow的方式,如果不是空值我就用GetRow(1)

public static void setData(int row, int col, String sheetName, String Data, String WriteExcelPath) throws IOException{
        
        try{
            
            FileInputStream excelFile = new FileInputStream(WriteExcelPath);
            XSSFWorkbook workbook = new XSSFWorkbook(excelFile);
            XSSFSheet excelSheet = workbook.getSheet(sheetName);
            
            XSSFRow Row = excelSheet.getRow(row);
            if(Row == null)
                Row = excelSheet.createRow(row);
            XSSFCell Cell = Row.getCell(col);
            
            if(Cell == null )
                Cell = Row.createCell(col);
            
            Cell.setCellValue(Data);
            FileOutputStream fileOut = new FileOutputStream(WriteExcelPath);
            workbook.write(fileOut);
            fileOut.close();
            workbook.close();
        }catch(Exception e){
            System.out.println("数据写入错误");
        }
    }

 

posted @ 2016-08-06 12:07  hjhsysu  阅读(1932)  评论(0编辑  收藏  举报