1. excel上传
import jar:
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文件上传
跳过第一条的数据
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;
}
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);
}