濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 通過(guò)剪貼板實(shí)現(xiàn)將DataGridView中的數(shù)據(jù)導(dǎo)出到Excel

通過(guò)剪貼板實(shí)現(xiàn)將DataGridView中的數(shù)據(jù)導(dǎo)出到Excel

熱門(mén)標(biāo)簽:征途美甲店地圖標(biāo)注 柳州電銷(xiāo)機(jī)器人公司 百度地圖怎樣做地圖標(biāo)注 太原400電話上門(mén)辦理 昆明語(yǔ)音電銷(xiāo)機(jī)器人價(jià)格 400電話如何申請(qǐng)取消 浦發(fā)電話機(jī)器人提醒還款 騰訊地圖標(biāo)注手機(jī) 電銷(xiāo)語(yǔ)音機(jī)器人型號(hào)參數(shù)
將DataGridView中的數(shù)據(jù)導(dǎo)出到Excel中有許多方法,常見(jiàn)的方法是使用Office COM組件將DataGridView中的數(shù)據(jù)循環(huán)復(fù)制到Excel Cell對(duì)象中,然后再保存整個(gè)Excel Workbook。但是如果數(shù)據(jù)量太大,例如上萬(wàn)行數(shù)據(jù)或者有多個(gè)Excel Sheet需要同時(shí)導(dǎo)出,效率會(huì)比較低??梢試L試使用異步操作或多線程的方式來(lái)解決UI死鎖的問(wèn)題。

這里介紹一種直接通過(guò)Windows剪貼板將數(shù)據(jù)從DataGridView導(dǎo)出到Excel的方法。代碼如下:
復(fù)制代碼 代碼如下:

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; }
}
}
}

導(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種文本格式

標(biāo)簽:新疆 德陽(yáng) 江蘇 蘭州 張家界 陽(yáng)泉 白山 天門(mén)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《通過(guò)剪貼板實(shí)現(xiàn)將DataGridView中的數(shù)據(jù)導(dǎo)出到Excel》,本文關(guān)鍵詞  通過(guò),剪貼板,實(shí),現(xiàn)將,DataGridView,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《通過(guò)剪貼板實(shí)現(xiàn)將DataGridView中的數(shù)據(jù)導(dǎo)出到Excel》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于通過(guò)剪貼板實(shí)現(xiàn)將DataGridView中的數(shù)據(jù)導(dǎo)出到Excel的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    长春市| 肥西县| 甘谷县| 和政县| 定安县| 瑞昌市| 灵武市| 汝州市| 商河县| 封丘县| 灵璧县| 安福县| 彭山县| 莫力| 安陆市| 东台市| 北川| 皮山县| 民县| 康保县| 天峻县| 岳西县| 乐东| 石家庄市| 荥阳市| 蒙城县| 玉树县| 岑巩县| 长岭县| 扎兰屯市| 清远市| 西藏| 永平县| 广元市| 抚州市| 洞口县| 天峻县| 呼图壁县| 湖北省| 阿拉尔市| 嵩明县|