如何做好小区迭代EXCEL在java的 [英] How to do cell iteration of excel in java
本文介绍了如何做好小区迭代EXCEL在java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2行5列的Excel。现在我手动输入code,从第一行取值。我怎么可以遍历进程?
下面是code在Excel中第一行。从第2排,我不知道该怎么办......我想陆陆续续地重复一行。
工作簿簿= Workbook.getWorkbook(新文件(
\\\\ C:\\\\ \\\\用户A-4935 \\\\ \\\\桌面DataPool_CA.xls));
苫布苫布= workbook.getSheet(工作表Sheet1);
的System.out.println(达到表);
小区的= sheet.getCell(2,1);
小区B = sheet.getCell(3,1);
小区C = sheet.getCell(4,1);
单元d = sheet.getCell(5,1);
电池E = sheet.getCell(6,1);
小区F = sheet.getCell(7,1);
传感器g = sheet.getCell(8,1);
细胞H = sheet.getCell(9,1);
小区i = sheet.getCell(10,1);串UID = a.getContents();
串deptFromDat = b.getContents();
串deptToDate = c.getContents();
串DEPT1 = d.getContents();
串arrival1 = e.getContents();
串eihon1 = f.getContents();
串枝code1 = g.getContents();
串userType1 = h.getContents();
串sessionId1 = i.getContents();
解决方案
使用下面的code遍历数据表中的所有行:
表表= workbook.getSheet(工作表Sheet1);
对于(鳞次栉比:表){
(电池单元:行){
//你的逻辑
}
}
或者也可使用下面的code:
表表= workbook.getSheet(工作表Sheet1);
对(INT I = 0; I&2;我++){
鳞次栉比= sheet.getRow(I)
如果(行== NULL){
//做一些与一个空行
继续;
}
为(中间体J = 0; J&小于5; J ++){
电池单元= row.getCell(J);
如果(细胞== NULL){
//做一些与空单元格
继续;
}
//你的逻辑
}
}
I am having an excel with 2 rows and 5 columns. Now I entered code manually to take values from 1st row. How can I iterate the process?
Below is the code for 1st row in excel. From the 2nd row on, I dont know how to do... I want to iterate one row after another.
Workbook workbook = Workbook.getWorkbook(new File(
"\\C:\\users\\a-4935\\Desktop\\DataPool_CA.xls"));
Sheet sheet = workbook.getSheet("Sheet1");
System.out.println("Reached to Sheet");
Cell a = sheet.getCell(2,1);
Cell b = sheet.getCell(3,1);
Cell c = sheet.getCell(4,1);
Cell d = sheet.getCell(5,1);
Cell e = sheet.getCell(6,1);
Cell f = sheet.getCell(7,1);
Cell g = sheet.getCell(8,1);
Cell h = sheet.getCell(9,1);
Cell i = sheet.getCell(10,1);
String uId = a.getContents();
String deptFromDat = b.getContents();
String deptToDate = c.getContents();
String dept1 = d.getContents();
String arrival1 = e.getContents();
String eihon1 = f.getContents();
String branchCode1 = g.getContents();
String userType1 = h.getContents();
String sessionId1 = i.getContents();
解决方案
Use the code below to iterate over all rows of a datasheet:
Sheet sheet = workbook.getSheet("Sheet1");
for (Row row : sheet) {
for (Cell cell : row) {
//your logic
}
}
Or, alternatively, use the following code:
Sheet sheet = workbook.getSheet("Sheet1");
for (int i = 0; i < 2; i++) {
Row row = sheet.getRow(i);
if(row == null) {
//do something with an empty row
continue;
}
for (int j = 0; j < 5; j++) {
Cell cell = row.getCell(j);
if(cell == null) {
//do something with an empty cell
continue;
}
//your logic
}
}
这篇关于如何做好小区迭代EXCEL在java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文