Skip to content

Commit

Permalink
Add exporting and reading custom fields system names
Browse files Browse the repository at this point in the history
  • Loading branch information
DWDBE committed Apr 3, 2024
1 parent 74af043 commit 30e0052
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.0.13</Version>
<Version>10.0.14</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Title>Excel Provider</Title>
<Description>Excel Provider</Description>
Expand Down
9 changes: 7 additions & 2 deletions src/ExcelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,13 @@ private void GetSchemaForTableFromFile(Schema schema, Dictionary<string, ExcelRe
columnCount = dt.Columns.Count;
}
foreach (System.Data.DataColumn c in dt.Columns)
{
excelTable.AddColumn(new Column(c.ColumnName, c.DataType, excelTable));
{
Column column = new Column(c.ColumnName, c.DataType, excelTable);
if(!string.IsNullOrEmpty(c.Caption) && !string.Equals(c.Caption, c.ColumnName, StringComparison.OrdinalIgnoreCase))
{
column.NameWithWhitespaceStripped = c.Caption;
}
excelTable.AddColumn(column);
}

}
Expand Down
15 changes: 13 additions & 2 deletions src/ExcelReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,22 @@ private void LoadExcelFile()
int i = 0;
foreach (var firstRowCell in worksheet.Cells[1, 1, 1, worksheet.Dimension.End.Column])
{
DataColumn column;
var header = hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column);
if (!dataTable.Columns.Contains(header) && !string.IsNullOrWhiteSpace(header))
dataTable.Columns.Add(header);
{
column = dataTable.Columns.Add(header);
}
else
dataTable.Columns.Add(header + i);
{
column = dataTable.Columns.Add(header + i);
}

if (!string.IsNullOrEmpty(firstRowCell.Comment?.Text))
{
column.Caption = firstRowCell.Comment.Text;
}

i++;
}

Expand Down
17 changes: 12 additions & 5 deletions src/ExportDataToExcelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ExportDataToExcelProvider : Integration.Interfaces.IDataExportProvi
{
private FieldsHelper FieldsHelper = new FieldsHelper();
private IEnumerable<ProductField> CustomFields = ProductField.GetProductFields();
private const string CommentsAuthor = "Dynamicweb";

public ExportDataToExcelProvider()
{
Expand Down Expand Up @@ -68,20 +69,26 @@ private ExcelPackage GetExcelPackage(string file)

private ExcelWorksheet GetExcelWorksheet(ExcelPackage package, string title)
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(title);
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(title);
return worksheet;
}

private ExcelRange AddCell(ExcelWorksheet worksheet, string text, int row, int column)
{
ExcelRange cell = worksheet.Cells[row, column];
cell.Value = text;
cell.Value = text;
return cell;
}

private void AddHeaderCell(ExcelWorksheet worksheet, string text, int row, int column)
private void AddHeaderCell(ExcelWorksheet worksheet, string text, int row, int column, string fieldSystemName)
{
AddCell(worksheet, text, row, column);
var cell = AddCell(worksheet, text, row, column);
if(!string.IsNullOrEmpty(fieldSystemName) &&
(fieldSystemName.StartsWith("CustomFields|", System.StringComparison.OrdinalIgnoreCase) ||
fieldSystemName.StartsWith("CategoryFields|", System.StringComparison.OrdinalIgnoreCase)))
{
cell.AddComment(fieldSystemName, CommentsAuthor);
}
}

private void AddHeader(ExcelWorksheet worksheet, IDictionary<string, string> fields)
Expand All @@ -99,7 +106,7 @@ private void AddHeader(ExcelWorksheet worksheet, IDictionary<string, string> fie
{
caption = GetFieldCaption(field, defaultLanguage);
}
AddHeaderCell(worksheet, caption, firstRowIndex, lastColumnIndex++);
AddHeaderCell(worksheet, caption, firstRowIndex, lastColumnIndex++, field);
}
}

Expand Down

0 comments on commit 30e0052

Please sign in to comment.