Created
March 25, 2014 11:04
-
-
Save adamkdean/9759419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Reflection; | |
using Excel = Microsoft.Office.Interop.Excel; | |
namespace CSV2XLS | |
{ | |
class CSV2XLS | |
{ | |
public struct ColumnProperties | |
{ | |
public string NumberFormat { get; set; } | |
public float ColumnWidth { get; set; } | |
} | |
public static bool ConvertToXLS(string csv, string filename, ColumnProperties[] columnProperties = null) | |
{ | |
var path = Path.GetDirectoryName(filename); | |
if (File.Exists(filename) || !HasWriteAccessToDirectory(path)) { | |
return false; | |
} | |
try | |
{ | |
var xlApp = new Excel.Application(); | |
var xlWorkbook = xlApp.Workbooks.Add(Missing.Value); | |
var xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1); | |
var lines = csv.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); | |
for (var y = 0; y < lines.Length; y++) | |
{ | |
var cells = lines[y].Split(','); | |
for (var x = 0; x < cells.Length; x++) | |
{ | |
Excel.Range cell = (Excel.Range)xlWorksheet.Cells[y+1, x+1]; | |
cell.Value = cells[x]; | |
if (columnProperties != null) | |
{ | |
if (columnProperties[x].ColumnWidth != null) | |
{ | |
cell.ColumnWidth = columnProperties[x].ColumnWidth; | |
} | |
if (columnProperties[x].NumberFormat != null) | |
{ | |
cell.NumberFormat = columnProperties[x].NumberFormat; | |
} | |
} | |
} | |
} | |
xlWorkbook.SaveAs(filename, Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, | |
Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, | |
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); | |
xlWorkbook.Close(true, Missing.Value, Missing.Value); | |
xlApp.Quit(); | |
ReleaseObject(xlWorksheet); | |
ReleaseObject(xlWorkbook); | |
ReleaseObject(xlApp); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
return false; | |
} | |
return true; | |
} | |
private static bool HasWriteAccessToDirectory(string path) | |
{ | |
try | |
{ | |
// Attempt to get a list of security permissions from the folder. | |
// This will raise an exception if the path is read only or do not have access to view the permissions. | |
System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(path); | |
return true; | |
} | |
catch (UnauthorizedAccessException) | |
{ | |
return false; | |
} | |
} | |
private static void ReleaseObject(object obj) | |
{ | |
try | |
{ | |
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); | |
obj = null; | |
} | |
catch (Exception) | |
{ | |
obj = null; | |
} | |
finally | |
{ | |
GC.Collect(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment