1

尝试从 ssrs 下载 excel 文件并使用 ClosedXML 进行修改,出现异常。以下是我的代码,在exlWorkBook = new XLWorkbook(FilePath);

public ActionResult ExportToExcel(string VesselObjectId, string EnquiryID)
{
    XLWorkbook exlWorkBook = null;

    string FileName = "QuoteCompare_" + VesselObjectId + "_" + String.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + ".xlsx";
    string DirPath = Server.MapPath("~/App_Files/Seachef");
    string FilePath = Path.Combine(DirPath, FileName);
    string strTargetURL = GlobalParameters.SSRSBaseUrl + "/SeaChefReports/rptQuoteCompare&User_ID=" + CurrentUserData.UserId + "&P_ENQUIRY_HD_ID=" + EnquiryID + "&rc:LinkTarget=_blank&rc:Parameters=False&rs:Format=EXCELOPENXML&rs:ClearSession=true";
    try
    {
        ReportViewer objReportViewer = new ReportViewer();
        objReportViewer.ProcessingMode = ProcessingMode.Remote;
        objReportViewer.ServerReport.ReportServerCredentials = new CustomReportCredentials(ConfigurationManager.AppSettings["ReportServerId"], ConfigurationManager.AppSettings["ReportServerPwd"], "BSM");
        objReportViewer.ServerReport.Refresh();
        SSRSWebClient client = new SSRSWebClient();
        client.DownloadFileBinary(FilePath, strTargetURL);

        if (new FileInfo(FilePath).Exists)
        {
            exlWorkBook = new XLWorkbook(FilePath);

            var ws = exlWorkBook.Worksheet("rptQuoteCompare");

            List<IXLCell> vendorsectionTotalCells = new List<IXLCell>();
            List<IXLCell> vendorCategoryTotalCells = new List<IXLCell>();

            ws.SheetView.FreezeRows(5);
            ws.SheetView.FreezeColumns(7);
            ws.Range(ws.FirstCell(), ws.LastCellUsed()).Style.Protection.Locked = true;
            ws.Cell("A1").Value = EnquiryID;

            //Doing Some more modifications

            exlWorkBook.Style.Protection.Locked = true;
            var protection = ws.Protect(BSMUiHelpers.SeachefSessionVariables.SeachefSessionData.ExcelPassword);
            protection.SelectLockedCells = true;
            protection.SelectUnlockedCells = true;

            Stream objStream = new MemoryStream();
            exlWorkBook.SaveAs(objStream);
            objStream.Position = 0;

            BinaryReader rdr = new BinaryReader(objStream);
            byte[] fileData = rdr.ReadBytes((int)objStream.Length);
            rdr.Close();
            objStream.Close();

            return File(fileData, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", FileName);
        }
        else
        {
            return Json("Export failed!");
        }

    }
    catch (Exception ex)
    {
        bool rethrow = UserInterfaceExceptionHandler.HandleExcetion(ref ex, CurrentUserData.UserId.ToString(), "");
        return Json("Export failed!" + ex.Message);
    }
    finally
    {
        if (exlWorkBook != null)
            exlWorkBook.Dispose();
        if (new FileInfo(FilePath).Exists)
            System.IO.File.Delete(FilePath);
    }
}

下面是堆栈跟踪

at ClosedXML.Excel.XLWorkbook.LoadCells(SharedStringItem[] sharedStrings, Stylesheet s, NumberingFormats numberingFormats, Fills fills, Borders borders, Fonts fonts, Dictionary`2 sharedFormulasR1C1, XLWorksheet ws, Dictionary`2 styleList, Cell cell, Int32 rowIndex)
   at ClosedXML.Excel.XLWorkbook.LoadRows(Stylesheet s, NumberingFormats numberingFormats, Fills fills, Borders borders, Fonts fonts, XLWorksheet ws, SharedStringItem[] sharedStrings, Dictionary`2 sharedFormulasR1C1, Dictionary`2 styleList, Row row)
   at ClosedXML.Excel.XLWorkbook.LoadSpreadsheetDocument(SpreadsheetDocument dSpreadsheet)
   at ClosedXML.Excel.XLWorkbook.LoadSheets(String fileName)
   at ClosedXML.Excel.XLWorkbook.Load(String file)
   at ClosedXML.Excel.XLWorkbook..ctor(String file, XLEventTracking eventTracking)
   at ClosedXML.Excel.XLWorkbook..ctor(String file)
   at BSM.BSMPALv2.Web.Areas.Seachef.Controllers.QuotationComparisionController.ExportToExcel(String VesselObjectId, String EnquiryID) in d:\XXX\TestSource\XXXXXXX.XXXXXX\XXXXXXX.XXXX\XXXXXXXXXX\Areas\ModuleName\Controllers\QuotationComparisionController.cs:line 293
4

0 回答 0