winform 导出datagridview 到excel
Posted 智取
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform 导出datagridview 到excel相关的知识,希望对你有一定的参考价值。
如果电脑上没有Microsoft.Office.Interop.Excel.dll去找DLL下载站下载即可
需要先导入这个dll的引用
需要先导入这个dll的引用
呈上代码:
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 Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
namespace 导出到execl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//绑定datagridview
private void Bind()
{
string sql = string.Format("select id, no, name, age, gender, address from stuinfo");
//getSet 方法返回的dataset所以要进行.Tables[0]
this.dataGridView1.DataSource = SQLHelpercs.getSet(SQLHelpercs.ConString, sql).Tables[0]; ;
}
/// <summary>
/// 导出excel
/// </summary>
/// <param name="fileName">要保存excel的名称</param>
/// <param name="dg">DataGridView 的 名称</param>
public void OutExecl(string fileName, DataGridView dg)
{
if (dg.Rows.Count > 0)//判断datagridview是否有数据
{
string saveName = string.Empty;//声明一个保存名称
SaveFileDialog sgfDialog=new SaveFileDialog();//创建一个保存对象
sgfDialog.DefaultExt = "xls";//默认保存扩展名
sgfDialog.Filter = "Excel文件|*.xls";//保存文件的类型
sgfDialog.FileName = fileName;//保存的名称
sgfDialog.ShowDialog();
saveName = sgfDialog.FileName;
if(saveName.IndexOf(":")<0)return;//点了取消
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
if (xlapp == null)
{
MessageBox.Show("无法创建Execl!");
return;
}
Microsoft.Office.Interop.Excel.Workbooks wbs = xlapp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = wbs.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); //添加工作薄
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
//写入标题
for (int i = 0; i < dg.ColumnCount; i++)
{
worksheet.Cells[1, i + 1] = dg.Columns[i].HeaderText;
}
//写入数值
for (int r = 0; r < dg.Rows.Count; r++)
{
for (int i = 0; i < dg.ColumnCount; i++)
{
worksheet.Cells[r + 2, i + 1] = dg.Rows[r].Cells[i].Value;
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
if (saveName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveName);
//fileSaved = true;
}
catch (Exception ex)
{
//fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
xlapp.Quit();
GC.Collect();//强行销毁
MessageBox.Show(fileName + "保存成功!", "提示", MessageBoxButtons.OK);
}
else
{
MessageBox.Show("报表为空,无表格需要导出","提示",MessageBoxButtons.OK);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Bind();
}
private void 导出到excelToolStripMenuItem_Click(object sender, EventArgs e)
{
OutExecl("cool.xls",this.dataGridView1);
}
}
}
以上是关于winform 导出datagridview 到excel的主要内容,如果未能解决你的问题,请参考以下文章
Visual Studio 2010 C#,winform,如何将dataGridview的数据导出到Excel表中?
Winform DataGridView控件数据导出EXcel
各位仁兄,我想请教一下在C#winform中,datagridview如何导出excel文件,将excel导入datagridview.