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

  • 相关阅读:
    VMWare安装Solaris虚拟机的网络设置
    PeopleTools预警程序制作
    listener.ora增加监听端口
    用.Net Mage工具更新WPF ClickOnce应用程序部署清单
    基本测试方法用例场景
    Qt Vs msvc debug版本没有问题但release版本出现异常
    Qt 打包release发布问题
    Qt 鼠标悬浮按钮上出现浮窗效果
    Qt 样式对于QPushbutton 增加 hover press release效果
    阿里云ECS无法通过SSL远程链接问题。
  • 原文地址:https://www.cnblogs.com/xyd51cto/p/7732125.html
Copyright © 2011-2022 走看看