winform中 將DataGrid中的資料匯入Excel中,並顯示Excel應用程式

來源:互聯網
上載者:User
datagrid|excel|程式|資料|顯示

   /// <summary>
        /// 將DataGrid中的資料匯入Excel中,並顯示Excel應用程式,
        /// 注意調用該方法必須有安裝Excel 2000應用程式,並且假定DataGrid中綁定的是一DataSet
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="ReportTitle"></param>
        public static void ExportDataGridToExcel(DataGrid grid,string ReportTitle)
        {
            DataTable myTable = ((DataSet)grid.DataSource).Tables[0];

            try
            {
                Excel.Application xlApp = new Excel.ApplicationClass();

                int rowIndex;
                int colIndex;

                rowIndex = 2;
                colIndex = 0;

                Excel.Workbook xlBook =xlApp.Workbooks.Add(true);              

                if (grid.TableStyles.Count >0 )
                {
                    Excel.Range range = xlApp.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,grid.TableStyles[0].GridColumnStyles.Count]);
                    range.MergeCells = true;
                    xlApp.ActiveCell.FormulaR1C1  = ReportTitle;
                    xlApp.ActiveCell.Font.Size  = 18;
                    xlApp.ActiveCell.Font.Bold = true;

                    foreach(DataGridColumnStyle colu in grid.TableStyles[0].GridColumnStyles)
                    {
                        colIndex=colIndex +1;
                        xlApp.Cells[2,colIndex] = colu.HeaderText ;
                    }

                    //得到的表所有行,賦值給儲存格
                    for (int row = 0;row < myTable.Rows.Count;row++)
                    {
                        rowIndex = rowIndex + 1;
                        colIndex = 0;
                        for (int col=0;col<grid.TableStyles[0].GridColumnStyles.Count;col++)
                        {
                            colIndex = colIndex + 1;
                            xlApp.Cells[rowIndex, colIndex] = grid[row,col].ToString();
                        }                   
                    }
                }
                else
                {
                    Excel.Range range = xlApp.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,myTable.Columns.Count]);
                    range.MergeCells = true;
                    xlApp.ActiveCell.FormulaR1C1  = ReportTitle;
                    xlApp.ActiveCell.Font.Size  = 18;
                    xlApp.ActiveCell.Font.Bold = true;
                   
                    //將表中的欄位名稱填到Excel的第一行
                    foreach(DataColumn Col in myTable.Columns)
                    {
                        colIndex = colIndex + 1;
                        xlApp.Cells[2, colIndex] = Col.ColumnName;               
                    }

                    //得到的表所有行,賦值給儲存格
                    for (int row = 0;row < myTable.Rows.Count;row++)
                    {
                        rowIndex = rowIndex + 1;
                        colIndex = 0;
                        for (int col=0;col<myTable.Columns.Count;col++)
                        {
                            colIndex = colIndex + 1;
                            xlApp.Cells[rowIndex, colIndex] = grid[row,col].ToString();
                        }                   
                    }
                }
           
                xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true;
                xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1;         

                xlApp.Cells.EntireColumn.AutoFit();
                xlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter ;
                xlApp.Cells.HorizontalAlignment = Excel.Constants.xlCenter ;       

                xlApp.Visible = true;           
            }
            catch(Exception e)
            {
                throw e;
            }
           


        }



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。