前端代碼:
1 <!--導入數據開始--> 2 <div class='container-fluid' style="color: white;"> 3 <div class='row-fluid' id='content-wrapper' style="display: block;"> 4 <div class="modal-add hide fade in" id="modal-addsubject" role="dialog" aria-hidden="false" style="display:none;height:20%;"> 5 <div class="modal-header" style="border-bottom:0;"> 6 <button class="close" data-dismiss="modal" type="button" style="color:white;opacity:1;">×</button> 7 <h3 style="color: white;"><div class="icon-edit"></div> 導入數據</h3> 8 </div> 9 <div class="modal-body" id="add_modify_subject"> 10 <form id='upload' enctype='multipart/form-data' action='' method='post' style='text-align:left;'> 11 <input name="fileBuildInfo1" id='fileBuildInfo1' class='big' type='file' mutiple="multiple" /> 12 </form> 13 </div> 14 <div class="modal-footer" style="border-top:0;box-shadow:none;-webkit-box-shadow:none;"> 15 <button class="btn btn-primary" style="background-color:#09abeb;border-style:solid;border-width:0.2px;border-color:#7cf8ff;" onclick="uploadFile()"><i class="icon-save"></i> 保存</button> 16 </div> 17 </div> 18 </div> 19 </div> 20 <!--導入數據結束-->
js方法:
1 //上傳excel數據文件 2 function uploadFile(){ 3 $('#upload').ajaxSubmit({ 4 url: "/UploadFiles/uploadexcelfile", 5 data: { 6 'filetype': "excel表格", 7 'type':case_type 8 }, 9 cache: false, 10 dataType: 'json', 11 success: function(data) { 12 if (data.result == "success") { 13 layer.msg('上傳成功!', { 14 icon: 1 15 }); 16 } else { 17 layer.msg('上傳失敗!', { 18 icon: 2 19 }); 20 } 21 }, 22 error: function() { 23 layer.msg('錯誤!', { 24 icon: 2 25 }); 26 } 27 }); 28 if(case_type=="ms"){ 29 select_btn(1); 30 }else if(case_type=="xs"){ 31 select_btn(2); 32 }else{ 33 select_btn(3); 34 } 35 $("#modal-addsubject").modal("hide"); 36 }
后台代碼:
1 package controllers; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.text.DecimalFormat; 8 import java.util.ArrayList; 9 import java.util.Date; 10 import java.util.HashMap; 11 import java.util.List; 12 import java.util.Map; 13 14 import org.apache.commons.io.FilenameUtils; 15 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 16 import org.apache.poi.ss.usermodel.Cell; 17 import org.apache.poi.ss.usermodel.CellValue; 18 import org.apache.poi.ss.usermodel.DateUtil; 19 import org.apache.poi.ss.usermodel.FormulaEvaluator; 20 import org.apache.poi.ss.usermodel.Row; 21 import org.apache.poi.ss.usermodel.Sheet; 22 import org.apache.poi.ss.usermodel.Workbook; 23 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 24 25 import models.ResultInfo; 26 import play.Play; 27 import play.data.Upload; 28 import play.libs.Files; 29 30 /** 31 * @author 32 * @date 2018年6月7日 上午9:01:52 33 * @Description 34 */ 35 public class UploadFiles extends BaseController { 36 37 // Excel 2003 38 private final static String XLS = "xls"; 39 // Excel 2007 40 private final static String XLSX = "xlsx"; 41 // 分隔符 42 private final static String SEPARATOR = "|"; 43 44 static String projectpath = Play.applicationPath.getPath().toString(); 45 static String path = projectpath + "\\public\\Filebakup\\excelfile\\"; 46 static int pagesize = 15; 47 static int errcount = 0; 48 static String rownum = ""; 49 50 // 上傳excel數據 51 public static void uploadexcelfile(String type, String filetype) 52 throws IOException { 53 List<Upload> files = null; 54 errcount = 0; 55 rownum = ""; 56 while (true) { 57 files = (List<Upload>) request.args.get("__UPLOADS"); 58 if (files != null) { 59 break; 60 } 61 } 62 List<String> filepaths = new ArrayList<String>(); 63 int sign = 0; 64 for (Upload upload : files) { 65 if (upload.getSize() > 0) { 66 File f = upload.asFile(); 67 String fileName = f.getName(); 68 File storeFile = new File(path + fileName); 69 String allpath = path + fileName; 70 filepaths.add(allpath); 71 Files.copy(f, storeFile); 72 List<String> list = null; 73 list = exportListFromExcel(new File(allpath), 0, type); 74 if (list.size() > 0) { 75 sign += 1; 76 } 77 } 78 } 79 ResultInfo result = new ResultInfo(); 80 if (sign > 0) { 81 renderJSON(result.success("success", refreshExpire())); 82 } else { 83 renderJSON(result.error("error", refreshExpire())); 84 } 85 } 86 87 public static List<String> exportListFromExcel(File file, int sheetNum, 88 String type) throws IOException { 89 return exportListFromExcel(new FileInputStream(file), 90 FilenameUtils.getExtension(file.getName()), sheetNum, type); 91 } 92 93 public static List<String> exportListFromExcel(InputStream is, 94 String extensionName, int sheetNum, String type) throws IOException { 95 Workbook workbook = null; 96 if (extensionName.toLowerCase().equals(XLS)) { 97 workbook = new HSSFWorkbook(is); 98 } else if (extensionName.toLowerCase().equals(XLSX)) { 99 workbook = new XSSFWorkbook(is); 100 } 101 return exportListFromExcel(workbook, sheetNum, type); 102 } 103 104 private static List<String> exportListFromExcel(Workbook workbook, 105 int sheetNum, String type) { 106 Date uploadtime = new Date(); 107 Sheet sheet = workbook.getSheetAt(sheetNum); 108 // 解析公式結果 109 FormulaEvaluator evaluator = workbook.getCreationHelper() 110 .createFormulaEvaluator(); 111 List<String> list = new ArrayList<String>(); 112 int minRowIx = sheet.getFirstRowNum() + 1; 113 if (!"".equals(type) && type != null && type.equals("qlqd")) { 114 minRowIx = minRowIx + 1; 115 } 116 int maxRowIx = sheet.getLastRowNum(); 117 for (int rowIx = minRowIx; rowIx <= maxRowIx; rowIx++) { 118 Row row = sheet.getRow(rowIx); 119 int mnnumber = rowIx - 1; 120 String mnnbs = ""; 121 if (mnnumber < 10) { 122 mnnbs = "000" + mnnumber; 123 } else if (mnnumber >= 10 && mnnumber < 100) { 124 mnnbs = "00" + mnnumber; 125 } else if (mnnumber >= 100 && mnnumber < 1000) { 126 mnnbs = "0" + mnnumber; 127 } else if (mnnumber >= 1000) { 128 mnnbs = "" + mnnumber; 129 } 130 StringBuilder sb = new StringBuilder(); 131 short minColIx = (short) (row.getFirstCellNum()); 132 short maxColIx = row.getLastCellNum(); 133 for (short colIx = minColIx; colIx <= maxColIx; colIx++) { 134 Cell cell = row.getCell(new Integer(colIx)); 135 CellValue cellValue = evaluator.evaluate(cell); 136 String cellstr = ""; 137 if (cellValue == null) { 138 sb.append(SEPARATOR + " "); 139 continue; 140 } 141 // 經過公式解析,最后只存在Boolean、Numeric和String三種數據類型,此外就是Error了 142 // 其余數據類型,根據官方文檔,完全可以忽略http://poi.apache.org/spreadsheet/eval.html 143 switch (cellValue.getCellType()) { 144 case Cell.CELL_TYPE_BOOLEAN: 145 sb.append(SEPARATOR + cellValue.getBooleanValue()); 146 break; 147 case Cell.CELL_TYPE_NUMERIC: 148 // 這里的日期類型會被轉換為數字類型,需要判別后區分處理 149 if (DateUtil.isCellDateFormatted(cell)) { 150 sb.append(SEPARATOR 151 + util.DateUtil.date2String(cell 152 .getDateCellValue())); 153 } else { 154 sb.append(SEPARATOR + new DecimalFormat("#").format(cellValue.getNumberValue())); 155 } 156 break; 157 case Cell.CELL_TYPE_STRING: 158 sb.append(SEPARATOR + cellValue.getStringValue()); 159 break; 160 case Cell.CELL_TYPE_FORMULA: 161 break; 162 case Cell.CELL_TYPE_BLANK: 163 break; 164 case Cell.CELL_TYPE_ERROR: 165 break; 166 default: 167 break; 168 } 169 } 170 if (!sb.toString() 171 .trim() 172 .equals("| | | | | | | | | | | | | | | | | | | | | | | | | | | |")) { 173 if (!"".equals(type) && type != null && type.equals("ms")) { 174 MSCaseInfoController.addmscase(sb.toString(), uploadtime); 175 } else if (!"".equals(type) && type != null 176 && type.equals("xs")) { 177 XSCaseInfoController.addxscase(sb.toString(), uploadtime); 178 } else if (!"".equals(type) && type != null 179 && type.equals("ks")) { 180 KSCaseInfoController.addkscase("部門受案號", sb.toString(), 181 uploadtime); 182 } else { 183 PowerListController.addPowerList(sb.toString()); 184 } 185 } 186 list.add(sb.toString()); 187 } 188 return list; 189 } 190 191 }