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));
    }
    }
    }
    }} 

  • 相关阅读:
    vue2.0使用基础
    docker 镜像中心搭建
    dubbo使用multicast注册方式消费者无法发现服务的一种情况(我遇到的情况)
    Bootstrap 避免模态框在用户点击背景空白处时,会自动关闭。
    HNOI2019滚粗记
    THUSC 2018 酱油记
    # HNOI2018滚粗记
    第二类斯特林数总结
    PKUWC 2018游记
    NOIP2017滚粗记
  • 原文地址:https://www.cnblogs.com/xyd51cto/p/7732125.html
Copyright © 2011-2022 走看看