我需要从Excel文件中导入数据到数据库,为此,我想检查所选文件的扩展名。
这是我的代码:
这是我的代码:
String filename = file.getName();
String extension = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
String excel = "xls";
if (extension != excel) {
JOptionPane.showMessageDialog(null, "Choose an excel file!");
}
else {
String filepath = file.getAbsolutePath();
JOptionPane.showMessageDialog(null, filepath);
String upload = UploadPoData.initialize(null, filepath);
if (upload == "OK") {
JOptionPane.showMessageDialog(null, "Upload Successful!");
}
}
但我总是得到:
选择一个Excel文件!
我无法找出我的代码有什么问题,请有人帮忙。
extension
变量得到什么值?有关字符串比较的好问题:https://dev59.com/DnRB5IYBdhLWcg3wyqEd - maksimovequalsIgnoreCase()
方法。 - posdef