EPPlus - 'Table Table1 Column Type does not have a unique name' 从 csv 文件生成 xlsx 文件时

EPPlus - 'Table Table1 Column Type does not have a unique name' when generating xlsx file from csv file

调用 package.Save() 时出现以下错误:

Table Table1 Column Type does not have a unique name

我给 table 起了个名字,确保任何空单元格都有一个默认的空类型,但仍然找不到哪里出错了或者我如何设置不唯一的列类型名称。 这是我使用的代码:

 public static bool ConvertToXlsx(string csvFilePath)
    {
        bool success = false;

        //we need an xlsx file path for the export and need to ensure the passed in file path is a CSV one
        var xlsxFilePath = Path.ChangeExtension(csvFilePath, "xlsx");
        csvFilePath = Path.ChangeExtension(csvFilePath, "csv");

        //convert the csv
        if (!string.IsNullOrWhiteSpace(xlsxFilePath) && !string.IsNullOrWhiteSpace(csvFilePath))
        {
            try
            {
                using (ExcelPackage package = new ExcelPackage(new FileInfo(xlsxFilePath)))
                {
                    //add a/another worksheet with datetime value so it doesn't clash with existing worksheets to the document
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Export_" + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss"));

                    //starting from cell A1, load in the CSV file data with first row as the header
                    worksheet.Cells["A1"].LoadFromText(
                        new FileInfo(csvFilePath), new ExcelTextFormat
                        {
                            Delimiter = ','
                        }, OfficeOpenXml.Table.TableStyles.Light1, true);
                    worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
                    foreach (var cell in worksheet.Cells)
                    {
                        if (cell.Value == null)
                        {
                            cell.Value = "";
                        }
                    }
                    //save as xlsx
                    package.Save();
                    success = true;
                }
                //attempt to delete the previously generated CSV file
                File.Delete(csvFilePath);
            }
            catch (Exception ex)
            {
                //if we cant delete the origionaly generated CSV file (used for the conversion), then return true

                if (ex.Message.Contains($"Access to the path '{csvFilePath}' is denied."))
                {
                    return true;
                }
                Console.WriteLine("Error: " + ex.Message);
                return false;
            }
        }
        return success;
    }

错误消息有点不清楚:它实际上是在说 "The Table 'Table1', Column 'Type' does not have a unique name"。查看 here 会发生什么。

LoadFromText 会在您的工作表中创建一个 Table,称为 "Table1"。你说 "I gave the table a name",我会说你没有。 EPPlus 将 table 命名为 "Table1"。 It happens here。您命名了工作表,table 是自动命名的。

检查源 csv 的第一行,它可能不止一次包含单词 "Type"。您可能希望在传递给 epplus 之前稍微修改其内容(检查重复项),或者在导入文本时检查 not using the first line as a header 的一些其他重载。

我遇到了同样的问题,解决方法如下

public class ProductModel
{
  [DisplayName("Name")]
  public string ProductName {get; set;}

  [DisplayName("Price")]
  public decimal Price {get; set;}

  [DisplayName("Price")]
  public string Category {get ;set;}
}

[显示名称("Price")]-->重复