(七)读取excel,遍历一个工作簿

原文链接:https://blog.csdn.net/class157/article/details/92816169,https://blog.csdn.net/class157/article/details/93237963

package cases;

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;
import org.apache.poi.ss.usermodel.*;
import org.testng.annotations.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @description:
 * @author: lv
 * @time: 2020/5/22 14:21
 */
public class DemoTest {

    @Test
    public void testPoi()throws IOException {
        //建立输入流
        InputStream is = new FileInputStream("D:\\3.xls");
        //接受一个输入流
        POIFSFileSystem fs = new POIFSFileSystem(is);
        //创建了一个工作簿
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        //获取第一个sheet页
        HSSFSheet hssfSheet = wb.getSheetAt(0);
        //判断Sheet是否为空
        if (hssfSheet == null) {
            return;
        }
        //遍历行row
        //continue只是终止本次循环,接着还执行后面的循环
        for (int rownum = 0; rownum <= hssfSheet.getLastRowNum(); rownum++) {
            //获取到每一行
            HSSFRow sheetRow = hssfSheet.getRow(rownum);
            if (sheetRow == null) {
                continue;
            }
            //遍历列cell
            for (int cellnum = 0; cellnum <= sheetRow.getLastCellNum(); cellnum++) {
                HSSFCell cell = sheetRow.getCell(cellnum);
                if (cell == null) {
                    continue;
                }
                //获取单元格的值
                System.out.print(" " + getValue(cell));
            }
        }
    }

    private static String getValue(HSSFCell hssfCell) {
        String cellValue = "";
        if(hssfCell == null){
            return cellValue;
        }
        //把数字当成String来读,避免出现1读成1.0的情况
        if(hssfCell.getCellType() == CellType.NUMERIC){
            hssfCell.setCellType(CellType.STRING);
        }

        //hssfCell.getCellType() 获取当前列的类型
        if (hssfCell.getCellType() == CellType.BOOLEAN) {
            cellValue = String.valueOf(hssfCell.getBooleanCellValue());
            return cellValue;
        } else if (hssfCell.getCellType() == CellType.NUMERIC) {
            hssfCell.setCellType(CellType.STRING);
            cellValue =  String.valueOf(hssfCell.getNumericCellValue());
            return cellValue;
        } else if(hssfCell.getCellType() == CellType.STRING){
            cellValue =  String.valueOf(hssfCell.getStringCellValue());
            return cellValue;
        }else if(hssfCell.getCellType() == CellType.FORMULA){
            cellValue =  String.valueOf(hssfCell.getCellFormula());
            return cellValue;
        }else if (hssfCell.getCellType() == CellType.BLANK){
            cellValue = " ";
            return cellValue;
        }else if(hssfCell.getCellType() == CellType.ERROR){
            cellValue = "非法字符";
            return cellValue;
        }else{
            cellValue = "未知类型";
            return cellValue;
        }

    }

}

注意点:

调用方式被弃用 
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
可以使用这个 
cell.setCellType(CellType.NUMERIC);

  

猜你喜欢

转载自www.cnblogs.com/lvchengda/p/12941427.html
今日推荐