java程序导入Excel表数据

来源:互联网 发布:陌生人语音聊天软件 编辑:程序博客网 时间:2024/06/06 08:36

程序如下:

File file = new File("D:\\1-2.xls");
    String vehicleidiac1 = "";
    int count = 0;
    Workbook wb = null;
try {
FileInputStream fileInputStream = new FileInputStream(file);
wb = Workbook.getWorkbook(fileInputStream);
} catch (FileNotFoundException e) {
System.out.println("文件导入错误");
e.printStackTrace();
} catch (BiffException e) {
System.out.println("文件流获取错误");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    Sheet sheet = wb.getSheet(0);
    int nRow = sheet.getRows();
    int nColumn = sheet.getColumns();
    System.out.print("转化前-----------");
System.out.println("转化后");
String nhql1 = "from YWRelation";
List<YWRelation> ywRelations1 = this.findByHql(nhql1);
for(int j = 1;j < nRow; j++){
String[] rowData = new String[nColumn];
Cell[] cells = sheet.getRow(j);
for (int k = 0; k < nColumn; k++) {
 synchronized (cells[k].getContents()) {
 Boolean istrue = false;
rowData[k] = cells[k].getContents(); 
try {
String vehicleidiac1 = "";
for(YWRelation ywRelation : ywRelations1){
if(rowData[k].equals(ywRelation.getVehiclejycode().trim())){
//解码
istrue = true;
vehicleidiac1 = RuleUtils.decodeJyCode(ywRelation.getVehiclecode().trim());
if(vehicleidiac1.equals("")&&vehicleidiac1.length()==0||"BXPTYXCL9999".equals(vehicleidiac1)){
vehicleidiac1="BXPTYXCL9999";
}
logger_debug.info(rowData[k]+"   "+vehicleidiac1);
continue;
}
}
if(istrue == false){
logger_debug.info(rowData[k]+"   "+"BXPTYXCL9999");
}
} catch (Exception e) {
System.out.println("录入错误");
e.printStackTrace();
}
}
}

}