//判断是否是下载模板
private String template;
public void setTemplate(String template) {
this.template = template;
}
public String download() throws Exception {
// 创建一个要到出表的表头
String[] heads = { "编号", "用户名", "邮箱", "年龄", "部门" };
//查询出要导出的数据,先展示出全部数据
baseQuery.setPageSize(Integer.MAX_VALUE);
this.pageList = employeeService.findByQuery(baseQuery);
List<Employee> employees = pageList.getData();
List<String[]> list = new ArrayList<>();
if (template ==null) {
//将 数据放入到list 中
for (Employee employee : employees) {
String[] content = new String[heads.length];
content[0] = employee.getId().toString();
content[1] = employee.getUsername();
content[2] = employee.getEmail()== null? "" : employee.getEmail();
content[3] = employee.getAge() == null? "":employee.getAge().toString();
content[4] = employee.getDept() == null? "" :employee.getDept().getName() ;
//放到list 中
list.add(content);
}
}
//调用BaseService 中的方法
//把数据放到流中,,配置Struts.xml
this.inputStream = employeeService.download(heads, list);
return "download";
}
@Override
public InputStream download(String[] heads, List<String[]> list) throws Exception {
// 创建一个内存对象
SXSSFWorkbook wb = new SXSSFWorkbook();
//创建一个Excel 表
Sheet sheet = wb.createSheet("sheet1");
//创建第0行
Row row = sheet.createRow(0);
// 处理到处表头 第0行
for (int i = 0; i < heads.length; i++) {
Cell cell = row.createCell(i);
//添加内容
cell.setCellValue(heads[i]);
}
for (int j = 0; j < list.size(); j++) {// 行
//创建 行
Row row2 = sheet.createRow(j+1);
for (int i = 0; i < heads.length; i++) {// 列
//创建格子
Cell cell = row2.createCell(i);
//赋值
cell.setCellValue(list.get(j)[i]);
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
wb.write(out);
out.close();
wb.dispose();
return new ByteArrayInputStream(out.toByteArray());
}
导入
public String execute() throws Exception {
if (upload != null) {
List<String[]> list = employeeService.importExcel(upload);
//将list 中的值转为Employee对象
for (String[] strings : list) {
//创建一个Employee 对象 String[] heads = { "编号", "用户名", "邮箱", "年龄", "部门" };
Employee employee = new Employee();
employee.setUsername(strings[1]+UUID.randomUUID().toString().substring(0, 4));
employee.setEmail(strings[2]);
if (StringUtils.isNotBlank(strings[3])) {
employee.setAge(Integer.valueOf(strings[3]));
}
if (StringUtils.isNotBlank(strings[4])) {
//这里要查出部门,所以要导入DepartmentService,并且提供一个 根据名字找部门的方法
Department department = departmentService.findByName(strings[4]);
employee.setDept(department);
}
//要持久化 保存到数据库
employeeService.save(employee);
}
putContextMap("ImportMsg", "成功导入"+list.size()+"条数据!");
}
return SUCCESS;
}
/**
* 导入Excel
* */
@Override
public List<String[]> importExcel(File file) throws Exception {
// 创建一个文件输出流,读取要导入的文件
FileInputStream inputStream = new FileInputStream(file);
//创建一个读取对象,在输入流中读取数据
Workbook workbook = new XSSFWorkbook(inputStream);
//获取表对象
Sheet sheet = workbook.getSheetAt(0); //----------------------不理解
//解析表对象 到list
//定义一个List
List<String[]> list = new ArrayList<>();
/**
* sheet.getLastRowNum(); 得到的是数据的总行数
* row.getLastCellNum();获得 一行的多少列
* */
for (int i = 0; i < sheet.getLastRowNum(); i++) {
// 获取行对象 ,注意 不要表头 从1 开始
Row row = sheet.getRow(i+1);
String[] strings = new String[row.getLastCellNum()];
for (int j = 0; j < strings.length; j++) {
//获得 格子
Cell cell = row.getCell(j);
strings[j] = cell.getStringCellValue();
}
list.add(strings);
}
return list;
}