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

原文地址:https://www.cnblogs.com/xyd51cto/p/7732125.html