springboot中Excel文件導入導出

Java學習大綱(持續更新):https://blog.csdn.net/weixin_39778570/article/details/94667501
更多IT學習資源:https://blog.csdn.net/weixin_39778570/article/details/100052454

Excel文件導入

從前端傳遞excel文件到後端,通過ajax
這裏使用的是lay-ui的控件

upload.render({
            elem: '#test' //綁定元素
            ,accept: 'file'
            ,url: '/xxx/xxx/upload' //上傳接口
            ,before: function(obj) {
                layer.msg('文件上傳中...', {
                    icon: 16,
                    shade: 0.01,
                    time: 0
                })
            },
            done: function(res) {
                layer.close(layer.msg(res.msg));//關閉上傳提示窗口
            }
        });

後端Controller獲取數據並處理

@RequestMapping("upload")
	@ResponseBody
	public BaseResponse doUpload(HttpServletRequest request) throws IOException {
		MultipartHttpServletRequest mreq = null;
		if(request instanceof  MultipartHttpServletRequest){
			mreq = (MultipartHttpServletRequest) request;
		}else {
			return BaseResponse.fail ();
		}
		try{
			boolean flag = xxx.analysisFile(mreq);
			if(!flag){
				return BaseResponse.fail ();
			}
			return BaseResponse.success();
		}catch (Exception e){
			return BaseResponse.fail ();
		}
	}

Server層xxx.analysisFile(mreq)

public boolean analysisFile(MultipartHttpServletRequest mreq){
		List<Map> maps = null;
		try {
			maps = ExcelUtils.analysisFile (mreq);
		} catch (Exception e) {
			return false;
		}
		if(maps==null){
			return false;
		}else{
			// 處理信息
			return true;
		}
	}

解析Excel文件請求的工具類(可以直接複用)

import org.apache.poi.ss.usermodel.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * Excel處理工具
 */
public class ExcelUtils {

    /**
     * 處理HTTP請求裏是file文件
     * @param mreq
     * @return 返回List<Map>每個Map存放一行
     */
    public static List<Map> analysisFile(MultipartHttpServletRequest mreq)throws Exception{
        InputStream inputStream= null;
        try {
            inputStream = mreq.getFile ("file").getInputStream ();
        } catch (IOException e) {
            throw e;
        }
        String fileName = mreq.getFile ("file").getOriginalFilename ();
        Workbook workbook = null;
        try {
            //判斷什麼類型文件
            if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
                workbook = WorkbookFactory.create(inputStream);
            }
        } catch (Exception e) {
            throw e;
        }
        if (workbook == null) {
            return null;
        } else {
            //獲取所有的工作表的的數量
            int numOfSheet = workbook.getNumberOfSheets ();
            System.out.println (numOfSheet + "--->numOfSheet");
            List<Map> mapList = new ArrayList<> ();
            //遍歷表
            for (int i = 0; i < numOfSheet; i++) {
                //獲取一個sheet也就是一個工作本。
                Sheet sheet = workbook.getSheetAt (i);
                if (sheet == null) {
                    continue;
                }
                //獲取一個sheet有多少Row
                int lastRowNum = sheet.getLastRowNum ();
                if (lastRowNum == 0) {
                    continue;
                }
                Row row;
                // 行頭
                List<String> headName = new ArrayList<>();
                if(lastRowNum>0){
                    row = sheet.getRow (0);
                    // 獲取一個Row有多少Cell
                    short lastCellNum = row.getLastCellNum ();
                    for (int k = 0; k <= lastCellNum; k++) {
                        if (row.getCell (k) == null) {
                            continue;
                        }
                        String res = getCellVal(row.getCell (k));
                        headName.add (res);
                    }
                }

                // 每一行的內容
                for (int j = 1; j <= lastRowNum; j++) {
                    row = sheet.getRow (j);
                    if (row == null) {
                        continue;
                    }
                    //獲取一個Row有多少Cell
                    short lastCellNum = row.getLastCellNum ();
                    Map<String,String> rowMap = new HashMap ();
                    for (int k = 0; k <= lastCellNum; k++) {
                        if (row.getCell (k) == null) {
                            continue;
                        }
                        String res = getCellVal(row.getCell (k));
                        rowMap.put (headName.get (k), res);
                    }
                    mapList.add (rowMap);
                }
            }
            return mapList;
        }
    }

    private static String getCellVal(Cell cell) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); //日期格式yyyy-mm-dd
        DecimalFormat df = new DecimalFormat("0");
        String val;
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    val = fmt.format(cell.getDateCellValue()); //日期型
                } else {
                    val = df.format(cell.getNumericCellValue()); //數字型
                }
                break;
            case Cell.CELL_TYPE_STRING: //文本類型
                val = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_BOOLEAN: //布爾型
                val = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_BLANK: //空白
                val = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_ERROR: //錯誤
                val = "錯誤";
                break;
            case Cell.CELL_TYPE_FORMULA: //公式
                try {
                    val = String.valueOf(cell.getStringCellValue());
                } catch (IllegalStateException e) {
                    val = String.valueOf(cell.getNumericCellValue());
                }
                break;
            default:
                val = cell.getRichStringCellValue() == null ? null : cell.getRichStringCellValue().toString();
        }
        return val;
    }
}

