【发布时间】:2019-04-15 01:38:25
【问题描述】:
需要用于数据库集成(CRUD 操作)的代码
如何使用 Spring MVC 上传 Excel 文件? MultipartFile 类提供对上传文件的详细信息的访问,包括文件名、文件类型等。我们可以使用一个简单的 HTML 页面来显示这些信息:
我们还可以将附加信息与正在上传的文件一起发送到服务器。我们只需在表单中包含必填字段:
FileUploadService.java
public class FileUploadService {
@Autowired
FileUploadDao fileUploadDao;
public String uploadFileData(String inputFilePath){
Workbook workbook = null;
Sheet sheet = null;
try
{
workbook = getWorkBook(new File(inputFilePath));
sheet = workbook.getSheetAt(0);
/*Build the header portion of the Output File*/
String headerDetails= "EmployeeId,EmployeeName,Address,Country";
String headerNames[] = headerDetails.split(",");
/*Read and process each Row*/
ArrayList<ExcelTemplateVO> employeeList = new ArrayList<>();
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext())
{
Row row = rowIterator.next();
//Read and process each column in row
ExcelTemplateVO excelTemplateVO = new ExcelTemplateVO();
int count=0;
while(count<headerNames.length){
String methodName = "set"+headerNames[count];
String inputCellValue = getCellValueBasedOnCellType(row,count++);
setValueIntoObject(excelTemplateVO, ExcelTemplateVO.class, methodName, "java.lang.String", inputCellValue);
}
employeeList.add(excelTemplateVO);
}
fileUploadDao.saveFileDataInDB(employeeList);
}
catch(Exception ex){
ex.printStackTrace();
}
return "Success";
}
【问题讨论】:
标签: java excel spring-mvc file-upload