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

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 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;

       }