婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 通過剪貼板實現將DataGridView中的數據導出到Excel

通過剪貼板實現將DataGridView中的數據導出到Excel

熱門標簽:征途美甲店地圖標注 柳州電銷機器人公司 百度地圖怎樣做地圖標注 太原400電話上門辦理 昆明語音電銷機器人價格 400電話如何申請取消 浦發電話機器人提醒還款 騰訊地圖標注手機 電銷語音機器人型號參數
將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種文本格式

標簽:新疆 德陽 江蘇 蘭州 張家界 陽泉 白山 天門

巨人網絡通訊聲明:本文標題《通過剪貼板實現將DataGridView中的數據導出到Excel》,本文關鍵詞  通過,剪貼板,實,現將,DataGridView,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《通過剪貼板實現將DataGridView中的數據導出到Excel》相關的同類信息!
  • 本頁收集關于通過剪貼板實現將DataGridView中的數據導出到Excel的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 施秉县| 江安县| 滁州市| 巨鹿县| 会东县| 土默特左旗| 越西县| 贡嘎县| 天柱县| 沿河| 唐山市| 河池市| 拉孜县| 苏州市| 东方市| 察哈| 福鼎市| 南溪县| 灵寿县| 纳雍县| 嘉定区| 庆云县| 吐鲁番市| 平潭县| 本溪市| 克拉玛依市| 铁力市| 光泽县| 项城市| 怀化市| 阿城市| 淮安市| 米脂县| 扶风县| 当阳市| 玉溪市| 龙江县| 兰西县| 玛曲县| 布拖县| 古丈县|