private static void ImportCsvDocument(Application a, CsvDocument csvDocument) { var tempCsvFile = GetTempCsvFile(); try { csvDocument.Save(tempCsvFile); var targetSheet = a.Workbooks.Add().ActiveSheet; a.ScreenUpdating = false; try { var workbook = a.Workbooks.Open(tempCsvFile); try { Range range = workbook.Sheets[1].Range("A1"); range.CurrentRegion.Copy(targetSheet.Range("A1")); } finally { workbook.Close(false); } } finally { a.ScreenUpdating = true; } } finally { File.Delete(tempCsvFile); } }
public static CsvDocument FromExcel(string fileName) { AssertExcelIsInstalled(); var csvFileName = GetTempCsvFile(); var xlsxFileName = fileName; try { ConvertToCsv(csvFileName, xlsxFileName); return(CsvDocument.Load(csvFileName)); } finally { File.Delete(csvFileName); } }
public static void SaveToExcel(this CsvDocument csvDocument, string fileName) { AssertExcelIsInstalled(); var a = new Application(); try { a.DisplayAlerts = false; LoadCsvDocument(a, csvDocument); a.ActiveWorkbook.SaveAs(fileName, XlFileFormat.xlOpenXMLWorkbook, CreateBackup: false); } finally { a.Quit(); Marshal.ReleaseComObject(a); } }
public static void ViewInExcel(this CsvDocument csvDocument) { AssertExcelIsInstalled(); var a = new Application(); try { a.DisplayAlerts = false; LoadCsvDocument(a, csvDocument); } finally { a.Visible = true; a.DisplayAlerts = true; Marshal.ReleaseComObject(a); } }
private static void LoadCsvDocument(Application a, CsvDocument csvDocument) { ImportCsvDocument(a, csvDocument); FormatAsTable(a); SelectFirstCell(a); }