我是靠谱客的博主 伶俐柚子,这篇文章主要介绍excel 、 csv 文件上传,现在分享给大家,希望可以做个参考。

1. excel上传

import jar: 


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(inp); for(int i=0, sheetLength=workbook.getNumberOfSheets(); i<sheetLength; i++) { Sheet sheet = workbook.getSheetAt(i); if(sheet == null) { continue; } int userCount = 0; for(int j=1, rowLength=sheet.getLastRowNum(); j<=rowLength; j++) { Row row = sheet.getRow(j); if(row != null && row.getRowNum() > 0) { user = new User(); try { user.setUserName(getValue(row.getCell(0))); user.setEmail(getValue(row.getCell(1))); user.setCellphone(getValue(row.getCell(2))); user.setTelephone(getValue(row.getCell(3))); } catch(Exception e) { appendImportExcelLog(String.format("Exception:import user.userName=[%s];"+e, user.getUserName()); continue; } } } catch(Exception e) { log.error("Exception: import user's Exception", e); }


2. csv文件上传

跳过第一条的数据


复制代码
1
2
3
4
5
6
7
public SimpleReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes, boolean ignoreLeadingWhiteSpace) { this.br = new BufferedReader(reader); this.parser = new CSVParser(separator, quotechar, escape, strictQuotes, ignoreLeadingWhiteSpace); this.skipLines = line; }


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
try { SimpleReader reader = new SimpleReader(new InputStreamReader(inp, Charset.forName("gbk")), ',', ''', 1); int userCount = 0; for(String[] line : reader.readAll()) { user = new UserDAO(); try { user.setUserName(line[0].trim()); user.setEmail(line[1].trim()); user.setCellphone(line[2].trim()); user.setTelephone(line[3].trim()); } catch(Exception e) { appendImportExcelLog(String.format("Exception:import user.userName=[%s];"+e, user.getUserName())); continue; } } catch(Exception e) { log.error("Exception: import user's Exception", e); }


最后

以上就是伶俐柚子最近收集整理的关于excel 、 csv 文件上传的全部内容,更多相关excel内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部