• java poi


    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    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 ReadExcel {
    	public static void main(String[] args) throws IOException {
    
    		// File file = new File("C:/Users.xlsx");
    		InputStream stream = new FileInputStream(
    				"E:\123.xlsx");
    
    		XSSFWorkbook xssfWorkbook = new XSSFWorkbook(stream);
    		XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
    
    		int rowstart = xssfSheet.getFirstRowNum();
    		int rowEnd = xssfSheet.getLastRowNum();
    		for (int i = rowstart; i <= rowEnd; i++) {
    			XSSFRow row = xssfSheet.getRow(i);
    			if (null == row)
    				continue;
    			int cellStart = row.getFirstCellNum();
    			int cellEnd = row.getLastCellNum();
    
    			for (int k = cellStart; k <= cellEnd; k++) {
    				XSSFCell cell = row.getCell(k);
    				if (null == cell)
    					continue;
    
    				switch (cell.getCellType()) {
    				case HSSFCell.CELL_TYPE_NUMERIC: // 数字
    					System.out.print(cell.getNumericCellValue() + "	");
    					break;
    				case HSSFCell.CELL_TYPE_STRING: // 字符串
    					System.out.print(cell.getStringCellValue() + "	");
    					break;
    				case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
    					System.out.println(cell.getBooleanCellValue() + "	");
    					break;
    				case HSSFCell.CELL_TYPE_FORMULA: // 公式
    					System.out.print(cell.getCellFormula() + "	");
    					break;
    				case HSSFCell.CELL_TYPE_BLANK: // 空值
    					System.out.println(" ");
    					break;
    				case HSSFCell.CELL_TYPE_ERROR: // 故障
    					System.out.println(" ");
    					break;
    				default:
    					System.out.print("未知类型   ");
    					break;
    				}
    
    			}
    			System.out.print("
    ");
    		}
    	}
    }
    

     

     读取xls

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    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 ReadXls {
        public static void main(String[] args) throws IOException, IOException {
            File file = new File("E:\123.xls");
            POIFSFileSystem poifsFileSystem = new POIFSFileSystem(
                    new FileInputStream(file));
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(poifsFileSystem);
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
    
            int rowstart = hssfSheet.getFirstRowNum();
            int rowEnd = hssfSheet.getLastRowNum();
            for (int i = rowstart; i <= rowEnd; i++) {
                HSSFRow row = hssfSheet.getRow(i);
                if (null == row)
                    continue;
                int cellStart = row.getFirstCellNum();
                int cellEnd = row.getLastCellNum();
    
                for (int k = cellStart; k <= cellEnd; k++) {
                    HSSFCell cell = row.getCell(k);
                    if (null == cell)
                        continue;
                    // System.out.print("" + k + "  ");
                    // System.out.print("type:"+cell.getCellType());
    
                    switch (cell.getCellType()) {
                    case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                        System.out.print(cell.getNumericCellValue() + "   ");
                        break;
                    case HSSFCell.CELL_TYPE_STRING: // 字符串
                        System.out.print(cell.getStringCellValue() + "   ");
                        break;
                    case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                        System.out.println(cell.getBooleanCellValue() + "   ");
                        break;
                    case HSSFCell.CELL_TYPE_FORMULA: // 公式
                        System.out.print(cell.getCellFormula() + "   ");
                        break;
                    case HSSFCell.CELL_TYPE_BLANK: // 空值
                        System.out.println(" ");
                        break;
                    case HSSFCell.CELL_TYPE_ERROR: // 故障
                        System.out.println(" ");
                        break;
                    default:
                        System.out.print("未知类型   ");
                        break;
                    }
    
                }
                System.out.print("
    ");
            }
        }
    }

    异常:Invalid header signature POI

    这一般是由于第三方工具自动生成的excel文件的文件不太规范,你可以手动打开Excel(如果可以打开)再另存为-》保存Excel就可以了

  • 相关阅读:
    log4net 发布到生产环境不写日志的解决方法--使用 NLog日志
    centos 下Supervisor 守护进程基本配置
    centos 7 下安装Nginx
    Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持
    centos之Haproxy 负载均衡学习笔记
    改进初学者的PID-介绍
    实现Modbus TCP多网段客户端应用
    有一种亲切是手机
    实现Modbus ASCII多主站应用
    爱好
  • 原文地址:https://www.cnblogs.com/liangbo-/p/6543360.html
Copyright © 2020-2023  润新知