SpringMVC 實現Excel匯入
阿新 • • 發佈:2019-01-04
//建立對工作表的引用
Sheet sheet= wb.getSheetAt(1);
//遍歷所有單元格,讀取單元格
int row_num=sheet.getLastRowNum();
//專案名稱
String proName="";
System.out.println("總行數---"+row_num);
for(int i=0 ;i< row_num ;i++){
Row row=sheet.getRow(i);
//獲得專案名稱
if(0==i){
proName=readValue(row.getCell(0));
System.out.println(proName);
}else if(i>3)
//取得第0個單元格的值
//String zeroValue=readValue(row.getCell(0));
{
String zeroValue=readValue(row.getCell(0));
System.out.println("第一個數字式:"+zeroValue);
if(isDig(zeroValue)){
//得到每一行的單元格
DelayTransition delayTran = new DelayTransition();
//生成pmrowId和treeId
delayTran.setPmrowId(PrimaryUtils.generateGuid());
delayTran.setTreeId(delayTran.getPmrowId());
delayTran.setProName(proName);
delayTran.setPname(readValue(row.getCell(1)));
delayTran.setMcardId(readValue(row.getCell(2)));
//這裡的問題主要是有些是用Excel中的計算出來的,匯入時候出現問題
//System.out.println("戶主"+readValue(row.getCell(1))+"身份證號碼"+readValue(row.getCell(2))+"補償面積"+readValue(row.getCell(3)));
System.out.println("第"+i+"行"+"第三列----------"+readValue(row.getCell(3)));
delayTran.setCompeneArea(Double.valueOf(readValue(row.getCell(3))).doubleValue());
delayTran.setDownDate(readValue(row.getCell(4)));
//System.out.println(readValue(row.getCell(5)));
//先將String轉化成float,然後再強制轉化成整型
System.out.println("應安置總人"+readValue(row.getCell(5))+"貨幣安置人數"+readValue(row.getCell(6))+"已住房安置人數"+readValue(row.getCell(7))+"合計已安置人數"+readValue(row.getCell(8)));
delayTran.setShouldPaceCount((int)Double.parseDouble(readValue(row.getCell(5))));
delayTran.setMoneyPaceCount((int)Double.parseDouble(readValue(row.getCell(6))));
delayTran.setHousePaceCount((int)Double.parseDouble(readValue(row.getCell(7))));
int alreadyPaceCount = (int)Double.parseDouble(readValue(row.getCell(6)))+(int)Double.parseDouble(readValue(row.getCell(7)));
delayTran.setAlreadyPaceCount(alreadyPaceCount);
int notPaceCount = (int)Double.parseDouble(readValue(row.getCell(5)))-alreadyPaceCount;
System.out.println(notPaceCount);
delayTran.setNotPaceCount(notPaceCount);
//這裡需要將string轉換成doubule型別
double compeneArea = Double.valueOf(readValue(row.getCell(3))).doubleValue();
int shouldPaceCount = (int)Double.parseDouble(readValue(row.getCell(5)));
double area = (compeneArea/shouldPaceCount)*notPaceCount;
delayTran.setArea(area);
double monthPayMoney = 12*area;
delayTran.setMonthPayMoney(monthPayMoney);
double thisPayMoney = monthPayMoney*3;
delayTran.setThisPayMoney(thisPayMoney);
delayTran.setRemark(readValue(row.getCell(13)));
//過濾身份證資訊
map.put(readValue(row.getCell(2)), delayTran);
}else{
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
}
return map;
}
public static void main(String[] args) {
}
}