• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    通过剪贴板实现将DataGridView中的数据导出到Excel
    将DataGridView中的数据导出到Excel中有许多方法,常见的方法是使用Office COM组件将DataGridView中的数据循环复制到Excel Cell对象中,然后再保存整个Excel Workbook。但是如果数据量太大,例如上万行数据或者有多个Excel Sheet需要同时导出,效率会比较低。可以尝试使用异步操作或多线程的方式来解决UI死锁的问题。

    这里介绍一种直接通过Windows剪贴板将数据从DataGridView导出到Excel的方法。代码如下:
    复制代码 代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Reflection;
    using Microsoft.Office.Interop.Excel;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    this.saveFileDialog1.Filter = "Excel Workbook|*.xlsx|Excel Macro-Enabled Workbook|*.xlsm|Excel 97-2003 Workbook|*.xls";
    this.saveFileDialog1.FileName = "demo.xlsx";

    LoadData();
    }

    private void LoadData()
    {
    BindingListCar> cars = new BindingListCar>();

    cars.Add(new Car("Ford", "Mustang", 1967));
    cars.Add(new Car("Shelby AC", "Cobra", 1965));
    cars.Add(new Car("Chevrolet", "Corvette Sting Ray", 1965));

    this.dataGridView1.DataSource = cars;
    }

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
    string filePath = string.Empty;
    if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    filePath = this.saveFileDialog1.FileName;
    }
    else
    {
    return;
    }

    this.dataGridView1.SelectAll();
    Clipboard.SetDataObject(this.dataGridView1.GetClipboardContent());

    Excel.Application objExcel = null;
    Excel.Workbook objWorkbook = null;
    Excel.Worksheet objsheet = null;
    try
    {
    objExcel = new Microsoft.Office.Interop.Excel.Application();
    objWorkbook = objExcel.Workbooks.Add(Missing.Value);
    objsheet = (Excel.Worksheet)objWorkbook.ActiveSheet;
    objExcel.Visible = false;

    objExcel.get_Range("A1", System.Type.Missing).PasteSpecial(XlPasteType.xlPasteAll, XlPasteSpecialOperation.xlPasteSpecialOperationNone, Type.Missing, Type.Missing);
    objsheet.Name = "Demo";
    //Set table properties
    objExcel.Cells.EntireColumn.AutoFit();//auto column width
    objExcel.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
    objExcel.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
    objExcel.ErrorCheckingOptions.BackgroundChecking = false;

    //save file
    objWorkbook.SaveAs(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value);
    }
    catch (Exception error)
    {
    MessageBox.Show(error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
    }
    finally
    {
    //Dispose the Excel related objects
    if (objWorkbook != null)
    {
    objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
    }
    if (objExcel.Workbooks != null)
    {
    objExcel.Workbooks.Close();
    }
    if (objExcel != null)
    {
    objExcel.Quit();
    }

    objsheet = null;
    objWorkbook = null;
    objExcel = null;
    GC.Collect(); // force final cleanup.
    }
    }
    }

    public class Car
    {
    private string _make;
    private string _model;
    private int _year;

    public Car(string make, string model, int year)
    {
    _make = make;
    _model = model;
    _year = year;
    }

    public string Make
    {
    get { return _make; }
    set { _make = value; }
    }

    public string Model
    {
    get { return _model; }
    set { _model = value; }
    }

    public int Year
    {
    get { return _year; }
    set { _year = value; }
    }
    }
    }

    导出数据到Excel的操作在事件toolStripButton1_Click中,代码的第49行和50行是将DataGridView当前选中的行复制到系统剪贴板中,62行将剪贴板中的内容粘贴到Excel默认Sheet的A1单元格中。Excel会自动格式化将粘贴的内容,如下图。


    使用剪贴板导出数据过程比较简单,省去了对Excel对象的遍历和操作,缺点是无法对导出的数据进行格式和样式的设置。如果需要对导出的数据进行样式设置,可以尝试使用OpenXML的方式来修改Excel文件的样式,
    您可能感兴趣的文章:
    • asp.net DataGridView导出到Excel的三个方法[亲测]
    • asp.net GridView导出到Excel代码
    • GridView导出Excel实现原理与代码
    • GridView选择性导出Excel解决方案
    • C#使用RenderControl将GridView控件导出到EXCEL的方法
    • ASP.NET使用GridView导出Excel实现方法
    • C#导出GridView数据到Excel文件类实例
    • GridView导出Excel常见的5种文本格式
    上一篇:在C#中生成与PHP一样的MD5 Hash Code的方法
    下一篇:.NET WinFrom中给文本框添加拖放事件的代码
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    通过剪贴板实现将DataGridView中的数据导出到Excel 通过,剪贴板,实,现将,DataGridView,