时间:2016-02-15 23:16 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【通过剪贴板实现将DataGridView中的数据导出到Excel】,下面是详细的讲解!
通过剪贴板实现将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()
{
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; }
}
}
}
关于通过剪贴板实现将DataGridView中的数据导出到Excel的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【asp】asp.net ubb使用代码-net-ubb使用
- 【默认图片】图片不存在使用默认图片代替的实例
- 【asp】asp.net 页面转向 Response.Redirect Ser
- 【jQuery】jQuery实现倒计时跳转的例子-倒计时跳
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
