import java.io.File;
import java.io.FileInputStream;
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.ss.usermodel.WorkbookFactory;
public class TestExcel {
public static void main(String[] args) {
try {
File file = new File("C://Users/Administrator/Desktop/test.xls");
FileInputStream is = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(is);//这种方式 Excel 2003/2007/2010 都是可以处理
Sheet sheet = workbook.getSheetAt(0);//第一个sheet
int rowCount = sheet.getPhysicalNumberOfRows();//获取总行数
Row row1 = sheet.getRow(0);//获取第一行 ,判断excel是否规范
if(!row1.getCell(0).toString().trim().equals("序号")||
!row1.getCell(1).toString().trim().equals("评价年度")||
!row1.getCell(2).toString().trim().equals("纳税人识别号")||
!row1.getCell(3).toString().trim().equals("纳税人名称")||
!row1.getCell(4).toString().trim().equals("评价结果")){
return;
}
for(int i=1;i<rowCount;i++){
Row row = sheet.getRow(i);
int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
String xh = "";//序号
String pjnd = "";//评价年度
String nsrsbh = "";//纳税人识别号
String nsrmc = "";//纳税人名称
String pjjg = "";//评价结果
for(int c=0;c<cellCount;c++){
String cellStr = row.getCell(c).toString().trim();
if(!cellStr.equals("")&&cellStr!=null){
if(c==0) xh=cellStr;
if(c==1) pjnd=cellStr;
if(c==2) nsrsbh=cellStr;
if(c==3) nsrmc=cellStr;
if(c==4) pjjg=cellStr;
}
}
System.out.println(xh+"|"+pjnd+"|"+nsrsbh+"|"+nsrmc+"|"+pjjg);//拿到数据进行数据库操作
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
原文地址:https://www.cnblogs.com/pegasus827/p/9384339.html