標籤:null excel int public try imp cells new amp
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();
}
}
}
JAVA讀取Excel2003、2007、2010教程