ftl:
<a id="subimport" onclick="subImport()"class="easyui-linkbutton" iconCls="icon-redo" style="display:inline-block" >導入</a>  
<a onclick="expCsv()" class="easyui-linkbutton" iconCls="icon-print" style="display:inline-block" >模板下載</a>
js:
function subImport() {
// probeAuthStatus(function() { // 已登錄
var fileName = $("#productFile").val();
if (fileName == null || fileName == "") {
$.messager.alert("操作提示","未選擇文件,請選擇!",'info');
return;
}
if (fileName != "") {
fileName = fileName.substring(fileName.lastIndexOf("."));
}
if (fileName.toLowerCase() != ".csv") {
$.messager.alert("操作提示","請填入CSV文件的格式!",'info');
return;
}
$("#queryForm").attr("action", base + "/standardSkuCmmdty/importSkuCmmdty.do");
$("#queryForm").submit();
// }, function() {//未登錄狀態
// login();
// });
};
java:
private static final String[] FILE_HEADER = { "SKU商品編碼" };
private static final CSVFormat CSV_FORMAT = CSVFormat.EXCEL.withHeader(FILE_HEADER).withQuote(null)
.withSkipHeaderRecord(true);
public String importProduct(Model model, @RequestParam("productFile") MultipartFile productFile) {
if (productFile == null) {
model.addAttribute(Constant.ERROR_MSG, "導入文件爲空!");
return Constant.COMMON_ERROR_PAGE;
}
try {
Iterable<CSVRecord> records = CSV_FORMAT.parse(new InputStreamReader(productFile.getInputStream(), "GBK"));
if (records != null && records.iterator() != null) {
List<StandardSkuCmmdtylImportErrorVo> errorList = Lists.newArrayList();
int totalSuccessCount = 0;
int i = 2;
for (CSVRecord record : records) {
int recordNum = record.size();
if (recordNum == 1) {
String cmmdtyCode = record.get(0);
// 獲取傳進來的商品編碼;
if (!StringUtils.isEmpty(cmmdtyCode)) {
int result = cmmdtyInfoBusiness.addStandardCmmdty(cmmdtyCode);
if (result != 1) {
StandardSkuCmmdtylImportErrorVo errorInfo = new StandardSkuCmmdtylImportErrorVo();
errorInfo.setCmmdtyCode(cmmdtyCode);
errorInfo.setErrMsg("line:" + i + " add cmmdty error.");
errorList.add(errorInfo);
} else {
totalSuccessCount = totalSuccessCount + 1;
}
}
} else {
model.addAttribute("totalSuccessCount", totalSuccessCount);
model.addAttribute(Constant.ERROR_MSG, "導入模板不對!");
return "standardSkuCmmdty/importResult.ftl";
}
i++;
}
model.addAttribute("totalSuccessCount", totalSuccessCount);
model.addAttribute("errorSize", errorList.size());
model.addAttribute("errorList", errorList);
} else {
model.addAttribute("totalSuccessCount", 0);
model.addAttribute(Constant.ERROR_MSG, "導入文件爲空!");
return "standardSkuCmmdty/importResult.ftl";
}
} catch (IOException e) {
LOGGER.error("解析文件報錯!IO", e);
}
return "standardSkuCmmdty/importResult.ftl";
}
導出功能:
/**
* 導出excel
*/
function expCsv(flag) {
window.location.href = base + "/standardSkuCmmdty/exportCsv.do";
}
@RequestMapping("/exportCsv.do")
public void exportCsv(HttpServletResponse response, HttpServletRequest request, String orgId, String modelFlag) {
String fileName = "標準SKU商品導入模板.csv";
String userAgent = request.getHeader("User-Agent");
// 處理中文名稱的問題
fileName = ExportFileUtil.dealChineseFileName(userAgent, fileName);
PrintWriter writer = null;
// 設置響應
response.setCharacterEncoding("GBK");
response.setContentType("text/csv;charset=GBK");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=30");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
try {
writer = response.getWriter();
CSV_FORMAT.print(writer);
writer.flush();
} catch (IOException e) {
LOGGER.error("生成文件報錯!IO", e);
} finally {
IOUtils.closeQuietly(writer);
}
}
/**
* 導出文件名中文處理
*
* @param userAgent
* @param fileName
* @return
*/
public static String dealChineseFileName(String userAgent, String fileName) {
String fn = fileName;
try {
// 針對IE或者以IE爲內核的瀏覽器:
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
fn = URLEncoder.encode(fileName, "UTF-8");
} else {
// 非IE瀏覽器的處理:
fn = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
}
} catch (UnsupportedEncodingException e1) {
LOGGER.error("文件名轉換出現錯誤", e1);
}
return fn;
}