html代碼
<input type="file" name="an" id="import_file" onchange="changes()"/>
<a href="javascript:void(0)" onclick="importExcel()" class="easyui-linkbutton" data-options="iconCls:'icon-save'">提交</a>
js代碼
function importExcel(){
var names=jQuery("#import_file").val();
if(names==null||names==""){
jQuery.messager.alert('系統提示','請選擇要導入的文件模版!','');
return;
}
var isValid = jQuery("#submitExcel").form('validate');
if(isValid){
// 提交導入表單
jQuery("#submitExcel").form("submit",{
url : '$!link.getContextPath()/base/baseStation_importExcel.htm',
});
}
}
java後臺方法
//導入數據庫
public String importExcel() throws BiffException, IOException, InvalidFormatException{
List<BaseStation> list=new ArrayList<BaseStation>();
List<BaseStation> baseList=new ArrayList<BaseStation>();
String msg="";
String extName = anFileName.substring(anFileName.lastIndexOf(".")+1);
boolean flag=false;
if("xlsx".endsWith(extName)){
//解析excel
XSSFWorkbook rwb = new XSSFWorkbook(an[0]);
XSSFSheet sheet = rwb.getSheetAt(0);
int totalRows = sheet.getLastRowNum()+1;//總行數
int cols;
try{
if(totalRows>1){
for(int i=1;i<totalRows&&!flag;i++){
XSSFRow row=sheet.getRow(i);//讀取某一行數據
//獲取行中所有列數據
cols=row.getLastCellNum();
for(int j=0;j<cols;j++){
BaseStation basestation=new BaseStation();
String stationCode=getCellString(row.getCell(j++));
if(StringUtils.isEmpty(stationCode)&&stationCode==""){
flag=true;
msg="導入失敗,請填寫基站編號";
break;
}
Map<String,Object> params=new HashMap<String, Object>();
params.put("baseStationCode", stationCode);
List<BaseStation> p=basestationManager.query(params);
if(p.size()<1||p.isEmpty()){
basestation.setBaseStationCode(stationCode);
String stationName=getCellString(row.getCell(j++));
if(StringUtils.isNotEmpty(stationName)){
basestation.setBaseStationName(stationName);
}
String provice=getCellString(row.getCell(j++));
if(StringUtils.isNotEmpty(provice)){
DictItem pr=dictItemManager.getDictByName(provice);
basestation.setProvice(pr.getItemId());
}
//所屬市
String city=row.getCell(j++).getStringCellValue();
DictItem dy=dictItemManager.getDictByName(city);
basestation.setCity(dy.getItemId());
//所屬區縣
String county=row.getCell(j++).getStringCellValue();
DictItem d=dictItemManager.getDictByName(county);
basestation.setCounty(d.getItemId());//所屬區縣
//類型
String basestationType=row.getCell(j++).getStringCellValue();
DictItem type=dictItemManager.getDictByName(basestationType);
basestation.setStationType(type.getItemId());
//地址
String address=getCellString(row.getCell(j++));
if(StringUtils.isNotEmpty(address)){
basestation.setAddress(address);
}
//狀態
basestation.setStatus(1);
list.add(basestation);
}
}else{
msg="導入失敗,請輸入導入信息!";
}
}catch(Exception e){
e.printStackTrace();
msg="導入失敗,請檢查Excel信息是否完整!";
}
}
try{
if(baseList.size()>0){
LinkedHashMap<String, String> map=new LinkedHashMap<String, String>();
//excel中字段名集合
map.put("baseStationCode", "編號");
map.put("baseStationName", "名稱");
map.put("proviceName", "省");
map.put("stationTypeName", "類型");
map.put("address", "地址");
HSSFWorkbook workBook = new HSSFWorkbook();// 創建一個Excel文件
String fileName = "未導入基站信息.xls";
try {
fileName=new String(fileName.getBytes("GBK"),"ISO-8859-1");
ExcelUtils.listToExcel(baseList, map, "問題信息數據",response,fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
if(list.size()>0){
basestationManager.save(list);
msg="導入成功!";
}
}catch(Exception e){
e.printStackTrace();
msg="導入失敗,請檢查Excel文件格式是否正確!";
}
request.setAttribute("msg", msg);
return list();
}