23 August 2011

CSV to XLS utility (requires Microsoft.Office.Interop.Excel)


namespace CSVtoExcel
{
 using System;
 using Excel = Microsoft.Office.Interop.Excel;
 using File = System.IO.File;
 using Path = System.IO.Path;
 class Program
 {
   static void Main(string[] args)
   {
     if (args.Length == 0)
     {
       throw new ApplicationException("Missing file name");
     }
     string file = args[0];
     if (!File.Exists(file))
     {
       throw new System.IO.FileNotFoundException("File specified in command line not found", file);
     }
     var app = new Excel.Application() { DisplayAlerts = false };
     try
     {
       app.Workbooks.Open(file);
       app.ActiveWorkbook.SaveAs(
         Path.ChangeExtension(file, "xls"),
         FileFormat: Excel.XlFileFormat.xlWorkbookNormal,
         ConflictResolution: Excel.XlSaveConflictResolution.xlLocalSessionChanges);
     }
     finally
     {
       app.Quit();
     }
   }
 }
}

No comments: