In the DataCollection Service, there is a possibility to use an extension for the export. Develop it on your own and decide how you want to save the output of the selected report.
This article describes the steps on how to make an extension.
Start Microsoft Visual Studio and add a new “Class Library” project.
Now add the following 2 references:
- "DataCollection.Agent.Core.Extensibility.Contracts.dll" (you can find this file in the folder of the DataCollector Windows Service).
- ComponentModel.Composition (this is a .NET assembly).
Decorate your Class with the attribute below, the installed name is visible with the extensions in the DataCollection Frontend.
[Export("%SaveExtensionName%", typeof(ISaveExtension))]
Make sure you have a class that implements "ISAF Extension".
When the project is finished, you can build it and place the output dll in the "Extensions" folder in the "Data Collection Windows Service" folder.
Important note
The DataCollection service uses "GetParameters" to determine which parameters can be installed by a user. These parameters are reflected in the interface of the Data Collection Frontend. The “Save” method is called when an Export is selected and processed by the DataCollection Service. The installed parameters will enter as strings. If necessary, you should cast them yourself to the correct type.
Example
Below is an example for a Custom CSV extension.
using DataCollection.Agent.Core.Extensibility.Contracts;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace DataCollection.Agent.Core.Extensibility.CSV
{
[Export("CustomToCSV", typeof(ISaveExtension))]
public class ToCSV : ISaveExtension
{
private List<String> m_Parameters = new List<string>();
private String m_LastError = String.Empty;
public List<string> GetParameters()
{
return m_Parameters;
}
public String GetLastError()
{
return m_LastError;
}
public String SaveLocation { get; set; }
public String Seperator { get; set; }
public Boolean EncloseWithQuotes { get; set; }
public ToCSV()
{
m_Parameters.Add("Save to file");
m_Parameters.Add("Seperator");
m_Parameters.Add("Enclose with quote");
}
public bool Save(DataTable data, List<KeyValuePair<string, string>> parameters)
{
Boolean MyResult = false;
if (ProcessParameters(parameters) == true)
{
String MyCSVFileContent = ConvertDatasourceCollectionToCSVString(data, Seperator, EncloseWithQuotes);
System.IO.StreamWriter MyFile = new System.IO.StreamWriter(SaveLocation);
MyFile.WriteLine(MyCSVFileContent);
MyFile.Close();
}
return MyResult;
}
private Boolean ProcessParameters(List<KeyValuePair<string, string>> parameters)
{
if (parameters.Any(x => x.Key == "Save Location") == true)
{
SaveLocation = parameters.Where(x => x.Key == "Save Location").FirstOrDefault().Value;
}
if (parameters.Any(x => x.Key == "Seperator") == true)
{
Seperator = parameters.Where(x => x.Key == "Seperator").FirstOrDefault().Value;
}
if (parameters.Any(x => x.Key == "Enclose with quote") == true)
{
String MyEncloseAsString = parameters.Where(x => x.Key == "Enclose with quote").FirstOrDefault().Value;
Boolean MyEncloseWithQuotes = true;
if (Boolean.TryParse(MyEncloseAsString, out MyEncloseWithQuotes) == true)
{
EncloseWithQuotes = MyEncloseWithQuotes;
}
}
return true;
}
private String ConvertDatasourceCollectionToCSVString(DataTable data, String seperator, Boolean encloseWithQuotes)
{
StringBuilder MyResult = new StringBuilder(String.Empty);
//Create Header!
for (Int32 i = 0; i < data.Columns.Count; i++)
{
if (i > 0)
{
MyResult.Append(seperator);
}
MyResult.Append(data.Columns[i]);
}
MyResult.Append(Environment.NewLine);
foreach (DataRow MyDataRow in data.Rows)
{
for (Int32 j = 0; j < data.Columns.Count; j++)
{
//Add delimiter
if (j > 0)
{
MyResult.Append(seperator);
}
String MyDataValueString = MyDataRow[j].ToString();
// Escape all quotes
MyDataValueString = MyDataValueString.Replace("\"", "\"\"");
// For simplicity always add quotes around the value
if (EncloseWithQuotes == true)
{
MyDataValueString = "\"" + MyDataValueString + "\"";
}
MyResult.Append(MyDataValueString);
}
MyResult.Append(Environment.NewLine);
}
return MyResult.ToString();
}
}
}
Comments
0 comments
Please sign in to leave a comment.