Excel上傳示例及後臺解析Excel
<form action="../module/AccountInfoBatch-input.action" method="post" id="formId" enctype="multipart/form-data">
檔案:<input type="file" name="file1" id="fileId"/>
<input id="tijiao" type="submit" style="margin-right: 10px; margin-top: 70px;" class="btn btn-md btn-primary" value="提交"></input>
</form>
<script>
function submitData() {
onsubmit ="document.getElementById('tijiao').disabled=true;return true;" ;
var fileId = $("#fileId").val();
if (fileId == "") {
alert("請選擇要上傳的檔案");
return false ;
}
var exec = (/[.]/.exec(fileId)) ? /[^.]+$/.exec(fileId.toLowerCase()) : '';
if (exec != "xlsx") {
alert("檔案格式不對,請上傳Excel檔案!");
return false;
}
$("#tijiao").val("提交ing···");
document.getElementById('tijiao').disabled=true;
$("#formId").submit();
}
</script>
java程式碼(示例):
/**
* 企業電子賬戶批量錄入
*
* @author kyzeng
*
*/
public class AccountInfoBatchAction extends CommonAction {
/**
*
*/
private static final long serialVersionUID = 1L;
@Autowired
BtUnitService btUnitService;
@Autowired
BtConfService btConfService;
private File file1;
public File getFile1() {
return file1;
}
public void setFile1(File file1) {
this.file1 = file1;
}
public String input() throws InvalidFormatException, IOException {
Map<String, Object> result = new HashMap<>();
setResult(result);
String date = new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date());
// 將客戶端的檔案上傳到服務端
String desPath = ServletActionContext.getServletContext().getRealPath("/imags");
File destFile = new File(desPath, date + ".xlsx");
FileUtils.copyFile(file1, destFile);
List<AccountCSVModel> csvList = EXCELPioParse.parseExcel(desPath + File.separator + date + ".xlsx");
destFile.delete();
List<String> msgAccountIdList = new ArrayList<String>();
setResult(result);
StringBuilder msg = new StringBuilder();
for (AccountCSVModel accountCSVModel : csvList) {
long sid;
try {
Map<String, Object> map = btUnitService.getBtUnit(accountCSVModel.getP2p());
sid = ((Number)map.get("SID")).longValue();
} catch (NumberFormatException e1) {
msg.append("電子賬號【" + accountCSVModel.getAccountId() + "】失敗:" + e1.getMessage());
continue;
}
try {
Map<String, Object> btConfCardBinMap = btConfService.getCodeByUnid(sid, Constant.BT_CONF_NAME_CARDBIN);
if (!(accountCSVModel.getAccountId().substring(0, 9)).equals(btConfCardBinMap.get("CODE"))) {
msgAccountIdList.add(accountCSVModel.getAccountId());
msg.append("電子賬號【" + accountCSVModel.getAccountId() + "】與平臺不匹配。");
continue;
}
} catch (Exception e1) {
msg.append("電子賬號【" + accountCSVModel.getAccountId() + "】失敗:" + e1.getMessage());
continue;
}
Map<String, Object> btConfClusterMap;
try {
btConfClusterMap = btConfService.getCodeByUnid(sid, Constant.BT_CONF_NAME_CLUSTER);
} catch (Exception e1) {
msg.append("電子賬號【" + accountCSVModel.getAccountId() + "】失敗:" + e1.getMessage());
continue;
}
try {
insert(accountCSVModel, (String) btConfClusterMap.get("CODE"));
} catch (Exception e) {
msg.append("電子賬號【" + accountCSVModel.getAccountId() + "】失敗:" + e.getMessage() );
continue;
}
}
if (!StringUtils.isEmpty(msg.toString())) {
result.put("code", msg.toString());
}else{
result.put("code", "企業電子賬戶錄入成功。");
}
return JSON_RESULT;
}
private Map<String,String> CLUSTERMAP=ImmutableMap.<String, String>builder()
.put(Constant.CLUSTER_CODE_20, "2.0")
.put(Constant.CLUSTER_CODE_JD, "JD")
.put(Constant.CLUSTER_CODE_KD, "KD")
.put(Constant.CLUSTER_CODE_LL, "LL")
.build();
private void insert(AccountCSVModel accountCSVModel, String code) throws CustomException {
String version = CLUSTERMAP.get(code);
HttpSendService.httpReq(version, genHttpRequest(accountCSVModel), Constant.TRY_HTTP_TIMES);
}
private JSONObject genHttpRequest(AccountCSVModel accountCSVModel) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("p2p", accountCSVModel.getP2p());
jsonObject.put("name", accountCSVModel.getName());
jsonObject.put("idNo", accountCSVModel.getIdNo());
jsonObject.put("idType", accountCSVModel.getIdType());
jsonObject.put("idCard", accountCSVModel.getCardNo());
jsonObject.put("accountId", accountCSVModel.getAccountId());
jsonObject.put("acctUse", accountCSVModel.getAcctUse());
jsonObject.put("openDate", accountCSVModel.getOpenDate());
jsonObject.put("mobile", accountCSVModel.getBindMobile());
return jsonObject;
}
解析Excel:
public static List<AccountCSVModel> parseExcel(String path) throws InvalidFormatException, IOException {
FileInputStream xlsOrxlsxFile = new FileInputStream(path);
List<AccountCSVModel> accountCSVModelList = new ArrayList<AccountCSVModel>();
Workbook wb = WorkbookFactory.create(xlsOrxlsxFile);
int sheetNum = wb.getNumberOfSheets();
Sheet sheet = null;
for (int sheetIndex = 0; sheetIndex < sheetNum; sheetIndex++) {
sheet = wb.getSheetAt(sheetIndex);
Row row = null;
int firstRowNum = sheet.getFirstRowNum()+1;
int lastRowNum = sheet.getLastRowNum();
for (int rowIndex = firstRowNum ; rowIndex <= lastRowNum; rowIndex++) {
row = sheet.getRow(rowIndex);
if (null != row) {
AccountCSVModel accountCVSModel = new AccountCSVModel();
accountCVSModel.setP2p(getValue(row.getCell(1)));
accountCVSModel.setIdNo(getValue(row.getCell(2)));
accountCVSModel.setIdType(getValue(row.getCell(3)));
accountCVSModel.setName(getValue(row.getCell(4)));
accountCVSModel.setBindMobile(getValue(row.getCell(5)));
accountCVSModel.setAcctUse(getValue(row.getCell(6)));
accountCVSModel.setCardNo(getValue(row.getCell(7)));
accountCVSModel.setAccountId(getValue(row.getCell(8)));
accountCVSModel.setOpenDate(getValue(row.getCell(9)));
accountCSVModelList.add(accountCVSModel);
}
}
}
return accountCSVModelList;
}
// 轉換資料格式
private static String getValue(Cell xssfRow) {
if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
return String.valueOf(xssfRow.getBooleanCellValue());
} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
return String.valueOf(xssfRow.getNumericCellValue());
} else {
return String.valueOf(xssfRow.getStringCellValue());
}
}
注意:form表單必須表明:enctype="multipart/form-data"
並且上上傳檔案只能form表單提交。不能ajax。
ajax實現:待實現
相關文章
- 通過ajax上傳excelExcel
- java將Excel檔案上傳並解析為List陣列JavaExcel陣列
- 解析ExcelExcel
- Excel上傳並讀取資料Excel
- 微信公眾號上如何上傳excel表格?Excel
- Java 解析 ExcelJavaExcel
- Java解析ExcelJavaExcel
- Laravel- 後臺批量匯入 ExcelLaravelExcel
- Salesforce LWC學習(三十二)實現上傳 Excel解析其內容SalesforceExcel
- Java解析Excel例項解析JavaExcel
- Excel 特殊分組彙總示例Excel
- PHPExcel讀取excel檔案示例PHPExcel
- poi解析Excel內容Excel
- [BUG反饋]上傳EXCEL想做匯入,發現上傳差不多成功被重置上傳Excel
- 使用Vue+Spring Boot實現Excel上傳VueSpring BootExcel
- vue實現Excel檔案的上傳與下載VueExcel
- JXL包大解析;Java程式生成excel檔案和解析excel檔案內容JavaExcel
- js-xlsx + handsontable + echarts實現excel上傳編輯然後顯示成圖表JSEchartsExcel
- salesforce lightning零基礎學習(十七) 實現上傳 Excel解析其內容SalesforceExcel
- mfc 讀寫 excel 示例 C++ libxlExcelC++
- laravel-admin,Dcat-admin與maatwebsite/excel上傳LaravelWebExcel
- python3+xlrd解析ExcelPythonExcel
- 如何使用Excel傳送郵件?Excel
- 一個後臺靜態模板,自帶excel,print,cvsExcel
- jmeter實際場景應用之測試上傳excel檔案JMeterExcel
- iOS 後臺上傳&資料同步iOS
- DcatAdmin 配合 `maatwebsite/excel` 擴充套件 在後臺增加 excel 匯出的功能,並可匯出圖片WebExcel套件
- 如何解析你,Excel 的 Date 呀Excel
- Retrofit+RxJava上傳圖片上傳圖片到後臺RxJava
- django實現將後臺資料excel檔案形式匯出DjangoExcel
- EXCEL資料上傳到SQL SERVER中的簡單實現方法ExcelSQLServer
- JavaScript魔法:線上Excel附件上傳與下載的完美解決方案JavaScriptExcel
- 使用 JavaScript 上傳 PDF 和 Excel 等二進位制檔案到 ABAP 伺服器並進行解析JavaScriptExcel伺服器
- C#上傳excel,解析主從表,1W資料快速插入資料庫,5s完成C#Excel資料庫
- 踹掉後端,前端匯出Excel!後端前端Excel
- excelExcel
- excel開啟後灰色不顯示內容 excel全是灰色無法編輯Excel
- 遞迴示例-展開編號(Excel函式集團)遞迴Excel函式