excel的上传与解析
https://app.yinxiang.com/fx/1b863972-9a9d-477e-82bf-fe960053068e
关于file
https://blog.csdn.net/sdut406/article/details/85647982
过程中会遇到的问题:
- 【Java excel解析】关于Excel 98-03版本 与 07以后版本的兼容性问题
XSSFWorkbook 可读07以后版本Excel HSSFWork 读03之前版本 Workbook 对文件类型进行了判断,达到兼容,不会引起以下包报错。
Package should contain a content type part [M1.13]]
- File 与MultipartFile转化问题
https://www.liangzl.com/get-article-detail-30596.html
M 转 F
File file = new File(path);
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
F 转 M
File file = new File("src/test/resources/input.txt");
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
- 取数值类型数据格式问题
https://blog.csdn.net/ghsau/article/details/10163043
public void importGirdInfo(MultipartFile file) {//检测是否为空
if (file == null || file.getSize() == 0) {
System.out.println("请重新选择导入文件");
}
//检测格式
String fn = file.getOriginalFilename();
if (!(fn.endsWith("xls") || fn.endsWith("xlsx"))) {
System.out.println("文件类型错误,请重新选择导入文件");
}InputStream input = null;
StringBuffer failedRows = new StringBuffer();
try {
input = file.getInputStream();
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(input);
} catch (InvalidFormatException e) {
e.printStackTrace();
}
Sheet sheet = workbook.getSheetAt(0);
//XSSFWorkbook 可读07版本ExcelHSSFWork 读03版本Workbook 对文件类型进行了判断
//XSSFWorkbook wb = new XSSFWorkbook(input);
//XSSFSheet sheet = wb.getSheetAt(0);
//分析数据System.out.println(sheet.getLastRowNum());
System.out.println(sheet.getFirstRowNum());
for (int r = 0;
r <= sheet.getLastRowNum();
r++) {Row row = sheet.getRow(r);
if (row == null || row.getCell(0) == null) {
continue;
}
try {
//DistrictDTO dto = new DistrictDTO();
String gridCode = "";
long gridCode1 = 0;
if (row.getCell(0).getCellType() == Cell.CELL_TYPE_NUMERIC) {
gridCode1 = Math.round(row.getCell(0).getNumericCellValue());
} else if (row.getCell(0).getCellType() == Cell.CELL_TYPE_STRING) {
gridCode = row.getCell(0).getStringCellValue();
}
System.out.println("打印");
System.out.println(gridCode);
System.out.println(gridCode1);
//分析数据的操作
//...} catch (Exception e) {}
}} catch (IOException e) {
System.out.println("解析异常,请重新导入");
} finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
System.out.println("关闭错误!");
}
}
}
}
测试类
@Test
public void test3() throws Exception {File file = new File("/Users/peach/Desktop/text.xls");
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
IOUtils
.toByteArray(input));
excelGridInfo.importGirdInfo(multipartFile);
}
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)