Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
0
 private static void LoadCsvDocument(Application a, CsvDocument csvDocument)
 {
     ImportCsvDocument(a, csvDocument);
     FormatAsTable(a);
     SelectFirstCell(a);
 }