MiniExcel icon indicating copy to clipboard operation
MiniExcel copied to clipboard

DynamicColumns Name not working as expected

Open GianlucaLocri opened this issue 3 years ago • 1 comments

Description

I've tried this code to format my columns with custom name but it is not working as expected. Here is a minimal reproducible example:

dynamic results = new List<dynamic>();
var resultRow = new ExpandoObject() as IDictionary<string, Object>;

resultRow.Add("Code", 12345);
results.Add(resultRow);

var resultsExcelConfig = new OpenXmlConfiguration
{
   DynamicColumns = new DynamicExcelColumn[] {
      new DynamicExcelColumn("Code"){Name="Code Custom Name"},
   }
};

MiniExcel.SaveAs(myPath, results, configuration: resultsExcelConfig);

The resulting file has an empty column header with no data. If I omit the configuration, the code works but the column name is the default one...

empty.txt

GianlucaLocri avatar Oct 20 '22 16:10 GianlucaLocri

For anyone still looking for a solution to this: you can use the new DataTable() approach instead. Paraphrasing the documentation (refer to the README.md in this repo in case you want more details):

DataTable dt = new DataTable();

/* ... */

DataRow dr = dt.NewRow();

dr["Name1"] = 15.2224;

dt.Rows.Add(dr);

OpenXmlConfiguration configuration = new OpenXmlConfiguration()
{
     EnableWriteNullValueCell = true,
     DynamicColumns = new [] {
         new DynamicExcelColumn("Name1") { Format = "0.00" } 
     }.ToList()
};
MiniExcel.Save("C:/your/path", dt, configuration)

anteeek avatar Sep 09 '23 18:09 anteeek