最近需要讀取從客戶端發送過來的excel文件內容,excel文件的格式是事先規定好的不變的。所以就研究了一下,因爲是自己寫的需要測試所以先寫了個簡單的網頁上傳文件,看客戶端上傳文件代碼:
<html>
<body>
<div class="offset1 span10 container-fluid">
<form id="firmForm" class="form-horizontal"
action="/front/cashFlow/payForAnotherInformationList.htm"
method="post" enctype="multipart/form-data" accept-charset="utf-8">
<div class="control-group">
<div class="control-group">
<label class="control-label" for="input01">上傳文件:</label>
<div class="controls" style="padding-top: 5px">
<input id="entrustFile" name="entrustFile" type="file" value="文件">
</div>
</div>
<input name="btnSubmit" type="button" class="tjBtn" id="btnSubmit"
value="提交" onclick="javascript:submit();"/>
</form>
</div>
</body>
</html>
<script type="text/javascript">
function submit() {
document.getElementById("firmForm").submit();
}
</script>
客戶端的工作完成了可以上傳文件測試了,現在主要介紹服務端的工作,首先我爲了解析excel文件的內容導入一個框架:poi-3.9-20121203.jar
然後我們第一步要做的是從request中獲取到這個文件,將HttpServletRequest類型的請求強轉成MultipartHttpServletRequest類型的請求,然後MultipartHttpServletRequest有個方法,可以通過文件名獲取文件,獲取到文件後通過getInputStream()函數獲取文件內容:
/**
* Return the contents plus description of an uploaded file in this request,
* or {@code null} if it does not exist.
* @param name a String specifying the parameter name of the multipart file
* @return the uploaded content in the form of a {@link MultipartFile} object
*/
MultipartFile getFile(String name);
接下來直接看源碼:
/**
* 讀取excel文件
*/
public List<CashOutRecord> haveFileFromClient(HttpServletRequest request) throws IOException {
// 轉型爲MultipartHttpRequest:
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 獲得文件:
MultipartFile contactFile = multiRequest.getFile("entrustFile");
// 獲取的代付信息列表
List<CashOutRecord> resultList = new ArrayList<CashOutRecord>();
if (null != contactFile && !contactFile.isEmpty()) {
//解析文檔
POIFSFileSystem fs;
//獲取整個文檔
HSSFWorkbook wb;
// 頁
HSSFSheet sheet;
// 行
HSSFRow row;
// 打開文件
try {
fs = new POIFSFileSystem(contactFile.getInputStream());
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
wb = new HSSFWorkbook();
}
String entrustNo = RandomStringUtils.randomNumeric(9);
//獲取第一頁
sheet = wb.getSheetAt(0);
// 得到總行數
int rowNum = sheet.getLastRowNum();
// 正文內容應該從第二行開始,第一行爲表頭的標題
for (int i = 1; i <= rowNum; i++) {
String outNo = entrustNo + "_" + RandomStringUtils.randomNumeric(6);
row = sheet.getRow(i);
String accountType = "";
String accountName = "";
String accountNum = "";
String bankName = "";
String bankProv = "";
String bankCity = "";
BigDecimal amount = null;
String bankBranch = "";
String reason = "";
try {
int idx = 0;
idx++;
accountType = getCellFormatValue(row.getCell(idx));
idx++;
accountName = getCellFormatValue(row.getCell(idx));
idx++;
accountNum = getCellFormatValue(row.getCell(idx));
idx++;
bankName = getCellFormatValue(row.getCell(idx));
idx++;
bankProv = getCellFormatValue(row.getCell(idx));
idx++;
bankCity = getCellFormatValue(row.getCell(idx));
idx++;
amount = new BigDecimal(getCellFormatValue(row.getCell(idx)));
idx++;
bankBranch = getCellFormatValue(row.getCell(idx));
idx++;
reason = getCellFormatValue(row.getCell(idx));
CashOutRecord cashEntrustRecord = new CashOutRecord();
cashEntrustRecord.setOutNo(outNo);
cashEntrustRecord.setAccountname(accountName);
cashEntrustRecord.setAccountnum(accountNum);
cashEntrustRecord.setBankname(bankName);
cashEntrustRecord.setBankbranch(bankBranch);
cashEntrustRecord.setBankprov(bankProv);
cashEntrustRecord.setBankcity(bankCity);
cashEntrustRecord.setAmount(amount);
if (accountType.equals("個人賬戶")) {
cashEntrustRecord.setAccounttype(11);
} else if (accountType.equals("企業賬戶")) {
cashEntrustRecord.setAccounttype(12);
}
cashEntrustRecord.setRemark(reason);
resultList.add(cashEntrustRecord);
} catch (Exception ex) {
System.out.print(ex);
}
}
return resultList;
}
return null;
}
----------
對於每個excel文件的表格中內容的格式我們也要做處理
----------
/**
* 匹配excel表格cell的類型
*/
private String getCellFormatValue(HSSFCell cell) {
String cellvalue = "";
if (cell != null) {
// 判斷當前Cell的Type
switch (cell.getCellType()) {
// 如果當前Cell的Type爲NUMERIC
case HSSFCell.CELL_TYPE_NUMERIC:
case HSSFCell.CELL_TYPE_FORMULA: {
// 判斷當前的cell是否爲Date
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellvalue = sdf.format(date);
}
// 如果是純數字
else {
// 取得當前Cell的數值
cellvalue = String.valueOf(cell.getNumericCellValue());
}
break;
}
// 如果當前Cell的Type爲STRIN
case HSSFCell.CELL_TYPE_STRING:
// 取得當前的Cell字符串
cellvalue = cell.getRichStringCellValue().getString();
break;
// 默認的Cell值
default:
cellvalue = " ";
}
} else {
cellvalue = "";
}
return cellvalue;
}