通過(guò)剪貼板實(shí)現(xiàn)將DataGridView中的數(shù)據(jù)導(dǎo)出到Excel
這里介紹一種直接通過(guò)Windows剪貼板將數(shù)據(jù)從DataGridView導(dǎo)出到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()
{
BindingList<Car> cars = new BindingList<Car>();
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; }
}
}
}
導(dǎo)出數(shù)據(jù)到Excel的操作在事件toolStripButton1_Click中,代碼的第49行和50行是將DataGridView當(dāng)前選中的行復(fù)制到系統(tǒng)剪貼板中,62行將剪貼板中的內(nèi)容粘貼到Excel默認(rèn)Sheet的A1單元格中。Excel會(huì)自動(dòng)格式化將粘貼的內(nèi)容,如下圖。

使用剪貼板導(dǎo)出數(shù)據(jù)過(guò)程比較簡(jiǎn)單,省去了對(duì)Excel對(duì)象的遍歷和操作,缺點(diǎn)是無(wú)法對(duì)導(dǎo)出的數(shù)據(jù)進(jìn)行格式和樣式的設(shè)置。如果需要對(duì)導(dǎo)出的數(shù)據(jù)進(jìn)行樣式設(shè)置,可以嘗試使用OpenXML的方式來(lái)修改Excel文件的樣式,
- asp.net DataGridView導(dǎo)出到Excel的三個(gè)方法[親測(cè)]
- asp.net GridView導(dǎo)出到Excel代碼
- GridView導(dǎo)出Excel實(shí)現(xiàn)原理與代碼
- GridView選擇性導(dǎo)出Excel解決方案
- C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法
- ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
- C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類(lèi)實(shí)例
- GridView導(dǎo)出Excel常見(jiàn)的5種文本格式
相關(guān)文章
asp.net中的check與uncheck關(guān)鍵字用法解析
這篇文章主要介紹了asp.net中的check與uncheck關(guān)鍵字用法,以實(shí)例形式較為詳細(xì)的分析了check與uncheck關(guān)鍵字的各種常見(jiàn)用法與使用時(shí)的注意事項(xiàng),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
.NET9?EFcore支持早期MSSQL數(shù)據(jù)庫(kù)?ROW_NUMBER()分頁(yè)功能
文章介紹了如何在.NET?9中使用EF?Core實(shí)現(xiàn)對(duì)早期MSSQL數(shù)據(jù)庫(kù)的ROW_NUMBER()分頁(yè)兼容,由于EF?Core?9對(duì)底層API進(jìn)行了重大更新,原有的兼容代碼需要重新實(shí)現(xiàn),具體實(shí)現(xiàn)代碼跟隨小編一起看看吧2024-11-11
IdentityServer4實(shí)現(xiàn).Net Core API接口權(quán)限認(rèn)證(快速入門(mén))
這篇文章主要介紹了IdentityServer4實(shí)現(xiàn).Net Core API接口權(quán)限認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Visual Studio 2017中找回消失的“在瀏覽器中查看”命令
這篇文章主要為大家詳細(xì)介紹了如何在Visual Studio 2017中找回消失的“在瀏覽器中查看”命令,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
.NET Web開(kāi)發(fā)之.NET MVC框架介紹
MVC是一種架構(gòu)設(shè)計(jì)模式,該模式主要應(yīng)用于圖形化用戶(hù)界面(GUI)應(yīng)用程序。那么什么是MVC?MVC由三部分組成:Model(模型)、View(視圖)及Controller(控制器)2014-03-03
ASP.NET中Onclick與OnClientClick遇到的問(wèn)題
本文主要介紹了ASP.NET中Onclick與OnClientClick遇到的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
asp.net中3種驗(yàn)證碼示例(實(shí)現(xiàn)代碼)(數(shù)字,數(shù)字字母混和,漢字)
asp.net中3種驗(yàn)證碼示例代碼,分別是數(shù)字,數(shù)字字母混和,漢字,需要的朋友可以參考下2012-10-10
C#調(diào)用動(dòng)態(tài)unlha32.dll解壓Lha后綴的打包文件分享
這篇文章介紹了,C#調(diào)用動(dòng)態(tài)unlha32.dll解壓Lha后綴的打包文件,有需要的朋友可以參考一下2013-09-09

