首页 > 开发 > 综合 > 正文

winform中 将DataGrid中的数据导入Excel中,并显示Excel应用程序

2024-07-21 02:26:09
字体:
来源:转载
供稿:网友

   /// <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;
            }
           


        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表