EPPlus - 表格表1 列类型在从 csv 文件生成 xlsx 文件时没有独有的名称
原标题: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;
}
最佳回答
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.
问题回答
I faced the same problem and solved is as follows
public class ProductModel
{
[DisplayName("Name")]
public string ProductName {get; set;}
[DisplayName("Price")]
public decimal Price {get; set;}
[DisplayName("Price")]
public string Category {get ;set;}
}
[DisplayName("Price")]-->duplicate
we are facing the same issue in our production environment. Both its quite random. If I use the same data on dev, it works fine. I have checked the code and there are no duplicate columns.
Please help
相关问题
What is the use of `default` keyword in C#?
What is the use of default keyword in C#?
Is it introduced in C# 3.0 ?
Anyone feel like passing it forward?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs.
...
NSArray s, Primitive types and Boxing Oh My!
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits.
Now here s the question: I feel really inclined to create some type of ...
C# Marshal / Pinvoke CBitmap?
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class.
My import looks like this:
[DllImport(@"test.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr ...
ADO.NET Entity Framework Association of Entities by Value Range
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber.
I want to create a many to many association ...
How to Use Ghostscript DLL to convert PDF to PDF/A
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
What is the most efficient keyvalue pair for ordering?
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Linqy no matchy
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control.
...