简体   繁体   English

如何生成超过 1048576 行的 Excel 工作表

[英]How to generate Excel sheets with more than 1048576 rows

when I try to generate an excel file with more than 1048576 rows, I get the error:当我尝试生成超过 1048576 行的 excel 文件时,出现错误:

System.ArgumentOutOfRangeException: Row number must be between 1 and 1048576> System.ArgumentOutOfRangeException:行号必须在 1 到 1048576 之间>

How can I generate the excel file (currently I have an 7,000,000 rows query to export).如何生成 excel 文件(目前我有 7,000,000 行查询要导出)。

This is how I am generating the files:这就是我生成文件的方式:

public static byte[] ExportToExcel(IEnumerable<object> data, string sheetName)
    {

        if (data == null)
        {
            return null;
        }

        XLWorkbook wb = new XLWorkbook();

        var ws = wb.Worksheets.Add(sheetName);

        var columns = data.FirstOrDefault().GetType().GetProperties().ToList();

        var greyBackgroun = XLColor.FromHtml("#DDDDDD");

        for (var i = 0; i < columns.Count; i++)
        {
            ws.Row(1).Cell(i + 1).Value = columns[i].GetDisplayName();
            ws.Row(1).Cell(i + 1).Style.Font.SetBold(true);
            ws.Row(1).Cell(i + 1).Style.Fill.SetBackgroundColor(greyBackgroun);

        }

        ws.Cell(2, 1).InsertData(data).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Left);
        ws.ColumnsUsed().AdjustToContents();
        ws.RowsUsed().AdjustToContents();

        using (MemoryStream memoryStream = new MemoryStream())
        {
            wb.SaveAs(memoryStream, false);
            return memoryStream.ToArray();
        }
    }

Update:更新:

Found out that Excel has a row limit (and it is 1048576).发现 Excel 有一个行限制(它是 1048576)。 I'll just generate a bunch of files and zip them instead of generating one file with 7 milion rows.我只会生成一堆文件并压缩它们,而不是生成一个有 700 万行的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM