Skip to content

Commit

Permalink
Merge pull request dotnetprojects#218 from dotnetprojects/ConvertPlcA…
Browse files Browse the repository at this point in the history
…larmTextListXlsxToCsv

convert GitHandler PlcAlarmTextListGroup xlsx to csv
  • Loading branch information
jogibear9988 authored Sep 24, 2024
2 parents 8ad300f + 1aa88ac commit ebdacff
Show file tree
Hide file tree
Showing 22 changed files with 145,850 additions and 145,774 deletions.
73 changes: 73 additions & 0 deletions TiaGitHandler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
using DotNetSiemensPLCToolBoxLibrary.DataTypes.Projectfolders;
using DotNetSiemensPLCToolBoxLibrary.General;
using DotNetSiemensPLCToolBoxLibrary.Projectfiles;
using ICSharpCode.SharpZipLib.Core;
using OfficeOpenXml;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using TiaGitHandler.Properties;
using Application = System.Windows.Application;
using MessageBox = System.Windows.Forms.MessageBox;
Expand Down Expand Up @@ -211,6 +214,26 @@ static void Main(string[] args)
ParseFolder(prj.ProjectStructure, exportPath, skippedBlocksList);
prj.ExportTextlists(prj.ProjectStructure, exportPath);

var xlsxFiles = Directory.GetFiles(exportPath, "*.xlsx", SearchOption.AllDirectories);
foreach (var xlsxFile in xlsxFiles.Where(x => x.Contains("plcalarmtextlistgroup")))
{
var fileInfo = new FileInfo(xlsxFile);
using (var package = new ExcelPackage(fileInfo))
{
var worksheet1 = package.Workbook.Worksheets[1];
var textlistType = worksheet1.Cells["B2"].Text; // textlist type - decimal, binary, bit

string targetFile = Path.Combine(
Path.GetDirectoryName(xlsxFile),
$"{Path.GetFileNameWithoutExtension(xlsxFile)}_{textlistType}.csv"
);

ConvertPlcAlarmTextListXlsxToCsv(xlsxFile, targetFile, textlistType);
}

File.Delete(xlsxFile);
}

Console.WriteLine();
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Expand All @@ -221,6 +244,43 @@ static void Main(string[] args)
Console.ReadKey();
}

public static void ConvertPlcAlarmTextListXlsxToCsv(string sourceFile, string targetFile, string textlistType)
{
var fileInfo = new FileInfo(sourceFile);
using (var package = new ExcelPackage(fileInfo))
{
var worksheet = package.Workbook.Worksheets[2];

var maxColumnNumber = worksheet.Dimension.End.Column;
var totalRowCount = worksheet.Dimension.End.Row;

using (var writer = new StreamWriter(targetFile, false, Encoding.UTF8))
{
for (int row = 1; row <= totalRowCount; row++)
{
var currentRow = new List<string>(maxColumnNumber);

for (int col = 1; col <= maxColumnNumber; col++)
{
var cellValue = worksheet.Cells[row, col].Text;

// double quotation marks (") -> ("")
if (cellValue.Contains("\""))
cellValue = cellValue.Replace("\"", "\"\"");

// Add quotation marks if cell contains commas or quotation marks
if (cellValue.Contains(",") || cellValue.Contains("\""))
cellValue = $"\"{cellValue}\"";

currentRow.Add(cellValue);
}

writer.WriteLine(string.Join(";", currentRow));
}
}
}
}

private class EncodingStringWriter : StringWriter
{
private readonly Encoding _encoding;
Expand Down Expand Up @@ -340,6 +400,7 @@ private static void ParseFolder(ProjectFolder folder, string dir, List<string> s
ns.AddNamespace("smns2", "http://www.siemens.com/automation/Openness/SW/Interface/v3");
ns.AddNamespace("smns3", "http://www.siemens.com/automation/Openness/SW/NetworkSource/StatementList/v3");
ns.AddNamespace("smns4", "http://www.siemens.com/automation/Openness/SW/NetworkSource/StructuredText/v2");
ns.AddNamespace("smns5", "http://www.siemens.com/automation/Openness/SW/NetworkSource/StructuredText/v4");

try
{
Expand Down Expand Up @@ -479,6 +540,18 @@ private static void ParseFolder(ProjectFolder folder, string dir, List<string> s
{
}

try
{
var nodes = xmlDoc.SelectNodes("//smns5:DateAttribute[@Name='ParameterModifiedTS']", ns);
foreach (var node in nodes.Cast<XmlNode>())
{
node.ParentNode.RemoveChild(node);
}
}
catch
{
}

//try
//{
// var nodes = xmlDoc.SelectNodes("//smns:Address[@Area='None' and @Informative='true']", ns);
Expand Down
3 changes: 3 additions & 0 deletions TiaGitHandler/TiaGitHandler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<StartupObject />
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EPPlus" Version="4.5.3.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotNetSiemensPLCToolBoxLibrary.TIAV13\DotNetSiemensPLCToolBoxLibrary.TIAV13.csproj">
<Private>true</Private>
Expand Down
Loading

0 comments on commit ebdacff

Please sign in to comment.