Skip to content

Commit

Permalink
Embedding option parser code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithin Philips committed Jul 20, 2012
1 parent 7c220f8 commit c5ccb30
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
6 changes: 5 additions & 1 deletion ExcelSheetsToCSV/ExcelSheetsToCSV.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ExcelSheetsToCSV</RootNamespace>
<AssemblyName>ExcelSheetsToCSV</AssemblyName>
<AssemblyName>exceltocsv</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
Expand Down Expand Up @@ -46,6 +46,7 @@
<Compile Include="Helper.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\ndesk-options-0.2.1\src\NDesk.Options\NDesk.Options\Options.cs" />
</ItemGroup>
<ItemGroup>
<COMReference Include="Microsoft.Office.Core">
Expand Down Expand Up @@ -76,6 +77,9 @@
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
64 changes: 58 additions & 6 deletions ExcelSheetsToCSV/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,78 @@
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.ComponentModel;
using NDesk.Options;

namespace ExcelSheetsToCSV
{
class Program
{
static int Main(string[] args)
{
if (args.Length != 1)
// the options to be set via command-line
bool help = false;
string outputType = "csv";
XlFileFormat format = XlFileFormat.xlCSVWindows;

// the command-line options
var p = new OptionSet()
{
{ "t|type=", v => outputType = v},
{ "h|help", v => help = v != null},
};

// the parse commandline options and get any arguments
List<string> files = p.Parse(args);

switch (outputType.ToLower())
{
case "csv":
format = XlFileFormat.xlCSVWindows;
break;
case "tab":
format = XlFileFormat.xlTextWindows;
break;
default:
if (!Enum.TryParse(outputType, out format))
{
Console.WriteLine("I did not recognize the output format you specified. Try again.");
return 0;
}
break;
}


// User need help or omitted required argument?
if (help || files.Count <= 0)
{
Console.WriteLine("Excel Sheets to CSV Converter");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine("\texcelsheetstocsv.exe <excel-file>");
Console.WriteLine("\texcelsheetstocsv.exe --help | ([--type=csv|tab] <excel-file> [<excel-file> ...])");
return 0;
}

string excelFile = new FileInfo(args[0]).FullName;
// Transform each file
foreach (var file in files)
{
var fi = new FileInfo(file);
if (fi.Exists)
{
TransformSpreadSheet(fi.FullName, format);
}
else
{
Console.Error.WriteLine("An input file you specified, '{0}', does not exist", fi.Name);
}
}


// done!
return 0;
}

static void TransformSpreadSheet(string excelFile, XlFileFormat format)
{
Console.WriteLine("Reading {0}...", excelFile);

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Expand All @@ -48,14 +102,12 @@ static int Main(string[] args)

File.Delete(csvFileName);
}
sheet.SaveAs(csvFileName, XlFileFormat.xlCSVWindows);
sheet.SaveAs(csvFileName, format);
}

int hwnd = xlApp.Application.Hwnd;
xlApp.Quit();
Helper.TryKillProcessByMainWindowHwnd(hwnd);

return 0;
}

}
Expand Down
3 changes: 3 additions & 0 deletions ExcelSheetsToCSV/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration>

0 comments on commit c5ccb30

Please sign in to comment.