如何使用java在resultset中存储excel数据
我需要将excel数据(按行)与数据库表进行比较 谁能帮我一下吗 我可以用下面的代码正确地按行和按列读取excel文件如何使用java在resultset中存储excel数据,java,excel,resultset,Java,Excel,Resultset,我需要将excel数据(按行)与数据库表进行比较 谁能帮我一下吗 我可以用下面的代码正确地按行和按列读取excel文件 public class POC { public static void main(String[] args) { String colText = null; XSSFWorkbook workbook; Cell cell = null; try { HashSet<String> xlRead = new
public class POC {
public static void main(String[] args) {
String colText = null;
XSSFWorkbook workbook;
Cell cell = null;
try {
HashSet<String> xlRead = new HashSet<String>();
InputStream myxls = new FileInputStream(new File(
"excelpath.xlsx"));
workbook = new XSSFWorkbook(myxls);
Sheet sheet = workbook.getSheetAt(0); // Get Your Sheet.
java.util.Iterator<Row> rowterator = sheet.rowIterator();
while (rowterator.hasNext()) {
Row row = rowterator.next();
java.util.Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
xlRead.add(cell.getStringCellValue() + "");
System.out.println(cell.getStringCellValue());
default:
break;
}
}
}
System.out.println("");
myxls.close();
for (String s : xlRead)
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
公共类POC{
公共静态void main(字符串[]args){
字符串colText=null;
XSSF工作手册;
Cell=null;
试一试{
HashSet xlRead=新的HashSet();
InputStream myxls=新文件InputStream(新文件(
“excelpath.xlsx”);
工作簿=新XSSF工作簿(myxls);
工作表=工作簿。getSheetAt(0);//获取工作表。
java.util.Iterator rowterator=sheet.rowterator();
while(rowterator.hasNext()){
Row=rowterator.next();
java.util.Iterator cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
cell=cellIterator.next();
开关(cell.getCellType()){
case Cell.Cell\u类型\u字符串:
xlRead.add(cell.getStringCellValue()+);
System.out.println(cell.getStringCellValue());
违约:
打破
}
}
}
System.out.println(“”);
myxls.close();
用于(字符串s:xlRead)
系统输出打印项次;
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
建议您创建一个对象来存储数据库中的数据(为此使用JPA),并使用客户实现的compareTo(对象对象)
。然后,您可以使用同一对象存储从excel获取的数据,并使用您创建的“compareTo”方法比较这两个对象
@Entity
@Table(name="Age")
public class MyAge() {
public MyAge();
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
@Column(name="age")
private int age;
... getters and setters, though lombok is better ...
@Override
public int compareTo(MyAge myAge) {
return Integer.compare(age, myAge.age);
}
}