EPP Plus 将先前的工作表数据附加到新工作表

问题描述 投票:0回答:1

我正在使用 EPP plus 写入 Excel 文件,基本上我有 10 个不同的字典,它们位于 foreach 循环中。我为每本词典添加一张新表。 当我添加第一张纸并打印数据时,效果很好。 当我添加下一张纸并添加数据时,它还包括上一张纸的数据。

如何避免这种情况?

foreach (var (field, index) in fieldsList.WithIndex())
{
    var fieldName = field.Key;
    var fieldValue = field.Value.Value.AsDictionary();
    foreach (var item in fieldValue)
    {
        var internalValue = item.Value.Value.AsDictionary();
        var jsonData = JsonConvert.SerializeObject(internalValue, Formatting.Indented);
        var customFieldClasses = JsonConvert.DeserializeObject<CustomFieldClasses>(jsonData);
        var customFieldDictionary = ObjectToDictionaryConverter.ConvertToDictionary(customFieldClasses);

        foreach (var kvp in customFieldDictionary)
        {
            string customKey = kvp.Key;
            string customValue = (kvp.Value != null) ? kvp.Value.Content : "";
            if (!keyValuePairs.ContainsKey(customKey))
            {
                keyValuePairs[customKey] = new List<string>();
            }
            keyValuePairs[customKey].Add(customValue);
        }
    }
    ExportToExcel(keyValuePairs, excelFilePath, "DataPage" + index.ToString());
}
static void ExportToExcel(Dictionary<string, List<string>> data, string filePath, string sheetName)
{
    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
    // Create a new Excel package
    using (var package = new ExcelPackage(filePath))
    {
        ExcelWorksheet worksheet;
        var existingWorksheet = package.Workbook.Worksheets.FirstOrDefault(ws => ws.Name == sheetName);
        // Add a worksheet to the package
        if (existingWorksheet == null)
        {
            worksheet = package.Workbook.Worksheets.Add(sheetName);
        }
        else
        {
            worksheet = existingWorksheet;
        }
        // Populate the worksheet with data from the dictionary
        
        int row = 1;
        int subRow = 1;
        int column = 1;
        foreach (var kvp in data)
        {
            worksheet.Cells[row, column].Value = kvp.Key;
            foreach (var item in kvp.Value)
            {
                subRow++;
                worksheet.Cells[subRow, column].Value = item;
            }
            column++;
            subRow = 1;
        }
        package.Save();
    }
}

我尝试同时使用 package.Save() 和 SaveAs,两者的工作原理相同。

c# asp.net .net epplus
1个回答
0
投票

您遇到的问题源于您在循环迭代中重复使用相同的 Dictionary 对象 keyValuePairs。该字典会累积每次迭代的数据,因此当您为下一张表添加数据时,它还包括前一张表中的数据。

要解决此问题,您需要为循环的每次迭代创建一个新的 Dictionary 对象。您可以通过将字典创建移动到循环内来完成此操作。以下是修改代码的方法:

 foreach (var (field, index) in fieldsList.WithIndex())
{
    var fieldName = field.Key;
    var fieldValue = field.Value.Value.AsDictionary();
    
    // Create a new dictionary for each iteration
    var keyValuePairs = new Dictionary<string, List<string>>();

    foreach (var item in fieldValue)
    {
        var internalValue = item.Value.Value.AsDictionary();
        var jsonData = JsonConvert.SerializeObject(internalValue, Formatting.Indented);
        var customFieldClasses = JsonConvert.DeserializeObject<CustomFieldClasses>(jsonData);
        var customFieldDictionary = ObjectToDictionaryConverter.ConvertToDictionary(customFieldClasses);

        foreach (var kvp in customFieldDictionary)
        {
            string customKey = kvp.Key;
            string customValue = (kvp.Value != null) ? kvp.Value.Content : "";
            if (!keyValuePairs.ContainsKey(customKey))
            {
                keyValuePairs[customKey] = new List<string>();
            }
            keyValuePairs[customKey].Add(customValue);
        }
    }
    ExportToExcel(keyValuePairs, excelFilePath, "DataPage" + index.ToString());
}

通过此修改,循环的每次迭代都会创建一个新的 keyValuePairs 字典,确保先前迭代中的数据不会转移到后续工作表中。

© www.soinside.com 2019 - 2024. All rights reserved.