我在這裏有一些問題,我想java讀取我的excel文檔,但如果有一些行我不想讀取,我想跳過這一點。這裏是我的Excel例如:讓我們假設我希望我的java程序讀取一行3 excel exampleJAVA - 從特定行中讀取excel
,並在這裏開始是我的代碼:
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection(dbconnection+","+dbusername+","+dbPass);
con.setAutoCommit(false);
PreparedStatement pstm = null ;
File file = new File(filepath);
FileInputStream input = new FileInputStream(file);
System.out.println(file);
if (!file.toString().contains("xlsx")){
System.out.println("hssf");
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheeth = wb.getSheetAt(0);
Row row;
for(int i=0; i<=sheeth.getLastRowNum(); i++){
row = sheeth.getRow(i);
int id = (int) row.getCell(0).getNumericCellValue();
String nama = row.getCell(1).getStringCellValue();
String rm = row.getCell(2).getStringCellValue();
String nama_ro = row.getCell(3).getStringCellValue();
String no_pks = row.getCell(4).getStringCellValue();
String ket = row.getCell(5).getStringCellValue();
String lob = row.getCell(6).getStringCellValue();
String sql = "INSERT INTO testdatapks VALUES('"+id+"','"+nama+"','"+rm+"','"+nama_ro+"','"+no_pks+"','"+ket+"','"+lob+"')";
pstm = (PreparedStatement) con.prepareStatement(sql);
pstm.execute();
System.out.println("Import rows "+i);
}
con.commit();
pstm.close();
con.close();
input.close();
System.out.println("Success import excel to mysql table");
}
我以爲我們不知道用戶或客戶端文件,他/她可以創造一個價值從任何行開始的卓越,我們永遠不會知道。如果excel上傳了第4行的值,該怎麼辦?或第5行?或者第一排? 有人可以幫助我做到這一點?前
只需將'i = 0'更改爲'i = 2' –
是的,但我們永遠不知道用戶或客戶端何時上傳其他excel的權利?如果excel上傳了第4行的值,該怎麼辦?或第5行?或者第一排? – Diastowo