调用
package.Save()
时出现以下错误:
Table Table1 Column Type 没有唯一的名称
我给了表一个名称,确保任何空单元格都有默认的空类型,但仍然找不到哪里出了问题,或者如何设置不唯一的列类型名称。 这是我使用的代码:
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;
}
public class ProductModel
{
[DisplayName("Name")]
public string ProductName {get; set;}
[DisplayName("Price")]
public decimal Price {get; set;}
[DisplayName("Price")]
public string Category {get ;set;}
}
[显示名称(“价格”)]-->重复
请帮忙