1.Angular 文件上传–》前端
<nz-upload [nzBeforeUpload]="noType" [nzFilter]="filters" >
<button nz-button [nzType]="'primary'" >确认button>
nz-upload>
/**
* 上传文件格式校验
*/
filters: UploadFilter[] = [
{
name: 'type',
fn: (fileList: UploadFile[]) => {
const filterFiles = fileList.filter(w => ~['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'].indexOf(w.type));
if (filterFiles.length !== fileList.length) {
this.msg.error(`包含文件格式不正确,只支持 excel 格式`);
return filterFiles;
}
return fileList;
}
}
];
//自定义文件上传
noType = (file: File): boolean => {
this.getExcelData(file)
return false;
}
//获取Excel数据
getExcelData(file: any) {
const formData = new FormData();
formData.append('file', file);
//上传文件并解析excel
this.http.post('api/web/web/upload/file', formData).subscribe((res: any) => {
})
}
2.后端读取Excel数据–》核心代码
/**
* @param input Excel文件的输入流
* @return
*/
public static Workbook readExcel(InputStream input) {
try {
Workbook workbook = WorkbookFactory.create(input); // 这种方式 Excel2003/2007/2010都是可以处理的
// Sheet的数量
int sheetCount = workbook.getNumberOfSheets(); //工作表的数量
for (int n = 0; n < sheetCount; n++) {
Sheet sheet = workbook.getSheetAt(n);
String sheetName = sheet.getSheetName();//获取sheet工作页的名称
int rowCount = sheet.getLastRowNum() + 1;//总行数
for (int i = 0; i < rowCount; i++) {
Row row = sheet.getRow(i);
if (row != null) {
// 总列数
int cellCount = row.getLastCellNum();
for (int j = 0; j < cellCount; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
getValue(cell)//每一列的数据
}
}
}
}
}
return workbook;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 功能描述:
* <根据列属性获取参数值>
* @param cell 1
* @return java.lang.Object
*/
private static Object getValue(Cell cell) {
Object obj = "";
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
obj = cell.getBooleanCellValue();
break;
case ERROR:
obj = cell.getErrorCellValue();
break;
case NUMERIC:
obj = cell.getNumericCellValue();
break;
case STRING:
obj = cell.getStringCellValue();
break;
default:
break;
}
return obj;
}
3.保存文件在tomcat服务器上–》
/**
* 自定义文件上传,上传Ft至p服务器
* 1.将前端传入的MultipartFile 类型转换为InputStream
*
* @param file 前端传来的文件的二进制数据流
* @param parentPath 传进来的地址--》tomcat地址获取 request.getSession().getServletContext().getRealPath("/");
* @return
*/
public static String uploadFile(MultipartFile file, String parentPath) {
String url = "";
String filename = file.getOriginalFilename();//获取MultipartFile 类型的文件名
String path = parentPath + getTimepath();//tomcat 存储路径
File tomcatfile=null;
try {
destFile = new File(makefile(parentPath) +File.separator + name);
} catch (IOException e1) {
e1.printStackTrace();
}
// 转存文件
try {
file.transferTo(destFile);
} catch (IllegalStateException | IOException e) {
LOGGER.info("文件上传异常");
}
return path ;
}
4.Angular文件下载–》通过url
download(url){//传入文件地址
var iframe = document.createElement("iframe")
iframe.style.display = "none";
iframe.src = environment.FILE_URL + url;//服务器地址+文件地址
document.body.appendChild(iframe);
}
5.后端根据excel数据编写Excel文件–》核心代码
/**
* 将excel数据+标题写入tomcat服务器中
* 1。将数据写
* @param url ftp中的地址
* @param file_name 文件名称
* @param list //标题+内容 --> 一行一行展示
* 列编号
* @throws Exception
*/
public static String downloadExcel(String url, String file_name, List<HashMap<String, String>> list, List<Integer> rowcode) throws Exception {
Workbook workbook = new HSSFWorkbook();//创建一个新的工作簿
int sheetNum = 1;
Sheet sheet = workbook.createSheet(); //创建sheet页
for (int i = 0; i < list.size(); i++) {
Row row = sheet.createRow(i);//创建行
for (int j = 0; j < rowcode.size(); j++) {
String key = rowcode.get(j).toString();
String value = list.get(i).get(key);
row.createCell(rowcode.get(j)).setCellValue(value);//创建单元格并设值
}
}
File file = new File(url + File.separator + file_name);
FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);
fileOut.close();
return file.getCanonicalPath() + file_name;
}