惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
MyScale Blog
MyScale Blog
博客园_首页
G
Google Developers Blog
博客园 - 【当耐特】
美团技术团队
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News
小众软件
小众软件
博客园 - 司徒正美
雷峰网
雷峰网
T
Tailwind CSS Blog
V
V2EX
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
罗磊的独立博客
量子位
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog

博客园 - Angelo Dell'inferno

[ASP.NET] 基于.Net Remoting 的网站访问监控模块 [转]Run a Http Response Filter together with an Ajax Update Panel [ASP.NET] HyperLink + Image 实现动态图片链接 [ASP.NET] (转)地址栏参数的判断 [ASP.NET] 实现Label自动换行 [ASP.NET] 验证码生成 [ASP.NET] 实现客户端浏览服务端目录的页面 [ASP.NET]Treeview 控件显示服务端目录文件夹及文件 [C#] 获取两个时间点的时间间隔 [SQL Server][转]数据库并发控制——活锁&死锁 [JavaScript] 防止页面被嵌入Iframe [SQL Server] 存储过程事务 [SQL Server] T-SQL 连接数据库方法 [C#] 杀Excel进程 [ASP.NET] (原创)自定义GridView分页 [C#] 将DataSet内容导入到Excel (矩阵区域导出) [ASP.NET] 服务器端下载文件实现 [Oracle] 日期相关操作 [ORACLE] 函数大全
[C#] GridView导出到Excel
Angelo Dell'inferno · 2008-03-04 · via 博客园 - Angelo Dell'inferno

       /// <summary>
       /// 导出到Excel
       /// </summary>
       /// <param name="dgv"></param>
       /// <param name="title"></param>
       public static void DataGridViewToExcel(DataGridView dgv, string title)
       {
           if (dgv.DataSource == null || dgv.Columns.Count == 0)
           {
               return;
           }
           Excel.Application exc = new Excel.ApplicationClass();
           if (exc == null)
           {
               throw new Exception("Excel无法启动");
           }
           Workbooks workbooks = exc.Workbooks;
           _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
           Sheets sheets = exc.Sheets;
           _Worksheet worksheet = (_Worksheet)sheets[1];
           if (worksheet == null)
           {
               throw new Exception("Worksheet error");
           }

           DataGridViewClipboardCopyMode bakMode = dgv.ClipboardCopyMode;
           bool copyCurrentCellText = true;
           bool showRowHeader = dgv.RowHeadersVisible;
           if (dgv is gvGridView)
           {
               copyCurrentCellText = ((gvGridView)dgv).CopyCurrentCellText;
               ((gvGridView)dgv).CopyCurrentCellText = false;
           }
           dgv.RowHeadersVisible = false;

           IDataObject bakClipbordDasta = Clipboard.GetDataObject();

           dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
           IDataObject exportData = dgv.GetClipboardContent();
           if (exportData != null)
           {
               DataObject data = exportData as DataObject;
               Clipboard.SetDataObject(data.GetText(TextDataFormat.UnicodeText));
           }
           if (dgv is gvGridView)
           {
               ((gvGridView)dgv).CopyCurrentCellText = copyCurrentCellText;
           }
           dgv.RowHeadersVisible = showRowHeader;

           if (exportData != null)
           {
               exc.Columns.NumberFormatLocal = "@";
               worksheet.Paste(worksheet.get_Range("A2", System.Type.Missing), false);
           }
           Clipboard.SetDataObject(bakClipbordDasta);

           Range r = worksheet.get_Range(exc.Cells[1, 1], exc.Cells[1, dgv.Columns.Count]);
           exc.Visible = false;
           r.MergeCells = true;
           if (r == null)
           {
               MessageBox.Show("Range无法启动");
               throw new Exception("Range error");
           }
           //以上是一些例行的初始化工作,下面进行具体的信息填充
           //标题
           exc.ActiveCell.FormulaR1C1 = title;
           exc.ActiveCell.Font.Size = 12;
           exc.ActiveCell.Font.Bold = true;

           exc.Cells.EntireColumn.AutoFit();
           //加边框
           exc.Cells.EntireColumn.Borders.LineStyle = 1;
           exc.Cells.EntireColumn.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThick;
           exc.Cells.EntireColumn.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThick;
           exc.Cells.EntireColumn.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThick;
           exc.Cells.EntireColumn.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThick;

           exc.Cells.VerticalAlignment = Excel.Constants.xlCenter;
           exc.Cells.HorizontalAlignment = Excel.Constants.xlCenter;

           exc.Visible = true;

       }