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

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber 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;

       }