Esempio n. 1
0
        private DataSet importExcelFile(string fileName)
        {
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

            System.Data.DataSet ds     = new System.Data.DataSet();
            Workbook            wkBook = excelApp.Workbooks.Open(fileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

            ds.DataSetName = wkBook.Name;

            foreach (Worksheet ws in wkBook.Worksheets)
            {
                ds.Merge(importExcelSheet(ws));
                ds.AcceptChanges();
            }

            System.Windows.Forms.DataGridTableStyle tstyle = new System.Windows.Forms.DataGridTableStyle();
            tstyle.AllowSorting   = true;
            tstyle.RowHeaderWidth = 0;

            DG.TableStyles.Add(tstyle);

            DG.NavigateBack();
            DG.NavigateBack();
            DG.DataSource = ds;

            excelApp.Quit();
            releaseObject(excelApp);
            releaseObject(wkBook);

            return(ds);
        }