首页 > 编程语言 > 详细

JAVA获取EXCEL列头

时间:2020-04-15 10:03:50      阅读:234      评论:0      收藏:0      [点我收藏+]
 FileInputStream fileInputStream = new FileInputStream(rootPath + path + "/" + fileName);
 List<String> list = ExcelUtils.queryTopCell(fileInputStream, path + "/" + fileName, caseNo, request);

 

package com.sinoup.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.*;


public class ExcelUtils {

    private final static String excel2003L = ".xls";
    private final static String excel2007U = ".xlsx";

    public static List<String> queryTopCell(InputStream is, String fileName, String caseCode,HttpServletRequest request) {
        Workbook workbook = null;
        //sheet页name
        Sheet sheet = null;
        String SheetName="";
        Row row = null;
        Cell cell = null;//验证文件格式
        try {
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            if (suffix.equals(excel2003L)) {
                workbook = new HSSFWorkbook(is);
            } else if (suffix.equals(excel2007U)) {
                workbook = new XSSFWorkbook(is);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Error ee) {
            ee.printStackTrace();
        } finally {
            System.out.print("");
        }

        List<String> cellList= new ArrayList<String>();
        //只取第一个sheet页
        for (int i = 0; i < 1; i++) { //workbook.getNumberOfSheets()
            //获取第一个sheet
            sheet = workbook.getSheetAt(i);
            if (sheet == null) continue;
            SheetName=sheet.getSheetName();

            cellList.add(caseCode); //0
            cellList.add(fileName); //1
            cellList.add(SheetName); //2
            //获取第一行
            row=sheet.getRow(0);
            //遍历第一行

            int o =0;
            Iterator<Cell> it=row.iterator();
            while(it.hasNext())
            {
                cell=it.next();
                o++;
                if (cell == null) continue;
                String c=String.valueOf(cell);
                System.out.println("第"+o+"个:"+c);
                cellList.add(c);
            }
        }
        return cellList;
    }

}

 

JAVA获取EXCEL列头

原文:https://www.cnblogs.com/tongsi/p/12703267.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!