Skip to content

Commit

Permalink
1.3 code cleanup, load data from object, json, url (json)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin kotynia committed Jul 17, 2017
1 parent 08521ae commit acc03fe
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 88 deletions.
1 change: 1 addition & 0 deletions ExcelToHtml.console/ExcelToHtml.console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestObject.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
127 changes: 90 additions & 37 deletions ExcelToHtml.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,139 @@
using System.Collections.Generic;
using System.IO;

namespace ExcelToHtml.CL
namespace ExcelToHtml.console
{
class Program
{
static void Main(string[] args)
{
string ExcelPath = String.Join("", args);

#if DEBUG //TEST
ExcelPath = @"c:\git\ExcelToHtml\Test\test4.xlsx";
#if DEBUG //TEST DATA
var testdata = new List<string>{
@"-t=c:\git\ExcelToHtml\Test\test1.xlsx",
@"-data=https://transit.land//api/v1/changesets/1/change_payloads"
};
args = testdata.ToArray();
#endif


var arguments = ResolveArguments(args);
string ExcelPath;
string DataUrl;


if (args.Length == 1)
ExcelPath = args[0];
else
arguments.TryGetValue("-t", out ExcelPath);

string DataPath = ExcelPath + ".yaml";
string HtmlPath = ExcelPath + ".html";

Console.WriteLine("ExcelToHtml https://github.com/marcinKotynia/ExcelToHtml ");
Console.WriteLine("Usage: ExcelToHtml.exe [Path] ");
Console.WriteLine("Usage: ExcelToHtml.exe c:\\book.xls ");

Console.WriteLine("ExcelToHtml https://github.com/marcinKotynia/ExcelToHtml");
Console.WriteLine("ExcelToHtml.console.exe [xlsx File]");
Console.WriteLine("");


#if !DEBUG
try
{
//Read Excel File
FileInfo ExcelFile = new FileInfo(ExcelPath);
#endif

Console.WriteLine(" Processing {0}", ExcelPath);

var WorksheetHtml = new ExcelToHtml.ToHtml(ExcelFile);
//Read Excel File
FileInfo ExcelFile = new FileInfo(ExcelPath);


//Read Data Simple JSON cell,value
FileInfo DataFile = new FileInfo(DataPath);
if (!DataFile.Exists)
{
Console.WriteLine("Data File Not Found {0}", DataFile);
}
else
{
var WorksheetHtml = new ExcelToHtml.ToHtml(ExcelFile);

//Dictionary<string, string> Cells = new Dictionary<string, string>();
//InputOutput.Add("A1", "Hello World"); //set hello world
//InputOutput.Add("A2", "=2+1"); //set formula
//InputOutput.Add("[[TemplateField]]", "HelloTemplate"); //FillTempalte Filed
//InputOutput.Add(".A2", null); //Output value form A2
WorksheetHtml.DebugMode = true;

string Data = File.ReadAllText(DataPath);
//Read Data Simple JSON cell,value
FileInfo DataFile = new FileInfo(DataPath);
if (!DataFile.Exists)
{
Console.WriteLine(" Loading optional configuration {0} - Not found.", DataFile);
}
else
{

//Read Data From Yaml
var DeSerializer = new YamlDotNet.Serialization.Deserializer();
Dictionary<string, string> Cells = DeSerializer.Deserialize<Dictionary<string, string>>(Data);
//Dictionary<string, string> Cells = new Dictionary<string, string>();
//InputOutput.Add("A1", "Hello World"); //set hello world
//InputOutput.Add("A2", "=2+1"); //set formula
//InputOutput.Add("[[TemplateField]]", "HelloTemplate"); //FillTempalte Filed
//InputOutput.Add(".A2", null); //Output value form A2

//Fill Cells
var output = WorksheetHtml.GetSetCells(Cells);
string Data = File.ReadAllText(DataPath);

var Serializer = new YamlDotNet.Serialization.Serializer();
string Yaml = Serializer.Serialize(output);
//Read Data From Yaml
var DeSerializer = new YamlDotNet.Serialization.Deserializer();
Dictionary<string, string> Cells = DeSerializer.Deserialize<Dictionary<string, string>>(Data);

//Get Set Cells and write to Yaml
var output = WorksheetHtml.DataGetSet(Cells);
var Serializer = new YamlDotNet.Serialization.Serializer();
string Yaml = Serializer.Serialize(output);
File.WriteAllText(DataPath, Yaml);
}

File.WriteAllText(DataPath, Yaml);
ExcelToHtml.console.Company test = new ExcelToHtml.console.Company();

}

if (arguments.TryGetValue("-data", out DataUrl))
WorksheetHtml.DataFromUrl(DataUrl);

string html = WorksheetHtml.Execute();
string html = WorksheetHtml.RenderHtml();

Console.WriteLine("File Saved {0}", HtmlPath);
File.WriteAllText(HtmlPath, html);
Console.WriteLine(" File Saved {0}", HtmlPath);
File.WriteAllText(HtmlPath, html);

#if DEBUG //TEST
Console.ReadKey();
Console.ReadKey();
#endif

#if !DEBUG
}
catch (Exception ex)
{
Console.WriteLine("ERROR " + ex.Message);
Console.ReadKey();
}
#endif

}


/// <summary>
/// Get Arguments
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
private static Dictionary<string, string> ResolveArguments(string[] args)
{
if (args == null)
return null;

if (args.Length > 0)
{
var arguments = new Dictionary<string, string>();

for (int i = 0; i < args.Length; i++)
{
int idx = args[i].IndexOf('=');
if (idx > 0)
arguments[args[i].Substring(0, idx)] = args[i].Substring(idx + 1);
else
arguments.Add(i.ToString(), args[i]);
}

return arguments;
}

return null;
}

}
}
12 changes: 6 additions & 6 deletions ExcelToHtml.console/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ExcelToHtml.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("ExcelToHtml.console")]
[assembly: AssemblyDescription("https://github.com/marcinKotynia/ExcelToHtml")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ExcelToHtml.Test")]
[assembly: AssemblyCompany("Marcin Kotynia")]
[assembly: AssemblyProduct("ExcelToHtml.console")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand All @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
38 changes: 38 additions & 0 deletions ExcelToHtml.console/TestObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelToHtml.console
{
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }

}

public class Company
{
public string CompanyName;
public string CompanyCode;
public List<Person> People = new List<Person>();

public Company() {
CompanyName = "Acme";
CompanyCode = "CODE13";

Person x = new Person();
x.Name = "John";
x.Surname = "Wick";

People.Add(x);
People.Add(x);
People.Add(x);

}


}
}
4 changes: 4 additions & 0 deletions ExcelToHtml/ExcelToHtml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<Reference Include="FastMember.Signed">
<HintPath>lib\FastMember.Signed.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
Expand All @@ -56,6 +59,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="stringHelpers.cs" />
<Compile Include="ToHtml.cs" />
<Compile Include="Helpers.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
4 changes: 4 additions & 0 deletions ExcelToHtml/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static Byte[] ListToExcel<T>(List<T> query)
ws.Cells[1, i + 1].Value = Headings[i].Name;
}

//populate our Data
//populate our Data
if (query.Count() > 0)
{
Expand All @@ -45,6 +46,9 @@ public static Byte[] ListToExcel<T>(List<T> query)
}
}




}

}
4 changes: 2 additions & 2 deletions ExcelToHtml/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
Loading

0 comments on commit acc03fe

Please sign in to comment.