I have the following XLSX template:
Column1Lbl | Column2Lbl | … | Column30Lbl | Column31Lbl | Column32Lbl | Column33Lbl |
---|---|---|---|---|---|---|
{{item.Column1}} | {{item.Column2}} | … | {{item.Column30}} | {{item.Column31}} | {{item.Column32}} | {{item.Column33}} |
{{item.Column1}} | {{item.Column2}} | … | {{item.Column30}} | {{item.Column31}} | {{item.Column32}} | {{item.Column33}} |
{{item.Column1}} | {{item.Column2}} | … | {{item.Column30}} | {{item.Column31}} | {{item.Column32}} | {{item.Column33}} |
… |
When exporting, it results in:
Título 1 | Título 2 | … | Título 30 | Título 31 | Título 32 | Título 33 |
---|---|---|---|---|---|---|
Dato 1 1 | Dato 2 1 | … | Dato 30 1 | Dato 31 1 | No property or field ‘column32’ exists in type ‘d__97`1’ | No property or field ‘column33’ exists in type ‘d__97`1’ |
Dato 1 2 | Dato 2 2 | … | Dato 30 2 | Dato 31 2 | ||
Dato 1 3 | Dato 2 3 | … | Dato 30 3 | Dato 31 3 | ||
… |
Here’s the relevant part of my source code:
var template = new XLTemplate(PropertiesFile.GetRequiredProperty(PropertiesFile.TEMPLATE_PATH));
var dataShipments = new DataTable();
dataShipments.Columns.Add("Column1", typeof(string));
dataShipments.Columns.Add("Column2", typeof(string));
...
dataShipments.Columns.Add("Column30", typeof(string));
dataShipments.Columns.Add("Column31", typeof(string));
dataShipments.Columns.Add("Column32", typeof(string));
dataShipments.Columns.Add("Column33", typeof(string));
IQueryable<IGrouping<string, DataItems>> groupItemsByColumn2 = dataItems.GroupBy(item => item.column2);
foreach (IGrouping<string, DataItems> groupedItemByColumn2 in groupItemsByColumn2)
{
IList<DataItems> items = groupedItemByColumn2.ToList();
foreach (DataItems item in items)
{
//Aquíe es donde se asignan los valores:
DataRow newRow = dataShipments.NewRow();
newRow["Column1"] = item.Column1 ?? string.Empty;
newRow["Column2"] = item.Column2 ?? string.Empty;
...
newRow["Column30"] = item.Column30 ?? string.Empty;
newRow["Column31"] = item.Column31 ?? string.Empty;
newRow["Column32"] = item.Column32 ?? string.Empty;
newRow["Column33"] = item.Column33 ?? string.Empty;
dataShipments.Rows.Add(newRow);
}
}
template.AddVariable("item",dataShipments);
//Lo siguiente parece que funciona bien debido a que se aplican todos los títulos:
template.AddVariable("Column1Lbl", "Títlo 1");
template.AddVariable("Column2Lbl", "Títlo 2");
...
template.AddVariable("Column30Lbl", "Títlo 30");
template.AddVariable("Column31Lbl", "Títlo 31");
template.AddVariable("Column32Lbl", "Títlo 32");
template.AddVariable("Column33Lbl", "Títlo 33");
template.Generate();
Interestingly, if I swap Column 30 with Column 32 by copying and pasting, it results in:
Título 1 | Título 2 | … | Título 30 | Título 31 | Título 32 | Título 33 |
---|---|---|---|---|---|---|
Dato 1 1 | Dato 2 1 | … | Dato 32 1 | Dato 20 1 | No property or field ‘column30’ exists in type ‘d__97`1’ | No property or field ‘column33’ exists in type ‘d__97`1’ |
Dato 1 2 | Dato 2 2 | … | Dato 32 2 | Dato 20 2 | ||
Dato 1 3 | Dato 2 3 | … | Dato 32 3 | Dato 20 3 | ||
… |
Note that the titles are processed correctly.
1