将DataGridView导出为Excel的函数

本文介绍了一种将DataGridView中的数据导出到Excel的方法,并提供了完整的C#代码示例。此方法适用于.NET应用程序,并且需要系统中已安装Excel。

前段时间,帮朋友写了一个在.Net中将DataGridView到处为Excel,希望能为其他朋友提供借鉴。 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace ExcelExports
{
    
public class ExcelExports
    
{
        
/// <summary>
        
/// 把DataGridView到处到Excel中
        
/// </summary>
        
/// <param name="gridView">目标DataGridView</param>
        
/// <param name="fileName">保存文件名称</param>
        
/// <param name="isShowExcle">是否显示Excel界面</param>
        
/// <returns>导出是否成功</returns>

        public static bool ExportForDataGridview(DataGridView gridView,string fileName,bool isShowExcle)
        
{
            
            
//建立Excel对象
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            
try
            
{
                
if (app == null)
                
{
                    
return false;
                }

                app.Visible 
= isShowExcle;
                Workbooks workbooks 
= app.Workbooks;
                _Workbook workbook 
= workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                Sheets sheets 
= workbook.Worksheets;
                _Worksheet worksheet 
= (_Worksheet)sheets.get_Item(1);
                
if (worksheet == null)
                
{
                    
return false;
                }

                
string sLen = "";
                
//取得最后一列列名
                char H=(char)(64+gridView.ColumnCount /26 );
                
char L = (char)(64 + gridView.ColumnCount % 26);
                
if (gridView.ColumnCount < 26)
                
{
                    sLen 
= L.ToString();
                }

                
else
                
{
                    sLen 
= H.ToString() + L.ToString();
                }



                
//标题
                string sTmp = sLen + "1";
                Range ranCaption 
= worksheet.get_Range(sTmp, "A1");
                
string[] asCaption = new string[gridView.ColumnCount];
                
for (int i = 0; i < gridView.ColumnCount; i++)
                
{
                    asCaption[i] 
= gridView.Columns[i].HeaderText;
                }

                ranCaption.Value2 
= asCaption;

                
//数据
                object[] obj = new object[gridView.Columns.Count];
                
for (int r = 0; r < gridView.RowCount-1; r++)
                
{
                    
for (int l = 0; l < gridView.Columns.Count; l++)
                    
{
                        
if (gridView[l, r].ValueType==typeof(DateTime))
                        
{
                            obj[l] 
= gridView[l, r].Value.ToString();
                        }

                        
else
                        
{
                            obj[l] 
= gridView[l, r].Value;
                        }

                    }

                    
string cell1 = sLen + ((int)(r + 2)).ToString();
                    
string cell2="A"+((int)(r+2)).ToString();
                    Range ran 
= worksheet.get_Range(cell1, cell2);
                    ran.Value2 
= obj;
                }

                
//保存
                workbook.SaveCopyAs(fileName);
                workbook.Saved 
= true;
            }

            
finally
            
{
                
//关闭
                app.UserControl = false;
                app.Quit();
            }

            
return true;

        }

    }

}

不过计算机必须安装Excel,否则无法完成。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值