vue實現Excel檔案的上傳與下載

wnxyz8023發表於2019-06-28

一.前言專案中使用到比較多的關於Excel的前端上傳與下載,整理出來,以便後續使用或分析他人。

1.前端vue:模板下載與匯入Excel

匯入Excel封裝了子元件,點選匯入按鈕可呼叫子元件,開啟檔案上傳的對話方塊,上傳成功後返回結果

      <el-col style="padding: 10px 0 20px;">
              <el-button
                class="pull-right"
                icon="el-icon-upload"
                type="primary"
                size="mini"
                @click="importFile()"
              >批量匯入</el-button>
              <el-button
                class="pull-right right-10"
                icon="el-icon-download"
                type="primary"
                size="mini"
                @click="downloadFile('檔案模板')"
              >模板下載</el-button>
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-plus"
                class="pull-right"
                @click="addRow"
              >新增</el-button>
              <div class="pull-right">
                <el-input
                  placeholder="請輸入編碼,名稱"
                  prefix-icon="el-icon-search"
                  v-model="FinQueryParams.archiveFilter"
                  size="mini"
                ></el-input>
              </div>
            </el-col>

  

 <!-- 批量匯入Dialog開始 -->
    <uploadTemp
      :apiURL="fileUploadUrl"
      ref="refFileUpload"
      :Refresh="Refresh"
      :OtherParams="{brandId: QueryParams.BrandID}"
    ></uploadTemp>
    <!-- 批量匯入Dialog結束 -->
  importFile() {
      this.$refs.refFileUpload.open();
    } 

