1. 程式人生 > >excel導入導出

excel導入導出

email exce array lan throws nbsp port style use

//判斷是否是下載模板
    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; }

excel導入導出