Search code examples
c#epplus

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


I get the following error when calling the package.Save():

Table Table1 Column Type does not have a unique name

I gave the table a name, ensured that any null cells have a default empty type, but still cant find where its going wrong or how I can set the Column Type name that is not unique. Here's the code I use:

 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;
    }

Solution

  • The error message is a little bit unclear: it's actually saying "The Table 'Table1', Column 'Type' does not have a unique name". See here what happens.

    LoadFromText will provoke the creation of a Table inside your worksheet, it is called "Table1". You say "I gave the table a name", well I'd say you didn't. EPPlus named the table "Table1". It happens here. You named the worksheet, the table was named automatically.

    Check the first line of your source csv, it probably contains the word "Type" more than once. You might want to modify its contents slightly before passing to epplus (check for duplicates), or check out some of the other overloads for not using the first line as a header when importing text.