diff --git a/src/Dynamicweb.DataIntegration.Providers.ExcelProvider.csproj b/src/Dynamicweb.DataIntegration.Providers.ExcelProvider.csproj
index 16d25a6..7b40f16 100644
--- a/src/Dynamicweb.DataIntegration.Providers.ExcelProvider.csproj
+++ b/src/Dynamicweb.DataIntegration.Providers.ExcelProvider.csproj
@@ -23,8 +23,8 @@
snupkg
-
-
+
+
diff --git a/src/ExcelDestinationWriter.cs b/src/ExcelDestinationWriter.cs
index 2c68804..216f149 100644
--- a/src/ExcelDestinationWriter.cs
+++ b/src/ExcelDestinationWriter.cs
@@ -1,249 +1,248 @@
-using Dynamicweb.Core;
-using Dynamicweb.DataIntegration.Integration;
-using Dynamicweb.DataIntegration.Integration.Interfaces;
-using Dynamicweb.DataIntegration.ProviderHelpers;
-using Dynamicweb.Logging;
-using OfficeOpenXml;
-using System;
+using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
+using Dynamicweb.Core;
+using Dynamicweb.DataIntegration.Integration;
+using Dynamicweb.DataIntegration.Integration.Interfaces;
+using Dynamicweb.DataIntegration.ProviderHelpers;
+using Dynamicweb.Logging;
+using OfficeOpenXml;
+
+namespace Dynamicweb.DataIntegration.Providers.ExcelProvider;
-namespace Dynamicweb.DataIntegration.Providers.ExcelProvider
+public class ExcelDestinationWriter : IDestinationWriter, IDisposable
{
- public class ExcelDestinationWriter : IDestinationWriter, IDisposable
+ private readonly ILogger _logger;
+ private readonly CultureInfo _cultureInfo;
+
+ public ExcelDestinationWriter()
{
- private readonly ILogger _logger;
- private readonly CultureInfo _cultureInfo;
+ }
- public ExcelDestinationWriter()
+ [Obsolete("Use overload method (string path, string destinationPath, MappingCollection mappings, ILogger logger, CultureInfo cultureInfo) instead.")]
+ public ExcelDestinationWriter(string path, string destinationPath, MappingCollection mappings, ILogger logger)
+ {
+ _path = path;
+ _mappings = mappings;
+ if (!Directory.Exists(path))
{
+ Directory.CreateDirectory(path);
}
+ _destinationPath = destinationPath;
+ _logger = logger;
+ _cultureInfo = CultureInfo.CurrentCulture;
+ }
- [Obsolete("Use overload method (string path, string destinationPath, MappingCollection mappings, ILogger logger, CultureInfo cultureInfo) instead.")]
- public ExcelDestinationWriter(string path, string destinationPath, MappingCollection mappings, ILogger logger)
- {
- _path = path;
- _mappings = mappings;
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- _destinationPath = destinationPath;
- _logger = logger;
- _cultureInfo = CultureInfo.CurrentCulture;
- }
+ public ExcelDestinationWriter(string path, string destinationPath, MappingCollection mappings, ILogger logger, CultureInfo cultureInfo)
+ {
- public ExcelDestinationWriter(string path, string destinationPath, MappingCollection mappings, ILogger logger, CultureInfo cultureInfo)
+ _path = path;
+ _mappings = mappings;
+ if (!Directory.Exists(path))
{
-
- _path = path;
- _mappings = mappings;
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- _destinationPath = destinationPath;
- _logger = logger;
- _cultureInfo = cultureInfo ?? CultureInfo.CurrentCulture;
+ Directory.CreateDirectory(path);
}
+ _destinationPath = destinationPath;
+ _logger = logger;
+ _cultureInfo = cultureInfo ?? CultureInfo.CurrentCulture;
+ }
- private readonly MappingCollection _mappings;
- private readonly string _path;
- private DataSet setForExcel;
- private DataTable tableForExcel;
- private readonly string _destinationPath;
- private Mapping _currentMapping;
- private ColumnMappingCollection _currentColumnMappings;
- public Mapping currentMapping
+ private readonly MappingCollection _mappings;
+ private readonly string _path;
+ private DataSet setForExcel;
+ private DataTable tableForExcel;
+ private readonly string _destinationPath;
+ private Mapping _currentMapping;
+ private ColumnMappingCollection _currentColumnMappings;
+ public Mapping currentMapping
+ {
+ get => _currentMapping;
+ set
{
- get => _currentMapping;
- set
- {
- _currentMapping = value;
- _currentColumnMappings = value.GetColumnMappings();
- }
+ _currentMapping = value;
+ _currentColumnMappings = value.GetColumnMappings();
}
+ }
- public virtual Mapping Mapping
- {
- get { return currentMapping; }
- }
+ public virtual Mapping Mapping
+ {
+ get { return currentMapping; }
+ }
- public virtual MappingCollection Mappings
- {
- get { return _mappings; }
- }
+ public virtual MappingCollection Mappings
+ {
+ get { return _mappings; }
+ }
- public virtual void Write(Dictionary row)
+ public virtual void Write(Dictionary row)
+ {
+ if (tableForExcel == null)
{
- if (tableForExcel == null)
- {
- tableForExcel = GetTableForExcel();
- }
+ tableForExcel = GetTableForExcel();
+ }
- DataRow r = tableForExcel.NewRow();
+ DataRow r = tableForExcel.NewRow();
- foreach (ColumnMapping columnMapping in _currentColumnMappings.Where(cm => cm.Active))
+ foreach (ColumnMapping columnMapping in _currentColumnMappings.Where(cm => cm.Active))
+ {
+ if (columnMapping.HasScriptWithValue)
{
- if (columnMapping.HasScriptWithValue)
+ if (columnMapping.DestinationColumn.Type == typeof(DateTime))
{
- if (columnMapping.DestinationColumn.Type == typeof(DateTime))
- {
- DateTime theDate = DateTime.Parse(columnMapping.GetScriptValue(), CultureInfo.InvariantCulture);
- r[columnMapping.DestinationColumn.Name] = theDate.ToString("dd-MM-yyyy HH:mm:ss:fff", _cultureInfo);
- }
- else if (columnMapping.DestinationColumn.Type == typeof(decimal) ||
- columnMapping.DestinationColumn.Type == typeof(double) ||
- columnMapping.DestinationColumn.Type == typeof(float))
- {
- r[columnMapping.DestinationColumn.Name] = ValueFormatter.GetFormattedValue(columnMapping.GetScriptValue(), _cultureInfo, columnMapping.ScriptType, columnMapping.ScriptValue);
- }
- else
- {
- r[columnMapping.DestinationColumn.Name] = columnMapping.GetScriptValue();
- }
+ DateTime theDate = DateTime.Parse(columnMapping.GetScriptValue(), CultureInfo.InvariantCulture);
+ r[columnMapping.DestinationColumn.Name] = theDate.ToString("dd-MM-yyyy HH:mm:ss:fff", _cultureInfo);
}
- else if (row[columnMapping.SourceColumn.Name] == DBNull.Value)
+ else if (columnMapping.DestinationColumn.Type == typeof(decimal) ||
+ columnMapping.DestinationColumn.Type == typeof(double) ||
+ columnMapping.DestinationColumn.Type == typeof(float))
{
- r[columnMapping.DestinationColumn.Name] = "NULL";
+ r[columnMapping.DestinationColumn.Name] = ValueFormatter.GetFormattedValue(columnMapping.GetScriptValue(), _cultureInfo, columnMapping.ScriptType, columnMapping.ScriptValue);
}
- else if (columnMapping.SourceColumn.Type == typeof(DateTime))
+ else
{
- if (DateTime.TryParse(columnMapping.ConvertInputValueToOutputValue(row[columnMapping.SourceColumn.Name])?.ToString(), out var theDateTime))
- {
- r[columnMapping.DestinationColumn.Name] = theDateTime.ToString("dd-MM-yyyy HH:mm:ss:fff", _cultureInfo);
- }
- else
- {
- r[columnMapping.DestinationColumn.Name] = DateTime.MinValue.ToString("dd-MM-yyyy HH:mm:ss:fff", CultureInfo.InvariantCulture);
- }
+ r[columnMapping.DestinationColumn.Name] = columnMapping.GetScriptValue();
+ }
+ }
+ else if (row[columnMapping.SourceColumn.Name] == DBNull.Value)
+ {
+ r[columnMapping.DestinationColumn.Name] = "NULL";
+ }
+ else if (columnMapping.SourceColumn.Type == typeof(DateTime))
+ {
+ if (DateTime.TryParse(columnMapping.ConvertInputValueToOutputValue(row[columnMapping.SourceColumn.Name])?.ToString(), out var theDateTime))
+ {
+ r[columnMapping.DestinationColumn.Name] = theDateTime.ToString("dd-MM-yyyy HH:mm:ss:fff", _cultureInfo);
}
else
{
- r[columnMapping.DestinationColumn.Name] = string.Format(_cultureInfo, "{0}", columnMapping.ConvertInputValueToOutputValue(row[columnMapping.SourceColumn.Name]));
+ r[columnMapping.DestinationColumn.Name] = DateTime.MinValue.ToString("dd-MM-yyyy HH:mm:ss:fff", CultureInfo.InvariantCulture);
}
}
- tableForExcel.Rows.Add(r);
+ else
+ {
+ r[columnMapping.DestinationColumn.Name] = string.Format(_cultureInfo, "{0}", columnMapping.ConvertInputValueToOutputValue(row[columnMapping.SourceColumn.Name]));
+ }
}
+ tableForExcel.Rows.Add(r);
+ }
- private DataTable GetTableForExcel()
+ private DataTable GetTableForExcel()
+ {
+ var table = new DataTable(currentMapping.DestinationTable.Name);
+ foreach (ColumnMapping c in _currentColumnMappings)
{
- var table = new DataTable(currentMapping.DestinationTable.Name);
- foreach (ColumnMapping c in _currentColumnMappings)
+ if (c.Active)
{
- if (c.Active)
- {
- table.Columns.Add(c.DestinationColumn.Name);
- }
+ table.Columns.Add(c.DestinationColumn.Name);
}
- return table;
}
+ return table;
+ }
- public void AddTableToSet()
+ public void AddTableToSet()
+ {
+ if (setForExcel == null)
{
- if (setForExcel == null)
- {
- setForExcel = new DataSet();
- }
- if (tableForExcel == null)
- {
- tableForExcel = GetTableForExcel();
- }
- setForExcel.Tables.Add(tableForExcel);
- tableForExcel = null;
+ setForExcel = new DataSet();
+ }
+ if (tableForExcel == null)
+ {
+ tableForExcel = GetTableForExcel();
}
+ setForExcel.Tables.Add(tableForExcel);
+ tableForExcel = null;
+ }
- public void GenerateExcel()
+ public void GenerateExcel()
+ {
+ ExcelPackage.LicenseContext = LicenseContext.Commercial;
+ FileInfo newFileInfo = new FileInfo(_path.CombinePaths(_destinationPath));
+ ExcelPackage pck = null;
+ if (newFileInfo.Exists)
{
- ExcelPackage.LicenseContext = LicenseContext.Commercial;
- FileInfo newFileInfo = new FileInfo(_path.CombinePaths(_destinationPath));
- ExcelPackage pck = null;
- if (newFileInfo.Exists)
+ try
{
- try
- {
- pck = new ExcelPackage(newFileInfo);
- }
- catch (Exception ex)
- {
- _logger.Log($"Can not write to the existing destination file: {ex.Message}. The file will be overwritten.");
- File.Delete(newFileInfo.FullName);
- pck = new ExcelPackage(newFileInfo);
- }
+ pck = new ExcelPackage(newFileInfo);
}
- else
+ catch (Exception ex)
{
+ _logger.Log($"Can not write to the existing destination file: {ex.Message}. The file will be overwritten.");
+ File.Delete(newFileInfo.FullName);
pck = new ExcelPackage(newFileInfo);
}
- using (pck)
+ }
+ else
+ {
+ pck = new ExcelPackage(newFileInfo);
+ }
+ using (pck)
+ {
+ foreach (DataTable table in setForExcel.Tables)
{
- foreach (DataTable table in setForExcel.Tables)
+ List workSheetsToRemove = new List();
+ foreach (var worksheet in pck.Workbook.Worksheets)
{
- List workSheetsToRemove = new List();
- foreach (var worksheet in pck.Workbook.Worksheets)
- {
- if (worksheet.Name.Equals(table.TableName, StringComparison.OrdinalIgnoreCase))
- {
- workSheetsToRemove.Add(worksheet);
- }
- }
- foreach (var worksheet in workSheetsToRemove)
+ if (worksheet.Name.Equals(table.TableName, StringComparison.OrdinalIgnoreCase))
{
- pck.Workbook.Worksheets.Delete(worksheet);
- }
- ExcelWorksheet ws = pck.Workbook.Worksheets.Add(table.TableName);
- ws.Cells["A1"].LoadFromDataTable(table, true);
- if (_logger != null)
- {
- _logger.Log("Added table: " + table.TableName + " Rows: " + table.Rows.Count);
+ workSheetsToRemove.Add(worksheet);
}
}
- pck.Save();
+ foreach (var worksheet in workSheetsToRemove)
+ {
+ pck.Workbook.Worksheets.Delete(worksheet);
+ }
+ ExcelWorksheet ws = pck.Workbook.Worksheets.Add(table.TableName);
+ ws.Cells["A1"].LoadFromDataTable(table, true);
if (_logger != null)
{
- _logger.Log("Writing to " + _destinationPath + " is saved and finished");
+ _logger.Log("Added table: " + table.TableName + " Rows: " + table.Rows.Count);
}
}
+ pck.Save();
+ if (_logger != null)
+ {
+ _logger.Log("Writing to " + _destinationPath + " is saved and finished");
+ }
}
+ }
- public virtual void Close()
- {
- }
+ public virtual void Close()
+ {
+ }
- #region IDisposable Implementation
+ #region IDisposable Implementation
- protected bool Disposed;
+ protected bool Disposed;
- protected virtual void Dispose(bool disposing)
+ protected virtual void Dispose(bool disposing)
+ {
+ lock (this)
{
- lock (this)
- {
- // Do nothing if the object has already been disposed of.
- if (Disposed)
- return;
+ // Do nothing if the object has already been disposed of.
+ if (Disposed)
+ return;
- if (disposing)
- {
- // Release diposable objects used by this instance here.
- }
-
- // Release unmanaged resources here. Don't access reference type fields.
-
- // Remember that the object has been disposed of.
- Disposed = true;
+ if (disposing)
+ {
+ // Release diposable objects used by this instance here.
}
- }
- public virtual void Dispose()
- {
- Dispose(true);
- // Unregister object for finalization.
- GC.SuppressFinalize(this);
+ // Release unmanaged resources here. Don't access reference type fields.
+
+ // Remember that the object has been disposed of.
+ Disposed = true;
}
+ }
- #endregion
+ public virtual void Dispose()
+ {
+ Dispose(true);
+ // Unregister object for finalization.
+ GC.SuppressFinalize(this);
}
-}
+
+ #endregion
+}
\ No newline at end of file
diff --git a/src/ExcelProvider.cs b/src/ExcelProvider.cs
index 2c7243c..62eb88f 100644
--- a/src/ExcelProvider.cs
+++ b/src/ExcelProvider.cs
@@ -1,12 +1,4 @@
-using Dynamicweb.Core;
-using Dynamicweb.Core.Helpers;
-using Dynamicweb.DataIntegration.Integration;
-using Dynamicweb.DataIntegration.Integration.Interfaces;
-using Dynamicweb.Extensibility.AddIns;
-using Dynamicweb.Extensibility.Editors;
-using Dynamicweb.Logging;
-using OfficeOpenXml;
-using System;
+using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
@@ -14,496 +6,503 @@
using System.Linq;
using System.Xml;
using System.Xml.Linq;
+using Dynamicweb.Core;
+using Dynamicweb.Core.Helpers;
+using Dynamicweb.DataIntegration.Integration;
+using Dynamicweb.DataIntegration.Integration.Interfaces;
+using Dynamicweb.Extensibility.AddIns;
+using Dynamicweb.Extensibility.Editors;
+using Dynamicweb.Logging;
+using OfficeOpenXml;
+
+namespace Dynamicweb.DataIntegration.Providers.ExcelProvider;
-namespace Dynamicweb.DataIntegration.Providers.ExcelProvider
+[AddInName("Dynamicweb.DataIntegration.Providers.Provider"), AddInLabel("Excel Provider"), AddInDescription("Excel Provider"), AddInIgnore(false)]
+public class ExcelProvider : BaseProvider, ISource, IDestination, IParameterOptions
{
- [AddInName("Dynamicweb.DataIntegration.Providers.Provider"), AddInLabel("Excel Provider"), AddInDescription("Excel Provider"), AddInIgnore(false)]
- public class ExcelProvider : BaseProvider, ISource, IDestination, IParameterOptions
- {
- private const string ExcelExtension = ".xlsx";
- private const string ExcelFilesSearchPattern = "*.xls*";
+ private const string ExcelExtension = ".xlsx";
+ private const string ExcelFilesSearchPattern = "*.xls*";
- [AddInParameter("Source folder"), AddInParameterEditor(typeof(FolderSelectEditor), "folder=/Files/;"), AddInParameterGroup("Source")]
- public string SourceFolder { get; set; }
+ [AddInParameter("Source folder"), AddInParameterEditor(typeof(FolderSelectEditor), "folder=/Files/;"), AddInParameterGroup("Source")]
+ public string SourceFolder { get; set; }
- [AddInParameter("Source file"), AddInParameterEditor(typeof(FileManagerEditor), "folder=/Files/;Tooltip=Selecting a source file will override source folder selection"), AddInParameterGroup("Source")]
- public string SourceFile { get; set; }
+ [AddInParameter("Source file"), AddInParameterEditor(typeof(FileManagerEditor), "folder=/Files/;Tooltip=Selecting a source file will override source folder selection"), AddInParameterGroup("Source")]
+ public string SourceFile { get; set; }
- [AddInParameter("Destination file"), AddInParameterEditor(typeof(TextParameterEditor), $"append={ExcelExtension};required"), AddInParameterGroup("Destination")]
- public string DestinationFile
+ [AddInParameter("Destination file"), AddInParameterEditor(typeof(TextParameterEditor), $"append={ExcelExtension};required"), AddInParameterGroup("Destination")]
+ public string DestinationFile
+ {
+ get
{
- get
- {
- return Path.GetFileNameWithoutExtension(_destinationFileName);
- }
- set
- {
- _destinationFileName = Path.GetFileNameWithoutExtension(value);
- }
+ return Path.GetFileNameWithoutExtension(_destinationFileName);
+ }
+ set
+ {
+ _destinationFileName = Path.GetFileNameWithoutExtension(value);
}
+ }
- private string _destinationFileName;
+ private string _destinationFileName;
- [AddInParameter("Destination folder"), AddInParameterEditor(typeof(FolderSelectEditor), "folder=/Files/"), AddInParameterGroup("Destination")]
- public string DestinationFolder { get; set; } = "/Files";
+ [AddInParameter("Destination folder"), AddInParameterEditor(typeof(FolderSelectEditor), "folder=/Files/"), AddInParameterGroup("Destination")]
+ public string DestinationFolder { get; set; } = "/Files";
- private Schema schema;
+ private Schema schema;
- private ExcelDestinationWriter destinationWriter;
+ private ExcelDestinationWriter destinationWriter;
- public override Schema GetOriginalDestinationSchema()
- {
- schema ??= GetOriginalSourceSchema();
- return schema;
- }
+ public override Schema GetOriginalDestinationSchema()
+ {
+ schema ??= GetOriginalSourceSchema();
+ return schema;
+ }
- public override bool SchemaIsEditable => true;
+ public override bool SchemaIsEditable => true;
- private bool IsFolderUsed => string.IsNullOrEmpty(SourceFile);
+ private bool IsFolderUsed => string.IsNullOrEmpty(SourceFile);
- public override Schema GetOriginalSourceSchema()
- {
- Schema result = new Schema();
+ public override Schema GetOriginalSourceSchema()
+ {
+ Schema result = new Schema();
- if (!IsFolderUsed)
+ if (!IsFolderUsed)
+ {
+ var sourceFilePath = GetSourceFilePath(SourceFile);
+ if (File.Exists(sourceFilePath))
{
- var sourceFilePath = GetSourceFilePath(SourceFile);
- if (File.Exists(sourceFilePath))
+ try
{
- try
+ if (SourceFile.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) ||
+ SourceFile.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||
+ SourceFile.EndsWith(".xlsm", StringComparison.OrdinalIgnoreCase))
{
- if (SourceFile.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) ||
- SourceFile.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||
- SourceFile.EndsWith(".xlsm", StringComparison.OrdinalIgnoreCase))
- {
- Dictionary excelReaders = new Dictionary
+ Dictionary excelReaders = new Dictionary
{
{ sourceFilePath, new ExcelReader(sourceFilePath) }
};
- GetSchemaForTableFromFile(result, excelReaders);
- }
- else
- {
- Logger?.Error("File is not an Excel file");
- }
+ GetSchemaForTableFromFile(result, excelReaders);
}
- catch (Exception ex)
+ else
{
- Logger?.Error(string.Format("GetOriginalSourceSchema error reading file: {0} message: {1} stack: {2}", sourceFilePath, ex.Message, ex.StackTrace));
+ Logger?.Error("File is not an Excel file");
}
}
- else
+ catch (Exception ex)
{
- Logger?.Error($"Source file {sourceFilePath} does not exist");
+ Logger?.Error(string.Format("GetOriginalSourceSchema error reading file: {0} message: {1} stack: {2}", sourceFilePath, ex.Message, ex.StackTrace));
}
}
else
{
- foreach (var sourceFilePath in GetSourceFolderFiles())
- {
- try
- {
- Dictionary excelReaders = new Dictionary
- {
- { sourceFilePath, new ExcelReader(sourceFilePath) }
- };
- GetSchemaForTableFromFile(result, excelReaders, true);
- }
- catch (Exception ex)
- {
- Logger?.Error(string.Format("GetOriginalSourceSchema error reading file: {0} message: {1} stack: {2}", sourceFilePath, ex.Message, ex.StackTrace));
- }
- }
- }
-
- return result;
- }
-
- private string workingDirectory = SystemInformation.MapPath("/Files/");
- public override string WorkingDirectory
- {
- get
- {
- return workingDirectory;
+ Logger?.Error($"Source file {sourceFilePath} does not exist");
}
- set { workingDirectory = value.Replace("\\", "/"); }
}
-
- private string GetSourceFilePath(string filePath)
+ else
{
- string srcFilePath = string.Empty;
-
- if (!string.IsNullOrEmpty(filePath))
+ foreach (var sourceFilePath in GetSourceFolderFiles())
{
- if (filePath.StartsWith(".."))
+ try
{
- srcFilePath = WorkingDirectory.CombinePaths(filePath.TrimStart(new char[] { '.' })).Replace("\\", "/");
+ Dictionary excelReaders = new Dictionary
+ {
+ { sourceFilePath, new ExcelReader(sourceFilePath) }
+ };
+ GetSchemaForTableFromFile(result, excelReaders, true);
}
- else
+ catch (Exception ex)
{
- srcFilePath = SystemInformation.MapPath(FilePathHelper.GetRelativePath(filePath, "/Files"));
+ Logger?.Error(string.Format("GetOriginalSourceSchema error reading file: {0} message: {1} stack: {2}", sourceFilePath, ex.Message, ex.StackTrace));
}
}
- return srcFilePath;
}
- private string SourceFolderPath => SystemInformation.MapPath(FilePathHelper.GetRelativePath(SourceFolder, "/Files"));
+ return result;
+ }
+
+ private string workingDirectory = SystemInformation.MapPath("/Files/");
+ public override string WorkingDirectory
+ {
+ get
+ {
+ return workingDirectory;
+ }
+ set { workingDirectory = value.Replace("\\", "/"); }
+ }
+
+ private string GetSourceFilePath(string filePath)
+ {
+ string srcFilePath = string.Empty;
- private IEnumerable GetSourceFolderFiles()
+ if (!string.IsNullOrEmpty(filePath))
{
- var folderPath = SourceFolderPath;
- if (Directory.Exists(folderPath))
+ if (filePath.StartsWith(".."))
{
- return Directory.EnumerateFiles(folderPath, ExcelFilesSearchPattern, SearchOption.TopDirectoryOnly);
+ srcFilePath = WorkingDirectory.CombinePaths(filePath.TrimStart(new char[] { '.' })).Replace("\\", "/");
+ }
+ else
+ {
+ srcFilePath = SystemInformation.MapPath(FilePathHelper.GetRelativePath(filePath, "/Files"));
}
- return Enumerable.Empty();
}
+ return srcFilePath;
+ }
- public override void UpdateSourceSettings(ISource source)
+ private string SourceFolderPath => SystemInformation.MapPath(FilePathHelper.GetRelativePath(SourceFolder, "/Files"));
+
+ private IEnumerable GetSourceFolderFiles()
+ {
+ var folderPath = SourceFolderPath;
+ if (Directory.Exists(folderPath))
{
- ExcelProvider newProvider = (ExcelProvider)source;
- SourceFile = newProvider.SourceFile;
- SourceFolder = newProvider.SourceFolder;
+ return Directory.EnumerateFiles(folderPath, ExcelFilesSearchPattern, SearchOption.TopDirectoryOnly);
}
+ return Enumerable.Empty();
+ }
- public override string Serialize()
- {
- XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", string.Empty));
+ public override void UpdateSourceSettings(ISource source)
+ {
+ ExcelProvider newProvider = (ExcelProvider)source;
+ SourceFile = newProvider.SourceFile;
+ SourceFolder = newProvider.SourceFolder;
+ }
- XElement root = new XElement("Parameters");
- document.Add(root);
+ public override string Serialize()
+ {
+ XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", string.Empty));
- root.Add(CreateParameterNode(GetType(), "Source file", SourceFile));
- root.Add(CreateParameterNode(GetType(), "Source folder", SourceFolder));
- root.Add(CreateParameterNode(GetType(), "Destination file", DestinationFile));
- root.Add(CreateParameterNode(GetType(), "Destination folder", DestinationFolder));
+ XElement root = new XElement("Parameters");
+ document.Add(root);
- return document.ToString();
- }
+ root.Add(CreateParameterNode(GetType(), "Source file", SourceFile));
+ root.Add(CreateParameterNode(GetType(), "Source folder", SourceFolder));
+ root.Add(CreateParameterNode(GetType(), "Destination file", DestinationFile));
+ root.Add(CreateParameterNode(GetType(), "Destination folder", DestinationFolder));
- void ISource.SaveAsXml(XmlTextWriter xmlTextWriter)
- {
- xmlTextWriter.WriteElementString("SourcePath", SourceFile);
- xmlTextWriter.WriteElementString("SourceFolder", SourceFolder);
- (this as ISource).GetSchema().SaveAsXml(xmlTextWriter);
- }
+ return document.ToString();
+ }
- void IDestination.SaveAsXml(XmlTextWriter xmlTextWriter)
- {
- xmlTextWriter.WriteElementString("DestinationFile", DestinationFile);
- xmlTextWriter.WriteElementString("DestinationFolder", DestinationFolder);
- (this as IDestination).GetSchema().SaveAsXml(xmlTextWriter);
- }
+ void ISource.SaveAsXml(XmlTextWriter xmlTextWriter)
+ {
+ xmlTextWriter.WriteElementString("SourcePath", SourceFile);
+ xmlTextWriter.WriteElementString("SourceFolder", SourceFolder);
+ (this as ISource).GetSchema().SaveAsXml(xmlTextWriter);
+ }
+
+ void IDestination.SaveAsXml(XmlTextWriter xmlTextWriter)
+ {
+ xmlTextWriter.WriteElementString("DestinationFile", DestinationFile);
+ xmlTextWriter.WriteElementString("DestinationFolder", DestinationFolder);
+ (this as IDestination).GetSchema().SaveAsXml(xmlTextWriter);
+ }
- public new ISourceReader GetReader(Mapping mapping)
+ public new ISourceReader GetReader(Mapping mapping)
+ {
+ string filePath;
+ if (!IsFolderUsed)
{
- string filePath;
- if (!IsFolderUsed)
- {
- filePath = SourceFile;
+ filePath = SourceFile;
- if (filePath.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||
+ if (filePath.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||
filePath.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) ||
filePath.EndsWith(".xlsm", StringComparison.OrdinalIgnoreCase))
+ {
+ if (!string.IsNullOrEmpty(WorkingDirectory))
{
- if (!string.IsNullOrEmpty(WorkingDirectory))
- {
- var sourceFilePath = GetSourceFilePath(filePath);
- if (!File.Exists(sourceFilePath))
- throw new Exception($"Source file {SourceFile} does not exist - Working Directory {WorkingDirectory}");
-
- return new ExcelSourceReader(sourceFilePath, mapping, this);
- }
- else
- {
- if (!File.Exists(filePath))
- throw new Exception($"Source file {filePath} does not exist - Working Directory {WorkingDirectory}");
+ var sourceFilePath = GetSourceFilePath(filePath);
+ if (!File.Exists(sourceFilePath))
+ throw new Exception($"Source file {SourceFile} does not exist - Working Directory {WorkingDirectory}");
- return new ExcelSourceReader(filePath, mapping, this);
- }
+ return new ExcelSourceReader(sourceFilePath, mapping, this);
}
else
- throw new Exception("The file is not a Excel file");
+ {
+ if (!File.Exists(filePath))
+ throw new Exception($"Source file {filePath} does not exist - Working Directory {WorkingDirectory}");
+
+ return new ExcelSourceReader(filePath, mapping, this);
+ }
}
else
- {
- string folderPath = SourceFolderPath;
- var fileName = mapping.SourceTable.SqlSchema;
- filePath = Directory.EnumerateFiles(folderPath, ExcelFilesSearchPattern, SearchOption.TopDirectoryOnly).FirstOrDefault(f => f.EndsWith(fileName, StringComparison.OrdinalIgnoreCase));
- if (!File.Exists(filePath))
- {
- throw new Exception($"Source file {fileName} does not exist in the Directory {folderPath}");
- }
- return new ExcelSourceReader(filePath, mapping, this);
+ throw new Exception("The file is not a Excel file");
+ }
+ else
+ {
+ string folderPath = SourceFolderPath;
+ var fileName = mapping.SourceTable.SqlSchema;
+ filePath = Directory.EnumerateFiles(folderPath, ExcelFilesSearchPattern, SearchOption.TopDirectoryOnly).FirstOrDefault(f => f.EndsWith(fileName, StringComparison.OrdinalIgnoreCase));
+ if (!File.Exists(filePath))
+ {
+ throw new Exception($"Source file {fileName} does not exist in the Directory {folderPath}");
}
+ return new ExcelSourceReader(filePath, mapping, this);
}
+ }
- public override void Close()
- {
+ public override void Close()
+ {
- }
+ }
- public override void UpdateDestinationSettings(IDestination destination)
- {
- ExcelProvider newProvider = (ExcelProvider)destination;
- newProvider.DestinationFile = DestinationFile;
- newProvider.DestinationFolder = DestinationFolder;
- }
+ public override void UpdateDestinationSettings(IDestination destination)
+ {
+ ExcelProvider newProvider = (ExcelProvider)destination;
+ newProvider.DestinationFile = DestinationFile;
+ newProvider.DestinationFolder = DestinationFolder;
+ }
- public override bool RunJob(Job job)
+ public override bool RunJob(Job job)
+ {
+ ReplaceMappingConditionalsWithValuesFromRequest(job);
+ Dictionary sourceRow = null;
+ try
{
- ReplaceMappingConditionalsWithValuesFromRequest(job);
- Dictionary sourceRow = null;
- try
- {
- CultureInfo ci = GetCultureInfo(job.Culture);
+ CultureInfo ci = GetCultureInfo(job.Culture);
- if (destinationWriter == null)
+ if (destinationWriter == null)
+ {
+ if (!string.IsNullOrEmpty(WorkingDirectory))
{
- if (!string.IsNullOrEmpty(WorkingDirectory))
- {
- destinationWriter = new ExcelDestinationWriter(workingDirectory.CombinePaths(DestinationFolder), $"{Path.GetFileNameWithoutExtension(DestinationFile)}{ExcelExtension}", job.Mappings, Logger, ci);
- }
- else
- {
- destinationWriter = new ExcelDestinationWriter($"{Path.GetFileNameWithoutExtension(SourceFile)}{ExcelExtension}", "", job.Mappings, Logger, ci);
- }
+ destinationWriter = new ExcelDestinationWriter(workingDirectory.CombinePaths(DestinationFolder), $"{Path.GetFileNameWithoutExtension(DestinationFile)}{ExcelExtension}", job.Mappings, Logger, ci);
+ }
+ else
+ {
+ destinationWriter = new ExcelDestinationWriter($"{Path.GetFileNameWithoutExtension(SourceFile)}{ExcelExtension}", "", job.Mappings, Logger, ci);
}
- foreach (var mapping in destinationWriter.Mappings)
+ }
+ foreach (var mapping in destinationWriter.Mappings)
+ {
+ destinationWriter.currentMapping = mapping;
+ using (ISourceReader sourceReader = mapping.Source.GetReader(mapping))
{
- destinationWriter.currentMapping = mapping;
- using (ISourceReader sourceReader = mapping.Source.GetReader(mapping))
+ while (!sourceReader.IsDone())
{
- while (!sourceReader.IsDone())
+ sourceRow = sourceReader.GetNext();
+ if (ProcessInputRow(sourceRow, mapping))
{
- sourceRow = sourceReader.GetNext();
- if (ProcessInputRow(sourceRow, mapping))
- {
- destinationWriter.Write(sourceRow);
- }
+ destinationWriter.Write(sourceRow);
}
- destinationWriter.AddTableToSet();
}
+ destinationWriter.AddTableToSet();
}
- destinationWriter.GenerateExcel();
- sourceRow = null;
}
- catch (Exception ex)
- {
- string msg = ex.Message;
- string stackTrace = ex.StackTrace;
+ destinationWriter.GenerateExcel();
+ sourceRow = null;
+ }
+ catch (Exception ex)
+ {
+ string msg = ex.Message;
+ string stackTrace = ex.StackTrace;
- Logger?.Error($"Error: {msg.Replace(System.Environment.NewLine, " ")} Stack: {stackTrace.Replace(System.Environment.NewLine, " ")}", ex);
- LogManager.System.GetLogger(LogCategory.Application, "Dataintegration").Error($"{GetType().Name} error: {msg} Stack: {stackTrace}", ex);
- if (sourceRow != null)
- {
- msg += GetFailedSourceRowMessage(sourceRow);
- }
- Logger.Log("Job failed " + msg);
- return false;
- }
- finally
+ Logger?.Error($"Error: {msg.Replace(System.Environment.NewLine, " ")} Stack: {stackTrace.Replace(System.Environment.NewLine, " ")}", ex);
+ LogManager.System.GetLogger(LogCategory.Application, "Dataintegration").Error($"{GetType().Name} error: {msg} Stack: {stackTrace}", ex);
+ if (sourceRow != null)
{
- sourceRow = null;
+ msg += GetFailedSourceRowMessage(sourceRow);
}
- return true;
+ Logger.Log("Job failed " + msg);
+ return false;
+ }
+ finally
+ {
+ sourceRow = null;
}
+ return true;
+ }
- private CultureInfo GetCultureInfo(string culture)
+ private CultureInfo GetCultureInfo(string culture)
+ {
+ try
{
- try
- {
- return string.IsNullOrWhiteSpace(culture) ? CultureInfo.CurrentCulture : CultureInfo.GetCultureInfo(culture);
- }
- catch (CultureNotFoundException ex)
- {
- Logger?.Log(string.Format("Error getting culture: {0}. Using {1} instead", ex.Message, CultureInfo.CurrentCulture.Name));
- }
- return CultureInfo.CurrentCulture;
+ return string.IsNullOrWhiteSpace(culture) ? CultureInfo.CurrentCulture : CultureInfo.GetCultureInfo(culture);
}
+ catch (CultureNotFoundException ex)
+ {
+ Logger?.Log(string.Format("Error getting culture: {0}. Using {1} instead", ex.Message, CultureInfo.CurrentCulture.Name));
+ }
+ return CultureInfo.CurrentCulture;
+ }
- private void GetSchemaForTableFromFile(Schema schema, Dictionary excelReaders, bool isFolderUsed = false)
+ private void GetSchemaForTableFromFile(Schema schema, Dictionary excelReaders, bool isFolderUsed = false)
+ {
+ foreach (var reader in excelReaders)
{
- foreach (var reader in excelReaders)
+ foreach (DataTable dt in reader.Value.ExcelSet.Tables)
{
- foreach (DataTable dt in reader.Value.ExcelSet.Tables)
+ Table excelTable = schema.AddTable(dt.TableName);
+ if (isFolderUsed)
{
- Table excelTable = schema.AddTable(dt.TableName);
- if (isFolderUsed)
+ excelTable.SqlSchema = Path.GetFileName(reader.Key);
+ }
+ try
+ {
+ int columnCount;
+ try
{
- excelTable.SqlSchema = Path.GetFileName(reader.Key);
+ columnCount = dt.Columns.Count;
}
- try
+ catch (System.ArgumentException)
{
- int columnCount;
- try
- {
- columnCount = dt.Columns.Count;
- }
- catch (System.ArgumentException)
- {
- columnCount = dt.Columns.Count;
- }
- foreach (System.Data.DataColumn c in dt.Columns)
- {
- 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);
- }
-
+ columnCount = dt.Columns.Count;
}
- catch (System.ArgumentException ae)
+ foreach (System.Data.DataColumn c in dt.Columns)
{
- string msg = string.Format("Error reading Excel file: {0} ", reader.Key);
- Exception ex = new Exception(msg, ae);
- throw ex;
+ 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);
}
- }
+ }
+ catch (System.ArgumentException ae)
+ {
+ string msg = string.Format("Error reading Excel file: {0} ", reader.Key);
+ Exception ex = new Exception(msg, ae);
+ throw ex;
+ }
}
- }
- Schema IDestination.GetSchema()
- {
- schema ??= GetOriginalSourceSchema();
- return schema;
}
+ }
- Schema ISource.GetSchema()
- {
- schema ??= GetOriginalSourceSchema();
- return schema;
- }
+ Schema IDestination.GetSchema()
+ {
+ schema ??= GetOriginalSourceSchema();
+ return schema;
+ }
+
+ Schema ISource.GetSchema()
+ {
+ schema ??= GetOriginalSourceSchema();
+ return schema;
+ }
- public ExcelProvider()
+ public ExcelProvider()
+ {
+ if (string.IsNullOrEmpty(FilesFolderName))
{
- if (string.IsNullOrEmpty(FilesFolderName))
- {
- FilesFolderName = "Files";
- }
+ FilesFolderName = "Files";
}
+ }
- public ExcelProvider(XmlNode xmlNode)
+ public ExcelProvider(XmlNode xmlNode)
+ {
+ foreach (XmlNode node in xmlNode.ChildNodes)
{
- foreach (XmlNode node in xmlNode.ChildNodes)
+ switch (node.Name)
{
- switch (node.Name)
- {
- case "Schema":
- schema = new Schema(node);
- break;
- case "SourcePath":
- if (node.HasChildNodes)
- {
- SourceFile = node.FirstChild.Value;
- }
- break;
- case "SourceFolder":
- if (node.HasChildNodes)
- {
- SourceFolder = node.FirstChild.Value;
- }
- break;
- case "DestinationFile":
- if (node.HasChildNodes)
- {
- DestinationFile = node.FirstChild.Value;
- }
- break;
- case "DestinationFolder":
- if (node.HasChildNodes)
- {
- DestinationFolder = node.FirstChild.Value;
- }
- break;
+ case "Schema":
+ schema = new Schema(node);
+ break;
+ case "SourcePath":
+ if (node.HasChildNodes)
+ {
+ SourceFile = node.FirstChild.Value;
+ }
+ break;
+ case "SourceFolder":
+ if (node.HasChildNodes)
+ {
+ SourceFolder = node.FirstChild.Value;
+ }
+ break;
+ case "DestinationFile":
+ if (node.HasChildNodes)
+ {
+ DestinationFile = node.FirstChild.Value;
+ }
+ break;
+ case "DestinationFolder":
+ if (node.HasChildNodes)
+ {
+ DestinationFolder = node.FirstChild.Value;
+ }
+ break;
- }
}
}
+ }
- internal ExcelProvider(Dictionary excelReaders, Schema schema, ExcelDestinationWriter writer)
- {
- this.schema = schema;
- destinationWriter = writer;
- }
+ internal ExcelProvider(Dictionary excelReaders, Schema schema, ExcelDestinationWriter writer)
+ {
+ this.schema = schema;
+ destinationWriter = writer;
+ }
- public ExcelProvider(string path)
- {
- SourceFile = path;
- }
+ public ExcelProvider(string path)
+ {
+ SourceFile = path;
+ }
- public override void OverwriteSourceSchemaToOriginal()
- {
- schema = GetOriginalSourceSchema();
- }
+ public override void OverwriteSourceSchemaToOriginal()
+ {
+ schema = GetOriginalSourceSchema();
+ }
- public override void OverwriteDestinationSchemaToOriginal()
+ public override void OverwriteDestinationSchemaToOriginal()
+ {
+ }
+
+ public override string ValidateDestinationSettings()
+ {
+ string extension = Path.GetFileNameWithoutExtension(DestinationFile);
+ if (!string.Equals(extension, DestinationFile, StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(extension) && !(extension.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) || extension.EndsWith(".xls", StringComparison.OrdinalIgnoreCase)))
{
+ return "File has to end with .xlsx or .xls";
}
+ return "";
+ }
- public override string ValidateDestinationSettings()
+ public override string ValidateSourceSettings()
+ {
+ if (string.IsNullOrEmpty(SourceFile) && string.IsNullOrEmpty(SourceFolder))
{
- string extension = Path.GetFileNameWithoutExtension(DestinationFile);
- if (!string.Equals(extension, DestinationFile, StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(extension) && !(extension.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) || extension.EndsWith(".xls", StringComparison.OrdinalIgnoreCase)))
- {
- return "File has to end with .xlsx or .xls";
- }
- return "";
+ return "No Source file neither folder are selected";
}
-
- public override string ValidateSourceSettings()
+ if (IsFolderUsed)
{
- if (string.IsNullOrEmpty(SourceFile) && string.IsNullOrEmpty(SourceFolder))
+ string srcFolderPath = SourceFolderPath;
+
+ if (!Directory.Exists(srcFolderPath))
{
- return "No Source file neither folder are selected";
+ return "Source folder \"" + SourceFolder + "\" does not exist";
}
- if (IsFolderUsed)
+ else
{
- string srcFolderPath = SourceFolderPath;
+ var files = GetSourceFolderFiles();
- if (!Directory.Exists(srcFolderPath))
+ if (files.Count() == 0)
{
- return "Source folder \"" + SourceFolder + "\" does not exist";
- }
- else
- {
- var files = GetSourceFolderFiles();
-
- if (files.Count() == 0)
- {
- return "There are no Excel files with the extensions: [*.xlsx, *.xls, *.xlsm] in the source folder ";
- }
+ return "There are no Excel files with the extensions: [*.xlsx, *.xls, *.xlsm] in the source folder ";
}
}
- else
+ }
+ else
+ {
+ ExcelPackage.LicenseContext = LicenseContext.Commercial;
+ if (SourceFile.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||
+ SourceFile.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) ||
+ SourceFile.EndsWith(".xlsm", StringComparison.OrdinalIgnoreCase))
{
- ExcelPackage.LicenseContext = LicenseContext.Commercial;
- if (SourceFile.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||
- SourceFile.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) ||
- SourceFile.EndsWith(".xlsm", StringComparison.OrdinalIgnoreCase))
- {
- string filename = GetSourceFilePath(SourceFile);
- if (!File.Exists(filename))
- {
- return $"Excel file \"{SourceFile}\" does not exist. WorkingDirectory - {WorkingDirectory}";
- }
- }
- else
+ string filename = GetSourceFilePath(SourceFile);
+ if (!File.Exists(filename))
{
- return "The file is not an Excel file";
- }
+ return $"Excel file \"{SourceFile}\" does not exist. WorkingDirectory - {WorkingDirectory}";
+ }
}
- if (!string.IsNullOrEmpty(SourceFile) && !string.IsNullOrEmpty(SourceFolder))
+ else
{
- return "Warning: In your Excel Provider source, you selected both a source file and a source folder. The source folder selection will be ignored, and only the source file will be used.";
+ return "The file is not an Excel file";
}
- return null;
}
-
- public IEnumerable GetParameterOptions(string parameterName) => new List();
+ if (!string.IsNullOrEmpty(SourceFile) && !string.IsNullOrEmpty(SourceFolder))
+ {
+ return "Warning: In your Excel Provider source, you selected both a source file and a source folder. The source folder selection will be ignored, and only the source file will be used.";
+ }
+ return null;
}
-}
+
+ public IEnumerable GetParameterOptions(string parameterName) => new List();
+}
\ No newline at end of file
diff --git a/src/ExcelReader.cs b/src/ExcelReader.cs
index eb9d078..efd050e 100644
--- a/src/ExcelReader.cs
+++ b/src/ExcelReader.cs
@@ -1,118 +1,117 @@
-using OfficeOpenXml;
-using System;
+using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
+using OfficeOpenXml;
-namespace Dynamicweb.DataIntegration.Providers.ExcelProvider
+namespace Dynamicweb.DataIntegration.Providers.ExcelProvider;
+
+public class ExcelReader : IDisposable
{
- public class ExcelReader : IDisposable
- {
- public string Filename { get; set; }
- public DataSet ExcelSet { get; set; }
+ public string Filename { get; set; }
+ public DataSet ExcelSet { get; set; }
- public ExcelReader(string filename)
+ public ExcelReader(string filename)
+ {
+ Filename = filename;
+ if (filename.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||
+ filename.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) ||
+ filename.EndsWith(".xlsm", StringComparison.OrdinalIgnoreCase))
{
- Filename = filename;
- if (filename.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||
- filename.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) ||
- filename.EndsWith(".xlsm", StringComparison.OrdinalIgnoreCase))
+ if (ExcelSet == null)
{
- if (ExcelSet == null)
+ if (File.Exists(filename))
{
- if (File.Exists(filename))
- {
- LoadExcelFile();
- }
+ LoadExcelFile();
}
}
- else
- {
- throw new Exception("File is not an Excel file");
- }
}
+ else
+ {
+ throw new Exception("File is not an Excel file");
+ }
+ }
- private void LoadExcelFile()
+ private void LoadExcelFile()
+ {
+ ExcelPackage.LicenseContext = LicenseContext.Commercial;
+ var fileInfo = new FileInfo(Filename);
+ using var package = new ExcelPackage(fileInfo);
+ var ds = new DataSet();
+ foreach (var worksheet in package.Workbook.Worksheets)
{
- ExcelPackage.LicenseContext = LicenseContext.Commercial;
- var fileInfo = new FileInfo(Filename);
- using var package = new ExcelPackage(fileInfo);
- var ds = new DataSet();
- foreach (var worksheet in package.Workbook.Worksheets)
- {
- var firstRowNumberWithData = GetFirstRowNumberWithData(worksheet);
- if (firstRowNumberWithData <= 0)
- continue;
+ var firstRowNumberWithData = GetFirstRowNumberWithData(worksheet);
+ if (firstRowNumberWithData <= 0)
+ continue;
- var emptyRows = new List();
- var dataTable = new DataTable(worksheet.Name);
- var firstRow = worksheet.Cells[firstRowNumberWithData, 1, firstRowNumberWithData, worksheet.Dimension.End.Column];
- for (var colNum = 1; colNum <= worksheet.Dimension.End.Column; colNum++)
+ var emptyRows = new List();
+ var dataTable = new DataTable(worksheet.Name);
+ var firstRow = worksheet.Cells[firstRowNumberWithData, 1, firstRowNumberWithData, worksheet.Dimension.End.Column];
+ for (var colNum = 1; colNum <= worksheet.Dimension.End.Column; colNum++)
+ {
+ var firstRowCell = firstRow[firstRowNumberWithData, colNum];
+ DataColumn column;
+ var header = firstRowCell.Text;
+ if (!dataTable.Columns.Contains(header) && !string.IsNullOrWhiteSpace(header))
+ {
+ column = dataTable.Columns.Add(header);
+ }
+ else
{
- var firstRowCell = firstRow[firstRowNumberWithData, colNum];
- DataColumn column;
- var header = firstRowCell.Text;
- if (!dataTable.Columns.Contains(header) && !string.IsNullOrWhiteSpace(header))
- {
- column = dataTable.Columns.Add(header);
- }
- else
- {
- column = dataTable.Columns.Add(header + colNum);
- }
+ column = dataTable.Columns.Add(header + colNum);
+ }
- if (!string.IsNullOrEmpty(firstRowCell.Comment?.Text))
- {
- column.Caption = firstRowCell.Comment.Text;
- }
+ if (!string.IsNullOrEmpty(firstRowCell.Comment?.Text))
+ {
+ column.Caption = firstRowCell.Comment.Text;
}
+ }
- for (var rowNum = firstRowNumberWithData + 1; rowNum <= worksheet.Dimension.End.Row; rowNum++)
+ for (var rowNum = firstRowNumberWithData + 1; rowNum <= worksheet.Dimension.End.Row; rowNum++)
+ {
+ var hasValue = false;
+ var wsRow = worksheet.Cells[rowNum, 1, rowNum, worksheet.Dimension.End.Column];
+ var row = dataTable.Rows.Add();
+ for(var colNum = 1; colNum <= worksheet.Dimension.End.Column; colNum++)
{
- var hasValue = false;
- var wsRow = worksheet.Cells[rowNum, 1, rowNum, worksheet.Dimension.End.Column];
- var row = dataTable.Rows.Add();
- for(var colNum = 1; colNum <= worksheet.Dimension.End.Column; colNum++)
- {
- string cellText = wsRow[rowNum, colNum].Text;
- row[colNum - 1] = cellText;
+ string cellText = wsRow[rowNum, colNum].Text;
+ row[colNum - 1] = cellText;
- if (!string.IsNullOrWhiteSpace(cellText))
- hasValue = true;
- }
-
- if (!hasValue)
- emptyRows.Add(row);
+ if (!string.IsNullOrWhiteSpace(cellText))
+ hasValue = true;
}
- foreach (var row in emptyRows)
- dataTable.Rows.Remove(row);
-
- ds.Tables.Add(dataTable);
+ if (!hasValue)
+ emptyRows.Add(row);
}
- ExcelSet = ds;
+ foreach (var row in emptyRows)
+ dataTable.Rows.Remove(row);
+
+ ds.Tables.Add(dataTable);
}
- private static int GetFirstRowNumberWithData(ExcelWorksheet worksheet)
- {
- for (var rowNum = 1; rowNum <= worksheet.Dimension?.End?.Row; rowNum++)
- {
- var wsRow = worksheet.Cells[rowNum, 1, rowNum, worksheet.Dimension.End.Column];
- for (var colNum = 1; colNum <= worksheet.Dimension.End.Column; colNum++)
- {
- string cellText = wsRow[rowNum, colNum].Text;
- if (!string.IsNullOrWhiteSpace(cellText))
- return rowNum;
- }
+ ExcelSet = ds;
+ }
+
+ private static int GetFirstRowNumberWithData(ExcelWorksheet worksheet)
+ {
+ for (var rowNum = 1; rowNum <= worksheet.Dimension?.End?.Row; rowNum++)
+ {
+ var wsRow = worksheet.Cells[rowNum, 1, rowNum, worksheet.Dimension.End.Column];
+ for (var colNum = 1; colNum <= worksheet.Dimension.End.Column; colNum++)
+ {
+ string cellText = wsRow[rowNum, colNum].Text;
+ if (!string.IsNullOrWhiteSpace(cellText))
+ return rowNum;
}
- return -1;
}
+ return -1;
+ }
- public void Dispose()
- {
- ExcelSet.Clear();
- ExcelSet.Dispose();
- }
+ public void Dispose()
+ {
+ ExcelSet.Clear();
+ ExcelSet.Dispose();
}
-}
+}
\ No newline at end of file
diff --git a/src/ExcelSourceReader.cs b/src/ExcelSourceReader.cs
index 976106e..eee52be 100644
--- a/src/ExcelSourceReader.cs
+++ b/src/ExcelSourceReader.cs
@@ -1,201 +1,200 @@
-using Dynamicweb.DataIntegration.Integration;
-using Dynamicweb.DataIntegration.Integration.Interfaces;
-using System;
+using System;
using System.Collections.Generic;
using System.Data;
+using Dynamicweb.DataIntegration.Integration;
+using Dynamicweb.DataIntegration.Integration.Interfaces;
+
+namespace Dynamicweb.DataIntegration.Providers.ExcelProvider;
-namespace Dynamicweb.DataIntegration.Providers.ExcelProvider
+///
+/// ExcelSourceReader
+///
+public class ExcelSourceReader : ISourceReader
{
+ private readonly Mapping mapping;
+ private readonly string path;
+ private ExcelReader reader;
+ private int rowsCount = 0;
+ private Dictionary nextResult;
+ private readonly ExcelProvider _provider;
+
+ private HashSet NumericTypes = new HashSet
+ {
+ typeof(decimal), typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(long), typeof(double), typeof(float)
+ };
+
///
- /// ExcelSourceReader
+ /// ColumnCount
///
- public class ExcelSourceReader : ISourceReader
+ public virtual int ColumnCount
{
- private readonly Mapping mapping;
- private readonly string path;
- private ExcelReader reader;
- private int rowsCount = 0;
- private Dictionary nextResult;
- private readonly ExcelProvider _provider;
-
- private HashSet NumericTypes = new HashSet
- {
- typeof(decimal), typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(long), typeof(double), typeof(float)
- };
-
- ///
- /// ColumnCount
- ///
- public virtual int ColumnCount
- {
- get { throw new NotImplementedException(); }
- }
+ get { throw new NotImplementedException(); }
+ }
- private ExcelReader Reader
+ private ExcelReader Reader
+ {
+ get
{
- get
+ if (reader == null)
{
- if (reader == null)
- {
- reader = new ExcelReader(path);
- }
- return reader;
+ reader = new ExcelReader(path);
}
+ return reader;
}
+ }
- internal ExcelSourceReader(ExcelReader reader, Mapping mapping)
- {
- this.reader = reader;
- this.mapping = mapping;
- VerifyDuplicateColumns();
- }
+ internal ExcelSourceReader(ExcelReader reader, Mapping mapping)
+ {
+ this.reader = reader;
+ this.mapping = mapping;
+ VerifyDuplicateColumns();
+ }
- public ExcelSourceReader(string filename, Mapping mapping, ExcelProvider provider)
- {
- path = filename;
- this.mapping = mapping;
- VerifyDuplicateColumns();
- _provider = provider;
- }
+ public ExcelSourceReader(string filename, Mapping mapping, ExcelProvider provider)
+ {
+ path = filename;
+ this.mapping = mapping;
+ VerifyDuplicateColumns();
+ _provider = provider;
+ }
- public ExcelSourceReader()
- {
- }
+ public ExcelSourceReader()
+ {
+ }
- public bool IsDone()
+ public bool IsDone()
+ {
+ if (Reader?.ExcelSet?.Tables != null && Reader.ExcelSet.Tables.Count > 0)
{
- if (Reader?.ExcelSet?.Tables != null && Reader.ExcelSet.Tables.Count > 0)
+ Dictionary result = new Dictionary();
+ DataTable dt = null;
+ foreach (DataTable table in Reader.ExcelSet.Tables)
{
- Dictionary result = new Dictionary();
- DataTable dt = null;
- foreach (DataTable table in Reader.ExcelSet.Tables)
+ if (table.TableName.Equals(mapping.SourceTable.Name))
{
- if (table.TableName.Equals(mapping.SourceTable.Name))
- {
- dt = table;
- break;
- }
+ dt = table;
+ break;
}
- if (dt.Rows.Count == rowsCount)
- {
- rowsCount = 0;
- return true;
- }
- if (dt != null)
- {
- DataRow dr = dt.Rows[rowsCount];
+ }
+ if (dt.Rows.Count == rowsCount)
+ {
+ rowsCount = 0;
+ return true;
+ }
+ if (dt != null)
+ {
+ DataRow dr = dt.Rows[rowsCount];
- foreach (Column column in mapping.GetSourceColumns(true, true))
+ foreach (Column column in mapping.GetSourceColumns(true, true))
+ {
+ if (!result.ContainsKey(column.Name) && dt.Columns.Contains(column.Name))
{
- if (!result.ContainsKey(column.Name) && dt.Columns.Contains(column.Name))
+ if (dr[column.Name] == null)
{
- if (dr[column.Name] == null)
- {
- result.Add(column.Name, DBNull.Value);
- }
- else
+ result.Add(column.Name, DBNull.Value);
+ }
+ else
+ {
+ string value = dr[column.Name].ToString();
+ if (NumericTypes.Contains(column.Type))
{
- string value = dr[column.Name].ToString();
- if (NumericTypes.Contains(column.Type))
+ if (string.IsNullOrEmpty(value))
{
- if (string.IsNullOrEmpty(value))
- {
- result.Add(column.Name, 0);
- }
- else
- {
- result.Add(column.Name, dr[column.Name]);
- }
+ result.Add(column.Name, 0);
}
else
{
- result.Add(column.Name, value);
+ result.Add(column.Name, dr[column.Name]);
}
}
+ else
+ {
+ result.Add(column.Name, value);
+ }
}
}
- rowsCount++;
}
+ rowsCount++;
+ }
- nextResult = result;
+ nextResult = result;
- if (RowMatchesConditions())
- {
- return false;
- }
-
- return IsDone();
+ if (RowMatchesConditions())
+ {
+ return false;
}
- return true;
- }
- private bool RowMatchesConditions()
- {
- return mapping.Conditionals?.CheckConditionals(nextResult) ?? true;
+ return IsDone();
}
+ return true;
+ }
- public Dictionary GetNext()
- {
- return nextResult;
- }
+ private bool RowMatchesConditions()
+ {
+ return mapping.Conditionals?.CheckConditionals(nextResult) ?? true;
+ }
- private void VerifyDuplicateColumns()
+ public Dictionary GetNext()
+ {
+ return nextResult;
+ }
+
+ private void VerifyDuplicateColumns()
+ {
+ if (Reader != null)
{
- if (Reader != null)
+ foreach (DataTable dt in Reader.ExcelSet.Tables)
{
- foreach (DataTable dt in Reader.ExcelSet.Tables)
+ List headers = new List();
+ foreach (System.Data.DataColumn c in dt.Columns)
{
- List headers = new List();
- foreach (System.Data.DataColumn c in dt.Columns)
+ if (!headers.Contains(c.ColumnName))
{
- if (!headers.Contains(c.ColumnName))
- {
- headers.Add(c.ColumnName);
- }
- else
- {
- throw new Exception(string.Format("File {0}.xlsx : repeated columns found: {1}. ",
+ headers.Add(c.ColumnName);
+ }
+ else
+ {
+ throw new Exception(string.Format("File {0}.xlsx : repeated columns found: {1}. ",
mapping.SourceTable.Name, string.Join(",", headers.ToArray())));
- }
}
}
}
}
+ }
- #region IDisposable Implementation
+ #region IDisposable Implementation
- protected bool Disposed;
+ protected bool Disposed;
- protected virtual void Dispose(bool disposing)
+ protected virtual void Dispose(bool disposing)
+ {
+ reader.Dispose();
+ lock (this)
{
- reader.Dispose();
- lock (this)
- {
- // Do nothing if the object has already been disposed of.
- if (Disposed)
- return;
-
- if (disposing)
- {
- // Release diposable objects used by this instance here.
- if (reader != null)
- reader.Dispose();
- }
-
- // Release unmanaged resources here. Don't access reference type fields.
+ // Do nothing if the object has already been disposed of.
+ if (Disposed)
+ return;
- // Remember that the object has been disposed of.
- Disposed = true;
+ if (disposing)
+ {
+ // Release diposable objects used by this instance here.
+ if (reader != null)
+ reader.Dispose();
}
- }
- public virtual void Dispose()
- {
- Dispose(true);
- // Unregister object for finalization.
- GC.SuppressFinalize(this);
+ // Release unmanaged resources here. Don't access reference type fields.
+
+ // Remember that the object has been disposed of.
+ Disposed = true;
}
+ }
- #endregion
+ public virtual void Dispose()
+ {
+ Dispose(true);
+ // Unregister object for finalization.
+ GC.SuppressFinalize(this);
}
-}
+
+ #endregion
+}
\ No newline at end of file
diff --git a/src/ExportDataToExcelProvider.cs b/src/ExportDataToExcelProvider.cs
index 7429133..240f16d 100644
--- a/src/ExportDataToExcelProvider.cs
+++ b/src/ExportDataToExcelProvider.cs
@@ -1,260 +1,259 @@
-using Dynamicweb.Core;
+using System.Collections;
+using System.Collections.Generic;
+using System.Data;
+using System.IO;
+using System.Linq;
+using Dynamicweb.Core;
using Dynamicweb.DataIntegration.Providers.ExcelProvider.PIM;
using Dynamicweb.Ecommerce.International;
using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Extensibility;
using OfficeOpenXml;
-using System.Collections;
-using System.Collections.Generic;
-using System.Data;
-using System.IO;
-using System.Linq;
-namespace Dynamicweb.DataIntegration.Providers.ExcelProvider
+namespace Dynamicweb.DataIntegration.Providers.ExcelProvider;
+
+public class ExportDataToExcelProvider : Integration.Interfaces.IDataExportProvider
{
- public class ExportDataToExcelProvider : Integration.Interfaces.IDataExportProvider
+ private FieldsHelper FieldsHelper = new FieldsHelper();
+ private IEnumerable CustomFields = ProductField.GetProductFields();
+ private const string CommentsAuthor = "Dynamicweb";
+
+ public ExportDataToExcelProvider()
{
- private FieldsHelper FieldsHelper = new FieldsHelper();
- private IEnumerable CustomFields = ProductField.GetProductFields();
- private const string CommentsAuthor = "Dynamicweb";
+ }
- public ExportDataToExcelProvider()
- {
- }
+ public bool ExportData(string destinationFilePath, IEnumerable