我有一个 excel 文件,如下所示。
1 Kr Veerappan 123 Hardware Team 02/04/2012 10:30:39 20:23:14
1 Kr Veerappan 123 Hardware Team 03/04/2012 09:46:01 19:58:16
1 Kr Veerappan 123 Hardware Team 04/04/2012 10:06:26 19:58:31
1 Kr Veerappan 123 Hardware Team 05/04/2012 11:38:51 19:32:17
1 Kr Veerappan 123 Hardware Team 06/04/2012 10:43:06 21:02:06
1 Kr Veerappan 123 Hardware Team 09/04/2012 11:02:32 22:05:14
1 Kr Veerappan 123 Hardware Team 10/04/2012 10:49:41 19:07:48
1 Kr Veerappan 123 Hardware Team 11/04/2012 10:16:42 20:30:13
1 Kr Veerappan 123 Hardware Team 12/04/2012 10:15:54 19:54:51
1 Kr Veerappan 123 Hardware Team 13/04/2012 10:18:30 19:05:55
1 Kr Veerappan 123 Hardware Team 18/04/2012 10:52:37 18:50:36
1 Kr Veerappan 123 Hardware Team 19/04/2012 10:24:59 20:29:59
1 Kr Veerappan 123 Hardware Team 20/04/2012 10:29:47 18:58:23
1 Kr Veerappan 123 Hardware Team 23/04/2012 10:24:31 20:11:29
1 Kr Veerappan 123 Hardware Team 24/04/2012 10:13:18 19:05:25
1 Kr Veerappan 123 Hardware Team 25/04/2012 10:44:53 16:30:13
1 Kr Veerappan 123 Hardware Team 26/04/2012 10:44:01 19:25:51
1 Kr Veerappan 123 Hardware Team 27/04/2012 10:58:37 19:24:37
1 Kr Veerappan 123 Hardware Team 30/04/2012 11:16:04 22:34:21
12310452 Ravi Pandit 123 Contractor 02/04/2012 10:59:52 20:10:36
12310452 Ravi Pandit 123 Contractor 03/04/2012 11:12:25 20:44:23
115 Narayana Rao 123 Client Application Team 21/04/2012 15:59:41 16:52:10
115 Narayana Rao 123 Client Application Team 22/04/2012 10:35:03 12:24:54
117 Butchi B Paddi 123 Mobile Team 03/04/2012 10:16:02 19:19:50
117 Butchi B Paddi 123 Mobile Team 04/04/2012 10:15:47 20:44:08
117 Butchi B Paddi 123 Mobile Team 05/04/2012 11:03:18 18:48:46
117 Butchi B Paddi 123 Mobile Team 06/04/2012 10:54:41 18:26:38
117 Butchi B Paddi 123 Mobile Team 09/04/2012 11:34:15 19:31:29
117 Butchi B Paddi 123 Mobile Team 10/04/2012 12:15:34 21:14:02
117 Butchi B Paddi 123 Mobile Team 11/04/2012 09:29:16 20:11:02
117 Butchi B Paddi 123 Mobile Team 12/04/2012 12:08:09 20:17:53
117 Butchi B Paddi 123 Mobile Team 13/04/2012 11:42:39 20:04:53
117 Butchi B Paddi 123 Mobile Team 14/04/2012 11:08:13 19:05:49
117 Butchi B Paddi 123 Mobile Team 16/04/2012 11:29:49 18:42:21
117 Butchi B Paddi 123 Mobile Team 17/04/2012 14:13:40 19:09:58
117 Butchi B Paddi 123 Mobile Team 18/04/2012 10:43:03 19:50:01
117 Butchi B Paddi 123 Mobile Team 19/04/2012 11:07:09 18:29:44
117 Butchi B Paddi 123 Mobile Team 21/04/2012 10:07:04 18:08:43
117 Butchi B Paddi 123 Mobile Team 23/04/2012 11:28:05 19:46:04
117 Butchi B Paddi 123 Mobile Team 24/04/2012 11:46:01 19:36:22
117 Butchi B Paddi 123 Mobile Team 25/04/2012 10:59:08 19:56:39
117 Butchi B Paddi 123 Mobile Team 26/04/2012 11:56:34 20:22:05
117 Butchi B Paddi 123 Mobile Team 27/04/2012 11:41:21 19:43:51
117 Butchi B Paddi 123 Mobile Team 30/04/2012 11:58:35 20:21:36
119 Sarish Nayak 001 Mobile Team 02/04/2012 10:33:08 14:51:02
119 Sarish Nayak 001 Mobile Team 05/04/2012 10:29:07 19:37:37
119 Sarish Nayak 001 Mobile Team 06/04/2012 10:43:06 15:20:56
119 Sarish Nayak 001 Mobile Team 10/04/2012 09:54:19 18:42:37
119 Sarish Nayak 001 Mobile Team 11/04/2012 09:59:44 18:38:20
119 Sarish Nayak 001 Mobile Team 12/04/2012 10:53:42 18:48:43
119 Sarish Nayak 001 Mobile Team 13/04/2012 10:19:11 19:32:57
119 Sarish Nayak 001 Mobile Team 24/04/2012 09:38:58 19:46:15
119 Sarish Nayak 001 Mobile Team 25/04/2012 09:41:14 19:38:27
119 Sarish Nayak 001 Mobile Team 26/04/2012 10:15:53 20:08:55
119 Sarish Nayak 001 Mobile Team 27/04/2012 09:59:06 19:06:02
120 Manjunatha Dn 065 Hardware Team 02/04/2012 10:19:44 21:45:20
120 Manjunatha Dn 065 Hardware Team 03/04/2012 10:36:08 20:48:28
120 Manjunatha Dn 065 Hardware Team 04/04/2012 10:18:37 22:54:23
120 Manjunatha Dn 065 Hardware Team 05/04/2012 10:29:14 22:45:08
120 Manjunatha Dn 065 Hardware Team 06/04/2012 10:46:33 21:04:38
120 Manjunatha Dn 065 Hardware Team 09/04/2012 10:30:50 20:48:48
120 Manjunatha Dn 065 Hardware Team 10/04/2012 10:20:36 20:22:51
120 Manjunatha Dn 065 Hardware Team 11/04/2012 10:57:13 22:29:50
120 Manjunatha Dn 065 Hardware Team 12/04/2012 10:27:47 21:43:52
120 Manjunatha Dn 065 Hardware Team 13/04/2012 10:23:33 21:51:19
120 Manjunatha Dn 065 Hardware Team 16/04/2012 10:43:52 20:08:14
120 Manjunatha Dn 065 Hardware Team 17/04/2012 10:21:48 23:34:04
120 Manjunatha Dn 065 Hardware Team 18/04/2012 10:29:54 18:24:57
120 Manjunatha Dn 065 Hardware Team 19/04/2012 10:32:54 22:34:13
120 Manjunatha Dn 065 Hardware Team 23/04/2012 10:31:01 21:54:44
120 Manjunatha Dn 065 Hardware Team 24/04/2012 10:44:52 23:30:50
120 Manjunatha Dn 065 Hardware Team 25/04/2012 02:19:51 21:03:21
120 Manjunatha Dn 065 Hardware Team 26/04/2012 10:36:07 22:52:40
120 Manjunatha Dn 065 Hardware Team 27/04/2012 10:40:58 21:41:16
120 Manjunatha Dn 065 Hardware Team 30/04/2012 10:43:33 22:32:15
121 Ashwini Amol Kulkarni 123 Qa Team 02/04/2012 11:18:11 20:18:34
121 Ashwini Amol Kulkarni 123 Qa Team 03/04/2012 11:00:37 19:05:52
121 Ashwini Amol Kulkarni 123 Qa Team 04/04/2012 10:49:30 20:24:35
121 Ashwini Amol Kulkarni 123 Qa Team 05/04/2012 10:20:59 19:53:49
121 Ashwini Amol Kulkarni 123 Qa Team 06/04/2012 11:15:03 18:17:20
121 Ashwini Amol Kulkarni 123 Qa Team 09/04/2012 11:21:01 19:36:11
121 Ashwini Amol Kulkarni 123 Qa Team 10/04/2012 10:38:39 20:40:26
121 Ashwini Amol Kulkarni 123 Qa Team 11/04/2012 10:07:13 19:16:23
121 Ashwini Amol Kulkarni 123 Qa Team 12/04/2012 10:39:32 17:33:30
121 Ashwini Amol Kulkarni 123 Qa Team 13/04/2012 10:49:31 19:33:58
121 Ashwini Amol Kulkarni 123 Qa Team 16/04/2012 12:13:08 20:13:14
121 Ashwini Amol Kulkarni 123 Qa Team 17/04/2012 10:45:31 20:27:33
121 Ashwini Amol Kulkarni 123 Qa Team 18/04/2012 13:17:38 19:46:26
121 Ashwini Amol Kulkarni 123 Qa Team 19/04/2012 11:13:41 19:17:13
121 Ashwini Amol Kulkarni 123 Qa Team 23/04/2012 10:54:53 19:13:41
121 Ashwini Amol Kulkarni 123 Qa Team 24/04/2012 10:26:54 19:31:41
121 Ashwini Amol Kulkarni 123 Qa Team 25/04/2012 12:30:13 18:56:19
121 Ashwini Amol Kulkarni 123 Qa Team 26/04/2012 11:19:46 20:42:33
121 Ashwini Amol Kulkarni 123 Qa Team 27/04/2012 11:38:42 17:23:34
121 Ashwini Amol Kulkarni 123 Qa Team 30/04/2012 16:09:55 20:04:45
122 Melvin P Perinchery 073 Mobile Team 02/04/2012 11:12:52 20:51:39
122 Melvin P Perinchery 073 Mobile Team 03/04/2012 10:51:50 21:05:26
122 Melvin P Perinchery 073 Mobile Team 04/04/2012 11:27:50 19:03:20
122 Melvin P Perinchery 073 Mobile Team 09/04/2012 12:00:08 19:40:01
122 Melvin P Perinchery 073 Mobile Team 10/04/2012 11:57:59 20:14:18
122 Melvin P Perinchery 073 Mobile Team 11/04/2012 10:55:41 18:43:04
122 Melvin P Perinchery 073 Mobile Team 12/04/2012 13:37:39 19:02:58
122 Melvin P Perinchery 073 Mobile Team 13/04/2012 10:47:50 18:34:46
122 Melvin P Perinchery 073 Mobile Team 16/04/2012 11:05:04 19:59:02
122 Melvin P Perinchery 073 Mobile Team 17/04/2012 10:57:45 19:34:32
122 Melvin P Perinchery 073 Mobile Team 18/04/2012 11:15:32 19:23:40
122 Melvin P Perinchery 073 Mobile Team 19/04/2012 12:42:23 19:05:18
122 Melvin P Perinchery 073 Mobile Team 23/04/2012 11:16:11 19:14:55
122 Melvin P Perinchery 073 Mobile Team 24/04/2012 11:05:07 18:44:45
122 Melvin P Perinchery 073 Mobile Team 26/04/2012 11:28:21 18:30:26
122 Melvin P Perinchery 073 Mobile Team 27/04/2012 11:35:13 18:53:09
123 Srinivas R Kaka 065 Qa Team 02/04/2012 10:55:53 20:22:10
123 Srinivas R Kaka 065 Qa Team 03/04/2012 10:52:14 19:12:11
123 Srinivas R Kaka 065 Qa Team 04/04/2012 11:19:28 22:26:03
123 Srinivas R Kaka 065 Qa Team 05/04/2012 10:08:21 20:14:40
123 Srinivas R Kaka 065 Qa Team 06/04/2012 10:46:01 15:04:14
123 Srinivas R Kaka 065 Qa Team 09/04/2012 10:47:02 18:42:00
123 Srinivas R Kaka 065 Qa Team 10/04/2012 11:36:47 19:35:51
123 Srinivas R Kaka 065 Qa Team 11/04/2012 11:06:07 20:24:37
123 Srinivas R Kaka 065 Qa Team 12/04/2012 10:30:28 19:47:31
123 Srinivas R Kaka 065 Qa Team 13/04/2012 10:38:22 20:47:39
123 Srinivas R Kaka 065 Qa Team 16/04/2012 11:25:59 19:44:00
123 Srinivas R Kaka 065 Qa Team 17/04/2012 11:43:50 19:54:12
123 Srinivas R Kaka 065 Qa Team 18/04/2012 10:12:31 19:56:19
123 Srinivas R Kaka 065 Qa Team 19/04/2012 10:50:11 20:20:50
123 Srinivas R Kaka 065 Qa Team 21/04/2012 12:09:09 18:30:17
123 Srinivas R Kaka 065 Qa Team 23/04/2012 10:46:34 18:33:16
123 Srinivas R Kaka 065 Qa Team 24/04/2012 10:57:27 19:43:56
123 Srinivas R Kaka 065 Qa Team 25/04/2012 10:35:21 19:00:49
123 Srinivas R Kaka 065 Qa Team 26/04/2012 11:22:14 19:23:42
123 Srinivas R Kaka 065 Qa Team 27/04/2012 10:58:41 14:39:33
12310637 Manikantha 123 System Admin 02/04/2012 10:32:21 19:04:40
12310637 Manikantha 123 System Admin 03/04/2012 10:19:37 19:44:45
12310637 Manikantha 123 System Admin 04/04/2012 10:20:22 19:14:41
12310637 Manikantha 123 System Admin 05/04/2012 09:55:52 19:49:13
12310637 Manikantha 123 System Admin 06/04/2012 09:41:59 19:48:23
12310637 Manikantha 123 System Admin 09/04/2012 10:09:36 19:47:30
12310637 Manikantha 123 System Admin 10/04/2012 10:05:47 19:46:35
12310637 Manikantha 123 System Admin 11/04/2012 10:08:27 20:54:45
12310637 Manikantha 123 System Admin 12/04/2012 10:17:44 20:07:29
12310637 Manikantha 123 System Admin 13/04/2012 10:00:08 20:16:36
12310637 Manikantha 123 System Admin 16/04/2012 10:02:32 19:53:39
12310637 Manikantha 123 System Admin 17/04/2012 09:57:33 21:47:45
12310637 Manikantha 123 System Admin 18/04/2012 10:21:35 18:54:46
12310637 Manikantha 123 System Admin 19/04/2012 09:55:52 22:24:15
12310637 Manikantha 123 System Admin 23/04/2012 09:53:19 19:10:49
12310637 Manikantha 123 System Admin 24/04/2012 09:47:38 19:51:01
12310637 Manikantha 123 System Admin 25/04/2012 10:27:02 19:20:06
12310637 Manikantha 123 System Admin 26/04/2012 10:19:55 19:42:20
12310637 Manikantha 123 System Admin 27/04/2012 09:57:48 19:44:49
12310637 Manikantha 123 System Admin 30/04/2012 09:53:48 20:30:34
12310647 Plain Card 123 Temporary Card 02/04/2012 09:59:55 20:46:42
12310647 Plain Card 123 Temporary Card 03/04/2012 10:37:15 21:56:23
12310647 Plain Card 123 Temporary Card 04/04/2012 10:31:51 19:36:49
12310647 Plain Card 123 Temporary Card 05/04/2012 11:20:40 20:11:41
12310647 Plain Card 123 Temporary Card 06/04/2012 10:19:19 21:18:34
12310647 Plain Card 123 Temporary Card 09/04/2012 10:49:12 19:52:51
12310647 Plain Card 123 Temporary Card 10/04/2012 10:40:18 20:32:47
12310647 Plain Card 123 Temporary Card 11/04/2012 10:05:50 20:23:45
12310647 Plain Card 123 Temporary Card 12/04/2012 10:56:13 19:55:19
12310647 Plain Card 123 Temporary Card 13/04/2012 10:29:22 20:14:39
12310647 Plain Card 123 Temporary Card 16/04/2012 10:48:22 20:32:47
12310647 Plain Card 123 Temporary Card 18/04/2012 09:52:18 21:44:43
12310647 Plain Card 123 Temporary Card 19/04/2012 10:35:28 19:28:30
12310647 Plain Card 123 Temporary Card 23/04/2012 10:32:22 19:09:25
12310647 Plain Card 123 Temporary Card 24/04/2012 11:24:52 19:35:56
12310647 Plain Card 123 Temporary Card 25/04/2012 11:00:23 19:35:40
12310647 Plain Card 123 Temporary Card 26/04/2012 10:59:39 21:04:15
我正在尝试将这些值分组到如下地图中:
private static Map showExcelData(List sheetData) {
String tempEmpid="",strdepartment="";
int j=1;
Map employeeMap = new LinkedHashMap();
Map tempEmployeeMap = new LinkedHashMap();
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
HSSFCell employeeid = (HSSFCell) list.get(0);
HSSFCell department = (HSSFCell) list.get(3);
HSSFCell date = (HSSFCell) list.get(5);
strdepartment = department.getRichStringCellValue().getString();
if(!(employeeid.getRichStringCellValue().getString().equals("EmpID") || date.getRichStringCellValue().getString().equals("Date") || department.getRichStringCellValue().getString().equals("Department"))){
if(!(strdepartment.equals("Temporary Card") || strdepartment.equals("Contractor"))){
employeeMap.put(employeeid.getRichStringCellValue().getString()+"_"+j,date.getRichStringCellValue().getString());
j++;
}
}// System.out.println("");
}
return employeeMap;
}
我认为在这里我可以根据员工 ID 对值进行分组,但坦率地说,我不知道该怎么做,因为我是 Java 中的新人。
所以我尝试从上述方法获取地图并尝试如下处理
employeeMap = showExcelData(sheetData);
String previousEemployeeID = "",employeeID[];
Iterator<Map.Entry> entries = employeeMap.entrySet().iterator();
int i=0;
while (entries.hasNext()) {
Map.Entry entry = entries.next();
employeeID = entry.getKey().toString().split("_");
System.out.println("employeeid : "+employeeID[0]);
if(previousEemployeeID.equals(employeeID[0])){
System.out.println(" If Key = " + entry.getKey() + ", Value = " + entry.getValue()+" : possible key is : "+employeeID[0]+"_"+String.valueOf(i));
i++;
}else{
if(i==0) System.out.println("previousEmployee id : "+entry.getKey());
System.out.println("Else Key = " + entry.getKey() + ", Value = " + entry.getValue()+" : possible key is : "+employeeID[0]+"_"+String.valueOf(i));
i=0;
}
previousEemployeeID = employeeID[0];
}
在这里,我只想根据员工 id 对值进行分组,但实际上得到的答案很糟糕,如果我尝试在 map 的第一个元素上方被消除,而第二个元素的结果很好。
这是完整的代码:
package org.slingemp.attendance;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
public class ReadEmployeeAttendanceDesktop {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
Map employeeMap = new LinkedHashMap();
String fileName = "D:/workspace/FinalFileUpload/SampleData.xls";
// Create an ArrayList to store the data read from excel sheet.
List sheetData = new ArrayList();
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
// Create an excel workbook from the file system.
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook.getSheetAt(0);// gets the first sheet on workbook
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
//count=count+1;
List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
data.add(cell);
}
sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
employeeMap = showExcelData(sheetData);
String previousEemployeeID = "",employeeID[];
Iterator<Map.Entry> entries = employeeMap.entrySet().iterator();
int i=0;
while (entries.hasNext()) {
Map.Entry entry = entries.next();
employeeID = entry.getKey().toString().split("_");
System.out.println("employeeid : "+employeeID[0]);
if(previousEemployeeID.equals(employeeID[0])){
System.out.println(" If Key = " + entry.getKey() + ", Value = " + entry.getValue()+" : possible key is : "+employeeID[0]+"_"+String.valueOf(i));
i++;
}else{
if(i==0) System.out.println("previousEmployee id : "+entry.getKey());
System.out.println("Else Key = " + entry.getKey() + ", Value = " + entry.getValue()+" : possible key is : "+employeeID[0]+"_"+String.valueOf(i));
i=0;
}
previousEemployeeID = employeeID[0];
}
}
private static Map showExcelData(List sheetData) {
String tempEmpid="",strdepartment="";
int j=1;
Map employeeMap = new LinkedHashMap();
Map tempEmployeeMap = new LinkedHashMap();
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
HSSFCell employeeid = (HSSFCell) list.get(0);
HSSFCell department = (HSSFCell) list.get(3);
HSSFCell date = (HSSFCell) list.get(5);
strdepartment = department.getRichStringCellValue().getString();
if(!(employeeid.getRichStringCellValue().getString().equals("EmpID") || date.getRichStringCellValue().getString().equals("Date") || department.getRichStringCellValue().getString().equals("Department"))){
if(!(strdepartment.equals("Temporary Card") || strdepartment.equals("Contractor"))){
employeeMap.put(employeeid.getRichStringCellValue().getString()+"_"+j,date.getRichStringCellValue().getString());
j++;
}
}// System.out.println("");
}
return employeeMap;
}
}
所以请帮我做以下事情:
- 根据员工 ID 对值进行分组。
在此先感谢安东尼拉吉小号