zoukankan      html  css  js  c++  java
  • 处理excel表

    package com.pojo;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;

    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.Cell;
    import org.apache.poi.ss.usermodel.Row;

    public class Excel {

    public static String getTitleValue(HSSFCell cell) {
    String strtitle=cell.getStringCellValue();
    return strtitle;
    }
    public static void main(String[] args) throws Exception {

    FileInputStream fileInputStream=new FileInputStream(new File(System.getProperty("user.dir")+"/src/user.xls"));
    //如果文件存在 对excel进行解析
    if(fileInputStream!=null){
    POIFSFileSystem poifsFileSystem=new POIFSFileSystem(fileInputStream);
    //得到文档对象
    HSSFWorkbook hssfWorkbook=new HSSFWorkbook(poifsFileSystem);
    //得到第一个表
    HSSFSheet hssfSheet=hssfWorkbook.getSheetAt(0);
    HSSFRow h_row=hssfSheet.getRow(0);
    if (hssfSheet==null) {
    System.out.println("这个表中的内容为空");
    }else {
    int firstRow=hssfSheet.getFirstRowNum();
    int lastRow=hssfSheet.getLastRowNum();
    for (int i = firstRow; i <= lastRow; i++) {
    //获取总行数
    Row row=hssfSheet.getRow(i);
    if(row!=null){
    // 获取每一行的列数
    int lastcell=row.getLastCellNum();
    int firstcell=row.getFirstCellNum();
    for (int j = firstcell; j < lastcell; j++) {
    Cell cell=row.getCell(j);
    if (cell!=null) {
    System.out.println(cell);
    }
    }
    }

    System.out.println(" ");

    };
    //标题总列数
    int colnum=h_row.getPhysicalNumberOfCells();
    String[] title=new String[colnum];
    for (int i = 0; i < title.length; i++) {
    title[i]=getTitleValue(h_row.getCell((short) i));
    }
    }
    }
    }} 

  • 相关阅读:
    对称二叉树
    显示图片路径问题
    爆炸的联赛模拟 8.24~8.25
    【Java基础总结】字符串
    pro、pre、test、dev环境
    开发环境、测试环境、预发布环境、生产环境的区别
    【IP】虚拟IP原理
    【版本】Spring Cloud 版本
    Zipkin
    【小笔记】小知识记录
  • 原文地址:https://www.cnblogs.com/xyd51cto/p/7732125.html
Copyright © 2011-2022 走看看