c# – 大数据损坏的excel文件

c# – 大数据损坏的excel文件,第1张

概述嗨,我正在使用EPPlus创建带有一些数据转储的xslx文件.它可以在本地计算机上以及在Web服务器上完美地处理少量数据. 但我有一个案例,我在数据集中有40,000行.它再次在本地机器上完美运行. 但在服务器上它正在创建文件,当我试图打开该文件时,它显示文件已损坏的错误.我尝试用记事本编辑文件,发现它里面有HTML内容. 我正在使用此代码: public static void CreateEx 嗨,我正在使用EPPlus创建带有一些数据转储的xslx文件.它可以在本地计算机上以及在Web服务器上完美地处理少量数据.

但我有一个案例,我在数据集中有40,000行.它再次在本地机器上完美运行.

但在服务器上它正在创建文件,当我试图打开该文件时,它显示文件已损坏的错误.我尝试用记事本编辑文件,发现它里面有HTML内容.

我正在使用此代码:

public static voID CreateExcel(string file_name,DataSet ds){    //  rowsPerSheet  = 50000;     string msg = "";    string Type = "";    using (ExcelPackage pck = new ExcelPackage()) {        //Create the worksheet        ExcelWorksheet ws = default(ExcelWorksheet);        int clCnt = 1;        foreach (Datatable tbl in ds.tables) {            ws = pck.Workbook.Worksheets.Add(tbl.tablename);            //Load the datatable into the sheet,starting from cell A1. Print the column names on row 1            ws.Cells("A1").LoadFromDatatable(tbl,true);            if (tbl.Rows.Count != 0) {                clCnt = 1;                foreach (DataColumn col in tbl.Columns) {                    ws.Column(clCnt).autoFit();                    // format all dates in german format (adjust accordingly)                    if (col.DataType.Equals(typeof(System.DateTime))) {                        dynamic colNumber = col.Ordinal + 1;                        ExcelRange range = ws.Cells(2,colNumber,tbl.Rows.Count + 1,colNumber);                        range.Style.Numberformat.Format = "MM/dd/yyyy";                    }                    if (col.DataType.Equals(typeof(System.Decimal)) || col.DataType.Equals(typeof(System.Double))) {                        dynamic colNumber = col.Ordinal + 1;                        ExcelRange range = ws.Cells(2,colNumber);                        range.Style.Numberformat.Format = "0.00";                    }                    clCnt += 1;                }            }        }        file_name = file_name.Replace(" ","_") + ".xlsx";        httpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";        httpContext.Current.response.addheader("content-disposition","attachment;  filename=" + file_name + "");        httpContext.Current.Response.BinaryWrite(pck.GetAsByteArray());        httpContext.Current.Response.End();    }}

父功能:

private voID GenerateReport(){    string msg = "";    string output = "where";    int count = 0;    string Jsscript = "";    string _FID = "";    if ((ddlPortfolio.SelectedValue <= 0 & grvforecast.Rows.Count <= 0)) {        Jsscript = "<script>alert('Please select potfolio')</script>";        this.Page.ClIEntScript.RegisterStartupScript(Page.GetType,"Jsclose",Jsscript);    } else if ((grvforecast.Rows.Count <= 0)) {        Jsscript = "<script>alert('Please add some entry in grID')</script>";        this.Page.ClIEntScript.RegisterStartupScript(Page.GetType,Jsscript);    } else {        if (btnValue.Value == "Cash/GAAP Report") {            _sqlStr = "[USP_CashGaapReport] '";            foreach (GrIDVIEwRow row in grvforecast.Rows) {                if ((count < grvforecast.Rows.Count - 1)) {                    _sqlStr += "" + grvforecast.Rows(count).Cells(1).Text + ",";                } else {                    _sqlStr += "" + grvforecast.Rows(count).Cells(1).Text + "";                }                count = count + 1;            }            _sqlStr += "'";        } else {            if ((btnValue.Value == "Forecast Attribute Report")) {                _sqlStr = "SELECT f.AttributeSetID as AttributeSetID,Attribute_Set.Tabname as AttributeSetname FROM Forecast_Account as f INNER JOIN    Attribute_Set ON f.AttributeSetID = Attribute_Set.AttributeSetID where ";            } else if ((btnValue.Value == "Forecast Data Report")) {                _sqlStr = "SELECT p.LegalEntityname AS Portfolio,f.name,c.Accountname,a.RepeatNumber,d.CashGAAP,d.TheDate,SUM(d.Amount) AS Amount,d.LastUpdated,d.UpdatedBy ";                _sqlStr += "FROM dbo.Portfolio AS p INNER JOIN dbo.Forecast AS f ON p.PortfolioID = f.PortfolioID INNER JOIN dbo.Forecast_Account AS a ON f.ForecastID = a.ForecastID ";                _sqlStr += "INNER JOIN dbo.Forecast_Data AS d ON a.ForecastAccountID = d.ForecastAccountID INNER JOIN dbo.CoA AS c ON c.AccountNumber = a.AccountNumber where ";            } else {                //    _sqlStr = "SELECT Portfolio,name,Accountname,CashGAAP,OriginalDate,sum(Amount) as Amount,AccountNumber,AttributeSetname,TheDate,Year"                //    _sqlStr &= " FROM (SELECT  Portfolio.LegalEntityname AS Portfolio,CoA.Accountname,Forecast_Data.CashGAAP,CONVERT(date,Forecast_Data.TheDate) AS OriginalDate,"                //    _sqlStr &= " SUM(Forecast_Data.Amount) AS Amount,CoA.AccountNumber,Attribute_Set.AttributeSetname,'' + CONVERT(varchar,YEAR(Forecast_Data.TheDate))"                //    _sqlStr &= " + '-' + CONVERT(varchar,MONTH(Forecast_Data.TheDate)) + '-01' AS TheDate,YEAR(Forecast_Data.TheDate) AS Year,Forecast_Attribute.Value"                //    _sqlStr &= " FROM   Portfolio INNER JOIN  Forecast AS f ON Portfolio.PortfolioID = f.PortfolioID INNER JOIN Forecast_Account ON f.ForecastID = Forecast_Account.ForecastID INNER JOIN Forecast_Data ON"                //    _sqlStr &= " Forecast_Account.ForecastAccountID = Forecast_Data.ForecastAccountID INNER JOIN CoA ON CoA.AccountNumber = Forecast_Account.AccountNumber"                //    _sqlStr &= " INNER JOIN Attribute_Set ON CoA.AttributeSetID = Attribute_Set.AttributeSetID INNER JOIN Forecast_Attribute ON Forecast_Account.ForecastAccountID = Forecast_Attribute.ForecastAccountID WHERE"                _sqlStr = "SELECT Portfolio,d.AccountNumber as AccountNumber,Year";                _sqlStr += " FROM (SELECT  Portfolio.LegalEntityname AS Portfolio,";                _sqlStr += " SUM(Forecast_Data.Amount) AS Amount,YEAR(Forecast_Data.TheDate))";                _sqlStr += " + '-' + CONVERT(varchar,Forecast_Attribute.Value";                _sqlStr += " FROM   Portfolio INNER JOIN  Forecast AS f ON Portfolio.PortfolioID = f.PortfolioID INNER JOIN Forecast_Account ON f.ForecastID = Forecast_Account.ForecastID INNER JOIN Forecast_Data ON";                _sqlStr += " Forecast_Account.ForecastAccountID = Forecast_Data.ForecastAccountID INNER JOIN CoA ON CoA.AccountNumber = Forecast_Account.AccountNumber";                _sqlStr += " INNER JOIN Attribute_Set ON CoA.AttributeSetID = Attribute_Set.AttributeSetID INNER JOIN Forecast_Attribute ON Forecast_Account.ForecastAccountID = Forecast_Attribute.ForecastAccountID WHERE";            }            foreach (GrIDVIEwRow row in grvforecast.Rows) {                if ((count < grvforecast.Rows.Count - 1)) {                    _sqlStr += " f.ForecastID=" + grvforecast.Rows(count).Cells(1).Text + " or";                    _FID += " a.ForecastID=" + grvforecast.Rows(count).Cells(1).Text + " or";                } else {                    _sqlStr += " f.ForecastID=" + grvforecast.Rows(count).Cells(1).Text + " ";                    _FID += " a.ForecastID=" + grvforecast.Rows(count).Cells(1).Text + " ";                }                count = count + 1;            }            if ((btnValue.Value == "Forecast Data Report")) {                _sqlStr += "GROUP BY p.LegalEntityname,d.UpdatedBy";            } else if ((btnValue.Value == "Cash/GAAP Report")) {                _sqlStr += " GROUP BY Portfolio.LegalEntityname,Forecast_Data.TheDate,Forecast_Attribute.Value) AS d left OUTER JOIN vendor ON d.Value = vendor.vendorname";                _sqlStr += " GROUP BY d.OriginalDate,d.AccountNumber,d.Portfolio,d.name,d.Accountname,d.AttributeSetname,d.Year,vendor.vendorname";                //    _sqlStr &= " GROUP BY Portfolio.LegalEntityname,Forecast_Attribute.Value) AS d"                //    _sqlStr &= " Group BY OriginalDate,Portfolio,Year"            }        }        try {            if ((btnValue.Value != "Forecast Attribute Report")) {                _ds = new DataSet();                _dttable = myDB.ExecuteDatatable(CommandType.Text,_sqlStr,null);                _dttable.tablename = btnValue.Value.Replace("_"," ");                _ds.tables.Add(_dttable);            } else {                _ds = new DataSet();                _sqlStr += "Group by f.AttributeSetID,Attribute_Set.Tabname  ";                _dttable = myDB.ExecuteDatatable(CommandType.Text,null);                foreach (DaTarow _dr in _dttable.Rows) {                    _sqlStr = "[USP_Forecast_Attributes] " + _dr["AttributeSetID"].ToString() + ",'" + _FID + "'";                    _dttable = myDB.ExecuteDatatable(CommandType.Text,null);                    _dttable.tablename = _dr["AttributeSetname"].ToString();                    _ds.tables.Add(_dttable);                }            }            if ((_ds != null)) {                if (DateAndTime.Now.IsDaylightSavingTime()) {                    strTime = System.DateTime.UtcNow.AddHours(-5).ToString("_MM_dd_yyyy_hh_mm_tt");                } else {                    strTime = System.DateTime.UtcNow.AddHours(-4).ToString("_MM_dd_yyyy_hh_mm_tt");                }                if (btnValue.Value != "Forecast Attribute Report") {                    epXL.CreateExcel(btnValue.Value,_ds);                //epXL.ExportToExcel(_dttable,btnValue.Value)                } else {                    epXL.CreateExcel(btnValue.Value,_ds);                    // & strTime,_ds)                }            }        } catch (Exception ex) {            Jsscript = "<script>alert('Report not generated')</script>";            this.Page.ClIEntScript.RegisterStartupScript(Page.GetType,Jsscript);        }    }}

编辑:
现在我收到了确切的错误:

System.IO.IsolatedStorage.IsolatedStorageException: Unable to create mutex. (Exception from HRESulT: 0x80131464)   at System.IO.IsolatedStorage.IsolatedStoragefile.Open(String infofile,String syncname)   at System.IO.IsolatedStorage.IsolatedStoragefile.Lock(Boolean& locked)   at System.IO.IsolatedStorage.IsolatedStoragefileStream..ctor(String path,fileMode mode,fileAccess access,fileShare share,Int32 bufferSize,IsolatedStoragefile isf)   at MS.Internal.IO.Packaging.PackagingUtilitIEs.SafeIsolatedStoragefileStream..ctor(String path,ReliableIsolatedStoragefileFolder folder)   at MS.Internal.IO.Packaging.PackagingUtilitIEs.createuserScopedisolatedStoragefileStreamWithRandomname(Int32 retryCount,String& filename)   at MS.Internal.IO.Packaging.SparseMemoryStream.SwitchModeIfNecessary()   at MS.Internal.IO.Packaging.SparseMemoryStream.Write(Byte[] buffer,Int32 offset,Int32 count)   at MS.Internal.IO.Packaging.CompressEmulationStream.Write(Byte[] buffer,Int32 count)   at MS.Internal.IO.Packaging.Compressstream.Write(Byte[] buffer,Int32 count)   at MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(Byte[] buffer,Int32 count)   at MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(Byte[] buffer,Int32 count)   at System.IO.StreamWriter.Flush(Boolean flushStream,Boolean flushEncoder)   at System.IO.StreamWriter.Write(String value)   at System.IO.TextWriter.Write(String format,Object arg0)   at OfficeOpenXml.ExcelWorksheet.UpdateRowCellData(StreamWriter sw)   at OfficeOpenXml.ExcelWorksheet.SaveXml()   at OfficeOpenXml.ExcelWorksheet.Save()   at OfficeOpenXml.ExcelWorkbook.Save()   at OfficeOpenXml.ExcelPackage.GetAsByteArray(Boolean save)   at OfficeOpenXml.ExcelPackage.GetAsByteArray()

我无法解决它.请建议.
谢谢

解决方法 我认为您的网页存在设计问题.至少,快速解决方法是在将文档写入Response之前添加它.

httpContext.Current.Response.Clear();

当您在输出中有响应时,表示它尝试呈现页面,在其中,您尝试执行其他 *** 作,在这种情况下编写文件. (以How can I return a pdf from a web request in ASP.NET?为例).

我们必须看到更多的代码才能完全检查,但我想你不会使用httpHandler,我建议你这样做.

public class DownloadfileHandler : IhttpHandler{    public bool IsReusable    {        get { return true; }    }    public voID ProcessRequest(httpContext context)    {        Response.BinaryWrite(...);    }}
总结

以上是内存溢出为你收集整理的c# – 大数据损坏的excel文件全部内容,希望文章能够帮你解决c# – 大数据损坏的excel文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1230490.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存