加载外部 Excel 文件
Load external Excel file
我有一个 Spring 操作,它应该读取 Excel 文件并访问单元格中包含的值。我正在使用 Java Excel API 来处理 Excel 文件。我希望它从外部文件夹中读取,而不是从我的项目的根目录中读取。我遇到的问题是 Spring MVC 无法识别我的文件。我几乎每次都会收到此错误:
The filename, directory name, or volume label syntax is incorrect
我什至尝试过类似的东西:
Resource banner = resourceLoader.getResource("http://howtodoinjava.com/readme.txt");
as seen here,仍然没有运气。我的控制器代码:
@RequestMapping(value="migrateexcel", method = RequestMethod.POST)
public String migrateexcel(Model uiModel, HttpServletRequest httpServletRequest) throws BiffException, IOException {
String fileName = httpServletRequest.getParameter("filename");
if (! fileName.isEmpty() && fileName!= null) {//just doing this as the logic kicks in from a form
FileSystemResource resource = new FileSystemResource("file:C:/Users/Administrator/Desktop/myfolder/test.xlsx");
File xlsFile = resource.getFile();
String temp = "";
Workbook workbook = Workbook.getWorkbook(xlsFile);
Sheet sheet = workbook.getSheet(0);
for(int i =0; i<sheet.getRows(); i++){
for(int j =0; j < sheet.getColumns(); j++){
Cell cell = sheet.getCell(j,i);
temp = cell.getContents();
System.out.println(temp);
}
}
uiModel.addAttribute("message", "Excel Data migrated successfully");
return "rates/processexcel";
}else{
uiModel.addAttribute("message", "Please select a file before submiting");
return "rates/processexcel";
}
}
我测试了一下,似乎唯一的问题是你引用文件的方式。
这两种方式应该可行(除非存在某种权限问题):
"C:/Users/Administrator/Desktop/myfolder/test.xlsx"
或
"C:\Users\Administrator\Desktop\myfolder\test.xlsx"
Spring 和 Spring MVC 实际上与问题无关,但 FileSystemResource
的构造函数期望字符串或 java.io.File
中的语法相同。
我有一个 Spring 操作,它应该读取 Excel 文件并访问单元格中包含的值。我正在使用 Java Excel API 来处理 Excel 文件。我希望它从外部文件夹中读取,而不是从我的项目的根目录中读取。我遇到的问题是 Spring MVC 无法识别我的文件。我几乎每次都会收到此错误:
The filename, directory name, or volume label syntax is incorrect
我什至尝试过类似的东西:
Resource banner = resourceLoader.getResource("http://howtodoinjava.com/readme.txt");
as seen here,仍然没有运气。我的控制器代码:
@RequestMapping(value="migrateexcel", method = RequestMethod.POST)
public String migrateexcel(Model uiModel, HttpServletRequest httpServletRequest) throws BiffException, IOException {
String fileName = httpServletRequest.getParameter("filename");
if (! fileName.isEmpty() && fileName!= null) {//just doing this as the logic kicks in from a form
FileSystemResource resource = new FileSystemResource("file:C:/Users/Administrator/Desktop/myfolder/test.xlsx");
File xlsFile = resource.getFile();
String temp = "";
Workbook workbook = Workbook.getWorkbook(xlsFile);
Sheet sheet = workbook.getSheet(0);
for(int i =0; i<sheet.getRows(); i++){
for(int j =0; j < sheet.getColumns(); j++){
Cell cell = sheet.getCell(j,i);
temp = cell.getContents();
System.out.println(temp);
}
}
uiModel.addAttribute("message", "Excel Data migrated successfully");
return "rates/processexcel";
}else{
uiModel.addAttribute("message", "Please select a file before submiting");
return "rates/processexcel";
}
}
我测试了一下,似乎唯一的问题是你引用文件的方式。
这两种方式应该可行(除非存在某种权限问题):
"C:/Users/Administrator/Desktop/myfolder/test.xlsx"
或
"C:\Users\Administrator\Desktop\myfolder\test.xlsx"
Spring 和 Spring MVC 实际上与问题无关,但 FileSystemResource
的构造函数期望字符串或 java.io.File
中的语法相同。