Skip to content

Commit

Permalink
Using DynamicConfiguration when writing data using DataTable (#561)
Browse files Browse the repository at this point in the history
Co-authored-by: Paweł Szybiak <pawel.szybiak@vsoft.pl>
  • Loading branch information
pszybiak and Paweł Szybiak authored Jan 20, 2024
1 parent cfdc452 commit cd1166c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -560,18 +559,30 @@ private void GenerateSheetByDataTable(MiniExcelStreamWriter writer, DataTable va
// dimension
var maxRowIndex = value.Rows.Count + (_printHeader && value.Rows.Count > 0 ? 1 : 0);
var maxColumnIndex = value.Columns.Count;
writer.Write($@"<x:dimension ref=""{GetDimensionRef(maxRowIndex, maxColumnIndex)}""/><x:sheetData>");
writer.Write($@"<x:dimension ref=""{GetDimensionRef(maxRowIndex, maxColumnIndex)}""/>");

var props = new List<ExcelColumnInfo>();
for (var i = 0; i < value.Columns.Count; i++)
{
var columnName = value.Columns[i].Caption ?? value.Columns[i].ColumnName;
var prop = GetColumnInfosFromDynamicConfiguration(columnName);
props.Add(prop);
}

WriteColumnsWidths(writer, props);

writer.Write("<x:sheetData>");
if (_printHeader)
{
writer.Write($"<x:row r=\"{yIndex}\">");
var xIndex = xy.Item1;
foreach (DataColumn c in value.Columns)
foreach (var p in props)
{
var r = ExcelOpenXmlUtils.ConvertXyToCell(xIndex, yIndex);
WriteC(writer, r, columnName: c.Caption ?? c.ColumnName);
WriteC(writer, r, columnName: p.ExcelColumnName);
xIndex++;
}

writer.Write($"</x:row>");
yIndex++;
}
Expand Down Expand Up @@ -613,7 +624,7 @@ private void GenerateSheetByIDataReader(MiniExcelStreamWriter writer, IDataReade
for (var i = 0; i < reader.FieldCount; i++)
{
var columnName = reader.GetName(i);
var prop = GetColumnInfosForIDataReader(columnName);
var prop = GetColumnInfosFromDynamicConfiguration(columnName);
props.Add(prop);
}

Expand Down Expand Up @@ -662,7 +673,7 @@ private void GenerateSheetByIDataReader(MiniExcelStreamWriter writer, IDataReade
}
}
private ExcelColumnInfo GetColumnInfosForIDataReader(string columnName)
private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName)
{
var prop = new ExcelColumnInfo
{
Expand Down
55 changes: 54 additions & 1 deletion tests/MiniExcelTests/MiniExcelOpenXmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,60 @@ public void DynamicColumnsConfigurationIsUsedWhenCreatingExcelUsingIDataReader()
Assert.Contains("Its value", rows[0]);
Assert.Contains("Name of something", rows[1]);
Assert.Contains("Its value", rows[1]);


Assert.Equal("MiniExcel", rows[0]["Name of something"]);
Assert.Equal(1D, rows[0]["Its value"]);
Assert.Equal("Github", rows[1]["Name of something"]);
Assert.Equal(2D, rows[1]["Its value"]);
}
}

[Fact]
public void DynamicColumnsConfigurationIsUsedWhenCreatingExcelUsingDataTable()
{
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
var table = new DataTable();
{
table.Columns.Add("Column1", typeof(string));
table.Columns.Add("Column2", typeof(int));
table.Rows.Add("MiniExcel", 1);
table.Rows.Add("Github", 2);
}

var configuration = new OpenXmlConfiguration
{
DynamicColumns = new[]
{
new DynamicExcelColumn("Column1")
{
Name = "Name of something",
Index = 0,
Width = 150
},
new DynamicExcelColumn("Column2")
{
Name = "Its value",
Index = 1,
Width = 150
}
}
};

MiniExcel.SaveAs(path, table, configuration: configuration);

using (var stream = File.OpenRead(path))
{
var rows = stream.Query(useHeaderRow: true)
.Select(x => (IDictionary<string, object>)x)
.Select(x => (IDictionary<string, object>)x)
.ToList();

Assert.Contains("Name of something", rows[0]);
Assert.Contains("Its value", rows[0]);
Assert.Contains("Name of something", rows[1]);
Assert.Contains("Its value", rows[1]);


Assert.Equal("MiniExcel", rows[0]["Name of something"]);
Assert.Equal(1D, rows[0]["Its value"]);
Assert.Equal("Github", rows[1]["Name of something"]);
Expand Down

0 comments on commit cd1166c

Please sign in to comment.