Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当存在大量行时,OpenXml会创建损坏的Excel_C#_Excel_Openxml - Fatal编程技术网

C# 当存在大量行时,OpenXml会创建损坏的Excel

C# 当存在大量行时,OpenXml会创建损坏的Excel,c#,excel,openxml,C#,Excel,Openxml,我使用openxml实现了这个方法来创建Excel电子表格。当行数低于15000行时,它100%都能工作,否则Excel有时会显示一条消息而损坏 Microsoft Excel无法打开或修复工作簿,因为它已损坏 有什么解释吗 public static SaveResults CreateExcelDocument(Stream excelStream, List<ExportColumns> columns, IEnumerable data, string sheetNa

我使用openxml实现了这个方法来创建Excel电子表格。当行数低于15000行时,它100%都能工作,否则Excel有时会显示一条消息而损坏

Microsoft Excel无法打开或修复工作簿,因为它已损坏

有什么解释吗

    public static SaveResults CreateExcelDocument(Stream excelStream, List<ExportColumns> columns, IEnumerable data, string sheetName)
    {
        SaveResults results = new SaveResults();
        results.Status = SAVE_STATUS.ERROR;

        try
        {
            if ((sheetName == null) || (sheetName.Trim() == ""))
                sheetName = "Report";
        }
        catch { }

        try
        {
            using (SpreadsheetDocument document = SpreadsheetDocument.Create(excelStream, SpreadsheetDocumentType.Workbook))
            {

                // Add a WorkbookPart to the document.
                WorkbookPart workbookPart = document.AddWorkbookPart();
                workbookPart.Workbook = new Workbook();

                // Add a WorksheetPart to the WorkbookPart.
                WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
                SheetData sheetData = new SheetData();
                worksheetPart.Worksheet = new Worksheet(sheetData);

                // Add Sheets to the Workbook.
                Sheets sheets = document.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

                Sheet sheet = new Sheet()
                {
                    Id = workbookPart.GetIdOfPart(worksheetPart),
                    SheetId = 1,
                    Name = sheetName
                };

                //get the type of the object in the list
                Type myListElementType = data.GetType().GetGenericArguments().Single();

                // Constructing header
                Row row = new Row();

                foreach (ExportColumns exportColumn in columns)
                {
                    if (!string.IsNullOrEmpty(exportColumn.display.Trim()))
                    {
                        row.Append(ConstructCell(exportColumn.display, CellValues.String));
                    }
                }

                // Insert the header row to the Sheet Data
                sheetData.AppendChild(row);


                foreach (var obj in data)
                {
                    row = new Row();

                    foreach (ExportColumns exportColumn in columns)
                    {
                        if (!string.IsNullOrEmpty(exportColumn.display.Trim()))
                        {
                            object cellObject = null;

                            try
                            {
                                if (myListElementType.GetProperty(exportColumn.name) == null)
                                {
                                    cellObject = ((IDictionary<string, object>)obj)[exportColumn.name];
                                }
                                else
                                {
                                    cellObject = myListElementType.GetProperty(exportColumn.name).GetValue(obj);
                                }

                                //if the value is null bad things happen so put in a cell with a null value string
                                if (cellObject == null)
                                {
                                    // create cell with data
                                    row.Append(
                                    ConstructCell(null, CellValues.String)
                                    );
                                    continue;
                                }

                                if (cellObject is DateTime)
                                {
                                    // create cell with data
                                    DateTime dtValue = (DateTime)cellObject;
                                    row.Append(
                                    ConstructCell(dtValue.ToOADate().ToString(), CellValues.String)
                                    );
                                }
                                else if (IsNumber(cellObject))
                                {
                                    // create cell with data
                                    row.Append(ConstructCell(cellObject.ToString(), CellValues.Number));
                                }
                                else
                                {
                                    // create cell with data
                                    row.Append(ConstructCell(SanitizeXmlString(cellObject.ToString()), CellValues.String));
                                }
                            }
                            catch (Exception ex2)
                            {
                                row.Append(ConstructCell(SanitizeXmlString(exportColumn.path.Trim()), CellValues.String));

                            }
                        }
                    }
                    sheetData.AppendChild(row);
                }

                sheets.Append(sheet);

                worksheetPart.Worksheet.Save();
                document.Close();
            }

            results.Status = SAVE_STATUS.OK;
        }
        catch (Exception ex)
        {
            Logger.Exception.Error("Export Error", ex);

            results.Exception = new ExceptionData(ex);
            results.Status = SAVE_STATUS.ERROR;
        }

        return results;
    }

    private static Cell ConstructCell(string value, CellValues dataType)
    {
        return new Cell()
        {
            CellValue = new CellValue(value),
            DataType = new EnumValue<CellValues>(dataType)
        };
    }
