vue导出(下载)Excel模板
vue导出(下载)Excel模板
代码
导出按钮 代码⽚.
// 导出按钮
<el-button type="primary" size="small" @click="this.downloadMemberTemplate">模板下载</el-button>
js 代码⽚.
async downloadMemberTemplate(){
try{
window.location.href="/api/ics/project/downloadMemberTemplate";
}catch(ex){
this.$(`下载失败:${ex}`)
}
},
java代码⽚.
// 下载导⼊隐患分类模版
@RequestMapping(value ="/downloadMemberTemplate", method = RequestMethod.GET, produces ="application/json;charset=UTF-8") public void downloadHiddenTroubleClassifyMaintainTemplate(HttpServletRequest request, HttpServletResponse response)throws IOException {
String realPathDir = DefaultClassLoader().getResource("").getPath()+TMP_PATH;
String os = Property("os.name");
LowerCase().startsWith("win")){//如果是Windows系统
realPathDir=java.URLDecoder.decode(realPathDir.substring(1,realPathDir.length()),"UTF-8");
}
System.out.println(realPathDir);
realPathDir = CleanPathUtil.cleanString(realPathDir);
File outputFile =new File(realPathDir +"/members_template.xlsx");
DownLoadUtils.download(outputFile,"⼈员范围导⼊模版.xlsx", request, response);
}
⼯具类 CleanPathUtil 代码⽚.
package net.ourwaymon.utils;
import java.util.ArrayList;
import java.util.List;
import Matcher;
import Pattern;
public class CleanPathUtil {
public static String cleanString(String aString){
return aString;
/*  if (aString == null) return null;
String cleanString = "";
try {
for (int i = 0; i < aString.length(); ++i) {
cleanString += cleanChar(aString.charAt(i));
}
cleanString=FileSwitch.vaildFilePath(cleanString);
cleanString=CleanHeadUtil.cleanHead(cleanString);
cleanString=replaceString(cleanString);
cleanString=validFilePath(cleanString);
} catch (Exception e) {
e.printStackTrace();
}
return cleanString;*/
}
public static boolean patternCheck(String str){
public static boolean patternCheck(String str){
if(!Pattern.matches("[0-9A-Za-z@.]+", str)){
return false;
}
return true;
}
private static String replaceString(String filePath)throws Exception {
String fileNameInformation =new Bytes("UTF-8"),"ISO-8859-1");
String fileRegex ="^(?<path>(?:[a-zA-Z]:)?\\\\(?:[^\\\\\\?\\/\\*\\|<>:\"]+\\\\)+)(?<filename>(?<name>[^\\\\\\?\\/\\*\\|<>:\"]+?)\\.(?<ext>[^.\\\\\\?\\/\\*\\|<>:\"]+))$";  Pattern pa = Patternpile(fileRegex);
Matcher ma = pa.matcher(fileNameInformation);
if(ma.find()){
placeAll("");
}
return filePath;
}
public static String validFilePath(String filepath)throws Exception{
List<String> allowedExtensions =new ArrayList<>();
boolean result =false;
allowedExtensions.add(".xml");
allowedExtensions.add(".jar");
allowedExtensions.add(".api");
allowedExtensions.add(".properties");
allowedExtensions.add(".xsd");
allowedExtensions.add(".flw");
allowedExtensions.add(".schema");
allowedExtensions.add(".MF");
allowedExtensions.add(".log");
allowedExtensions.add(".xls");
allowedExtensions.add(".xlsx");
for(String suf:allowedExtensions){
dsWith(suf)){
result =true;
break;
}
}
if(!result){
// 按指定模式在字符串查
String pattern1 ="(.*)(\\.log\\.{0,1}\\d{0,})$";
// 创建 Pattern 对象
Pattern r1 = Patternpile(pattern1);
// 现在创建 matcher 对象
Matcher m1 = r1.matcher(filepath);
if(!m1.matches()){
Exception Exception =new Exception("file name is Illage");
throw Exception  ;
}
}
return filepath;
}
public static boolean isChinese(char c){
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if(ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS){
return true;
}
}
return false;
}
private static char cleanChar(char aChar){
// 0 - 9
for(int i =48; i <58;++i){
if(aChar == i)return(char) i;
}
// 'A' - 'Z'
for(int i =65; i <91;++i){
if(aChar == i)return(char) i;
}
// 'a' - 'z'
for(int i =97; i <123;++i){
if(aChar == i)return(char) i;
}
if(isChinese(aChar))
return aChar;
switch(aChar){
case'/':
return'/';
replaceall()case'.':
return'.';
case'-':
return'-';
case'_':
return'_';
case' ':
return' ';
}
return'%';
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。