向後臺提交檔案的方法

 submitFile() {
      const _this = this;
      if (!_this.files.name) {
        _this.$message.warning("請選擇要上傳的檔案!");
        return false;
      }
      let fileFormData = new FormData();
      //filename是鍵,file是值,就是要傳的檔案
      fileFormData.append("file", _this.files, _this.files.name);
      if(_this.OtherParams){
        const keys=Object.keys(_this.OtherParams);
        keys.forEach(e=>{
          fileFormData.append(e, _this.OtherParams[e]);
        })
      }
      let requestConfig = {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      };
      AjaxHelper.post(_this.apiURL, fileFormData, requestConfig)
        .then(res => {
          console.log(res);
          if (res.success) {
            const result = res.result;
            if (result.errorCount == 0 && result.successCount > 0) {
              _this.$message({
                message: `匯入成功,成功${result.successCount}條`,
                type: "success"
              });
              _this.closeFileUpload();
              _this.Refresh();
            } else if (result.errorCount > 0 && result.successCount >= 0) {
              _this.Refresh();
              _this.tableData = result.uploadErrors;
              _this.successCount = result.successCount;
              _this.innerVisible = true;
            } else if (result.errorCount == 0 && result.successCount == 0) {
              _this.$message({
                message: `上傳檔案中資料為空`,
                type: "error"
              });
            }
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
View Code

 

這是上傳檔案的呼叫方法。

2.模板下載

關於模板下載,之前沒有考慮到IE10瀏覽器的相容問題,導致在IE10下檔案沒法下載,後來百度後找到了解決辦法。

downloadFile(name) {
      let requestConfig = {
        headers: {
          "Content-Type": "application/json;application/octet-stream"
        }
      };
      AjaxHelper.post(this.downLoadUrl, requestConfig, {
        responseType: "blob"
      }).then(res => {
        // 處理返回的檔案流
        const content = res.data;
        const blob = new Blob([content]);
        var date =
          new Date().getFullYear() +
          "" +
          (new Date().getMonth() + 1) +
          "" +
          new Date().getDate();
        const fileName = date + name + ".xlsx";
        if ("download" in document.createElement("a")) {
          // 非IE下載
          const elink = document.createElement("a");
          elink.download = fileName;
          elink.style.display = "none";
          elink.href = URL.createObjectURL(blob);
          document.body.appendChild(elink);
          elink.click();
          URL.revokeObjectURL(elink.href); // 釋放URL 物件
          document.body.removeChild(elink);
        } else {
          // IE10+下載
          navigator.msSaveBlob(blob, fileName);
        }
      });
    },

  前端的處理就結束了。

3.後端對於檔案上傳和下載的處理

檔案上傳

public UploadResult UploadFiles(IFormFile file, Guid brandId)
        {
            try
            {
                UploadResult uploadResult = new UploadResult();
                if (file == null)
                {
                    throw new UserFriendlyException(501, "上傳的檔案為空,請重新上傳");
                }
                string filename = Path.GetFileName(file.FileName);
                string fileEx = Path.GetExtension(filename);//獲取上傳檔案的副檔名
                string NoFileName = Path.GetFileNameWithoutExtension(filename);//獲取無副檔名的檔名
                string FileType = ".xls,.xlsx";//定義上傳檔案的型別字串
                if (!FileType.Contains(fileEx))
                {
                    throw new UserFriendlyException(501, "無效的檔案型別,只支援.xls和.xlsx檔案");
                }
                //源資料
                MemoryStream msSource = new MemoryStream();
                file.CopyTo(msSource);
                msSource.Seek(0, SeekOrigin.Begin);
                DataTable sourceExcel = ReadStreamToDataTable(msSource, "", true);

                //模板資料
                string dataDir = _hosting.WebRootPath;//獲得當前伺服器程式的執行目錄  
                dataDir = Path.Combine(dataDir, "ExcelTemplate");
                var path = dataDir + "//檔案模版.xlsx";
                MemoryStream msModel = new MemoryStream();
                FileStream stream = new FileStream(path, FileMode.Open);
                stream.CopyTo(msModel);
                msModel.Seek(0, SeekOrigin.Begin);
                DataTable templateExcel = ReadStreamToDataTable(stream, "", true);
                //驗證是否同模板相同 
                string columnName = templateExcel.Columns[0].ColumnName;
                if (columnName != sourceExcel.Columns[0].ColumnName)
                {
                    throw new UserFriendlyException(501, "上傳的模板檔案不正確");
                }
                int sucessCount = 0;
                int errorCount = 0;
                // 處理後臺邏輯 執行 插入操作

                uploadResult.SuccessCount = sucessCount;
                uploadResult.ErrorCount = errorCount;
                uploadResult.uploadErrors = errorList;
                return uploadResult;
            }
            catch (Exception ex)
            {
                throw new UserFriendlyException(501, "上傳的模板檔案不正確");
            }
        }
View Code

將檔案流轉化為Datable

    public static DataTable ReadStreamToDataTable(Stream fileStream, string sheetName = null, bool isFirstRowColumn = true)
        {
            //定義要返回的datatable物件
            DataTable data = new DataTable();
            //excel工作表
            ISheet sheet = null;
            //資料開始行(排除標題行)
            int startRow = 0;
            try
            {
                //根據檔案流建立excel資料結構,NPOI的工廠類WorkbookFactory會自動識別excel版本,建立出不同的excel資料結構
                IWorkbook workbook = WorkbookFactory.Create(fileStream);
                //如果有指定工作表名稱
                if (!string.IsNullOrEmpty(sheetName))
                {
                    sheet = workbook.GetSheet(sheetName);
                    //如果沒有找到指定的sheetName對應的sheet,則嘗試獲取第一個sheet
                    if (sheet == null)
                    {
                        sheet = workbook.GetSheetAt(0);
                    }
                }
                else
                {
                    //如果沒有指定的sheetName,則嘗試獲取第一個sheet
                    sheet = workbook.GetSheetAt(0);
                }
                if (sheet != null)
                {
                    IRow firstRow = sheet.GetRow(0);
                    //一行最後一個cell的編號 即總的列數
                    int cellCount = firstRow.LastCellNum;
                    //如果第一行是標題列名
                    if (isFirstRowColumn)
                    {
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                            ICell cell = firstRow.GetCell(i);
                            if (cell != null)
                            {
                                string cellValue = cell.StringCellValue;
                                if (cellValue != null)
                                {
                                    DataColumn column = new DataColumn(cellValue);
                                    data.Columns.Add(column);
                                }
                            }
                        }
                        startRow = sheet.FirstRowNum + 1;
                    }
                    else
                    {
                        startRow = sheet.FirstRowNum;
                    }
                    //最後一列的標號
                    int rowCount = sheet.LastRowNum;
                    for (int i = startRow; i <= rowCount; ++i)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null || row.FirstCellNum < 0) continue; //沒有資料的行預設是null       

                        DataRow dataRow = data.NewRow();
                        for (int j = row.FirstCellNum; j < cellCount; ++j)
                        {
                            //同理,沒有資料的單元格都預設是null
                            ICell cell = row.GetCell(j);
                            if (cell != null)
                            {
                                if (cell.CellType == CellType.Numeric)
                                {
                                    //判斷是否日期型別
                                    if (DateUtil.IsCellDateFormatted(cell))
                                    {
                                        dataRow[j] = row.GetCell(j).DateCellValue;
                                    }
                                    else
                                    {
                                        dataRow[j] = row.GetCell(j).ToString().Trim();
                                    }
                                }
                                else
                                {
                                    dataRow[j] = row.GetCell(j).ToString().Trim();
                                }
                            }
                        }
                        data.Rows.Add(dataRow);
                    }
                }
                return data;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
View Code

檔案下載比較簡單

  public async Task<FileStreamResult> DownloadFiles()
        {
            string dataDir = _hosting.WebRootPath;//獲得當前伺服器程式的執行目錄  
            dataDir = Path.Combine(dataDir, "ExcelTemplate");
            var path = dataDir + "//檔案模版.xlsx";
            var memoryStream = new MemoryStream();
            using (var stream = new FileStream(path, FileMode.Open))
            {
                await stream.CopyToAsync(memoryStream);
            }
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/octet-stream");//檔案流方式,指定檔案流對應的ContenType。
        }

檔案上傳結果通知類

 public class UploadResult
    {
        public int RepeatCount { get; set; }
        public int SuccessCount { get; set; }
        public int FileRepeatCount { get; set; }
        public int ErrorCount { get; set; }

        public List<UploadErrorDto> uploadErrors { get; set; }
    }
    public class UploadErrorDto
    {
        public string RowIndex { get; set; }
        public string ErrorCol { get; set; }
        public string ErrorData { get; set; }
    }

  

通過以上處理後,我們就可以在前端實現檔案的上傳了,若上傳失敗則會返回失敗結果

 

 

 以上就是整個前後端關於檔案上傳與下載的實現,想通過日常記錄這種方式,來幫助自己更好的掌握基礎,穩固自己的技能

相關文章