public static SaveResults CreateExcelDocument(流excelStream、列表列、IEnumerable数据、字符串sheetName)
{
SaveResults=新的SaveResults();
结果.Status=SAVE_Status.ERROR;
尝试
{
如果((sheetName==null)| |(sheetName.Trim()==“”))
sheetName=“报告”;
}
捕获{}
尝试
{
使用(SpreadsheetDocument文档=SpreadsheetDocument.Create(excelStream,SpreadsheetDocumentType.工作簿))
{
//将工作簿部件添加到文档中。
WorkbookPart WorkbookPart=document.AddWorkbookPart();
workbookPart.工作簿=新工作簿();
//将工作表部件添加到工作簿部件。
WorksheetPart WorksheetPart=workbookPart.AddNewPart();
SheetData SheetData=新的SheetData();
工作表部分工作表=新工作表(sheetData);
//将工作表添加到工作簿中。
Sheets Sheets=document.WorkbookPart.Workbook.AppendChild(new Sheets());
图纸=新图纸()
{
Id=workbookPart.GetIdOfPart(工作表部分),
SheetId=1,
名称=图纸名称
};
//获取列表中对象的类型
类型myListElementType=data.GetType().GetGenericArguments().Single();
//构造标头
行=新行();
foreach(ExportColumns exportColumn in columns)
{
如果(!string.IsNullOrEmpty(exportColumn.display.Trim()))
{
Append(ConstructCell(exportColumn.display,CellValues.String));
}
}
//将标题行插入图纸数据
sheetData.AppendChild(行);
foreach(数据中的var obj)
{
行=新行();
foreach(ExportColumns exportColumn in columns)
{
如果(!string.IsNullOrEmpty(exportColumn.display.Trim()))
{
对象cellObject=null;
尝试
{
if(myListElementType.GetProperty(exportColumn.name)==null)
{
cellObject=((IDictionary)obj)[exportColumn.name];
}
其他的
{
cellObject=myListElementType.GetProperty(exportColumn.name).GetValue(obj);
}
//如果该值为null,则会发生不好的情况,因此请将其放在具有null值字符串的单元格中
if(cellObject==null)
{
//使用数据创建单元格
行。追加(
ConstructCell(null,CellValues.String)
);
继续;
}
if(cellObject为日期时间)
{
//使用数据创建单元格
DateTime dtValue=(DateTime)cellObject;
行。追加(
ConstructCell(dtValue.ToOADate().ToString(),CellValues.String)
);
}
else if(IsNumber(cellObject))
{
//使用数据创建单元格
Append(ConstructCell(cellObject.ToString(),CellValues.Number));
}
其他的
{
//使用数据创建单元格
Append(ConstructCell(SanitizeXmlString(cellObject.ToString()),CellValues.String));
}
}
捕获(异常ex2)
{
Append(ConstructCell(SanitizeXmlString(exportColumn.path.Trim()),CellValues.String));
}
}
}
sheetData.AppendChild(行);
}
附页(页);
worksheetPart.Worksheet.Save();
document.Close();
}
结果.Status=保存状态.OK;
}
捕获(例外情况除外)
{
Logger.Exception.Error(“导出错误”,ex);
结果:异常=新的异常数据(ex);
结果.Status=SAVE_Status.ERROR;
}
返回结果;
}
私有静态单元格ConstructCell(字符串值、CellValues数据类型)
{
返回新单元格()
{
CellValue=新的CellValue(值),
数据类型=新的枚举值(数据类型)
};
}

两个问题:首先,您使用的是哪个版本的开放式XML SDK?第二,您是否曾经在一次测试之前看过开放式XML标记