Excel 文件導出

由於ajax 是無法直接導出excel的,因爲ajax返回值只能是字符流,而導出excel是後臺往瀏覽器中寫入二進制的字節流
流程是這樣的,前端發送一個ajax請求到後端,後端緩存請求數據,並保存請求,返回給瀏覽器一個UUID,瀏覽器通過UUID再發起請求(可以通過window.location.href的方式),服務器把excel文件寫入response中顯示到瀏覽器

前端請求

form.on('submit(export)', function(data){
            var field = data.field;
            $.ajax({
                url:'<@spring.url "/xxx/ttt/export"/>',
                dataType : "json",
                data : JSON.stringify(field),
                contentType : "application/json; charset=utf-8",
                type : "POST",
                async: true,
                success : function(data){
                    var key =data.msg
                    window.location.href = "/xxx/ttt/doExport?key="+key
                }
            })
        });

後端處理請求,先緩存,再查詢(爲了簡單起見這裏就不查詢了),再清楚緩存

/**
     * 由於ajax無法直接導出excel,所以第一次把請求生成的ExcelParam緩存起來,然後前端再次window.open(url);
     */
    public static Map<String, Xxx> excelParamCache = new ConcurrentHashMap<> ();

    //第一步緩存參數
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/export")
    @ResponseBody
    public BaseResponse export(@RequestBody Xxx requestVo) {
        String key = UUID.randomUUID().toString();
        excelParamCache.put(key,requestVo);
        return new BaseResponse(0,key);
    }

    @RequestMapping(value = "doExport")
    public BaseResponse doExport(String key, HttpServletRequest request, HttpServletResponse response) throws IOException {
        if(key==null){
            return BaseResponse.fail ();
        }
        try {
            if(!excelParamCache.containsKey (key)){
                return BaseResponse.fail ();
            }
            HSSFWorkbook workbook = new HSSFWorkbook ();
            HSSFSheet sheet = workbook.createSheet ("信息表");

            // 設置要導出的文件的名字
            String fileName = "報表-" + new Date () + ".xls";

            // 新增數據行,並且設置單元格數據
            int rowNum = 1;

            // headers表示excel表中第一行的表頭 在excel表中添加表頭
            String[] headers = {"id", "uid", "地址", "城市"};
            HSSFRow row = sheet.createRow (0);
            for (int i = 0; i < headers.length; i++) {
                HSSFCell cell = row.createCell (i);
                HSSFRichTextString text = new HSSFRichTextString (headers[i]);
                cell.setCellValue (text);
            }

            //在表中存放查詢到的數據放入對應的列
            for (int i = 0; i < 4; i++) {
                HSSFRow row1 = sheet.createRow (rowNum);
                row1.createCell (0).setCellValue (1);
                row1.createCell (1).setCellValue (2);
                row1.createCell (2).setCellValue (3);
                row1.createCell (3).setCellValue ("hhh");
                rowNum++;

            }

            response.setContentType ("application/octet-stream");
            response.setHeader ("Content-disposition", "attachment;filename=" + fileName);
            response.flushBuffer ();
            workbook.write (response.getOutputStream ());
        }catch (Exception e){
            excelParamCache.remove(key);
        }finally {
            excelParamCache.remove(key);
        }
        return BaseResponse.success ();
    }

依賴

<!-- 文件上傳 -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.14</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.14</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>3.14</version>
    </dependency>

執行結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章