指数数组的边界之外。使用EP Plus

时间:2019-03-07 06:04:38

标签: c# asp.net excel asp.net-mvc epplus

我有一个字节流,其中包含所有使用Ep的数据,我想以具有密码保护的Excel格式导出文件。但这行显示错误:

ws.Cells["A1"].LoadFromCollection(fileBytes.ToList());

我的整个代码:

    public static void SendFileBytes(HttpResponse response, byte[] fileBytes, string fileName, string mimeType)
    {
        if (response != null && fileBytes != null && fileBytes.Length > 0 && !String.IsNullOrWhiteSpace(fileName) && !String.IsNullOrWhiteSpace(mimeType))
        {
            response.Clear();
            response.Buffer = true;
            response.ContentEncoding = System.Text.Encoding.UTF8;
            response.ContentType = mimeType; // "application/pdf";
            response.AddHeader("content-disposition", "attachment;filename=" + Uri.EscapeDataString(fileName));
            response.Charset = "";
            response.Cache.SetCacheability(HttpCacheability.NoCache);



            using (ExcelPackage pack = new ExcelPackage())
            {

                ExcelWorksheet ws = pack.Workbook.Worksheets.Add("heelo");

                ws.Cells["A1"].LoadFromCollection(fileBytes.ToList());
                pack.Save("123");
                var ms = new System.IO.MemoryStream();
                pack.SaveAs(ms);

                ms.WriteTo(HttpContext.Current.Response.OutputStream);
                ms.Close();


            }
            response.Flush();
            response.End();
        }
    }

1 个答案:

答案 0 :(得分:0)

LoadFromCollection需要一个IEnumerable<T>,而不是字节数组。

List<string> list = new List<string>()
{
    "AAA",
    "BBB",
    "CCC"
};

ws.Cells["A1"].LoadFromCollection(list , true);