C# 中导出Excel表格中,怎么实现啊.?

我用的是DatagridView表显示数据.!但是我想把dataGridView表中的数据导出到Excel表中啊1.、要用到什么控件吗.?麻烦说的详细点.!-------... 我用的是DatagridView表显示数据.! 但是我想把dataGridView表中的数据

导出到Excel表中啊1.、要用到什么控件吗.? 麻烦说的详细点.!

--------------------------------------------------------------------
有源码那就更好啊1. 谢谢你们了.! 我在此感激不尽。。。

分不是很多,希望你们不要嫌少..

--------------------------------------------------------------------
展开
 我来答
百度网友c4249ef
2008-11-04 · TA获得超过4867个赞
知道大有可为答主
回答量:1855
采纳率:0%
帮助的人:3230万
展开全部
我给你个源码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace Property_management
{
class Export
{
private static System.Windows.Forms.DataGridView gridView;
private static System.Windows.Forms.ProgressBar toolStripProgressBar1;
private static Timer time;
private static DataSet objSet = new DataSet();
private static SaveFileDialog saveFileDialog = new SaveFileDialog();
private static SaveFileDialog saveFileDialog2 = new SaveFileDialog();

public static System.Windows.Forms.DataGridView _gridView
{
get { return gridView; }
set { gridView = value; }
}
public static System.Windows.Forms.ProgressBar _toolStripProgressBar1
{
get { return toolStripProgressBar1; }
set { toolStripProgressBar1 = value; }
}
public static Timer _time
{
get { return time; }
set { time = value; }
}
public static DataSet _objSet
{
get { return objSet; }
set { objSet.Clear(); objSet = value; }
}

public static void ExportTOExcel2()
{
if (gridView.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
toolStripProgressBar1.Visible = true;
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); ;
excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = "test";

//生成字段名称
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText.ToString();
}
//填充数据
for (int i = 0; i < gridView.RowCount; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].Value.GetType() == typeof(string))
{
excel.Cells[i + 2, j + 1] ="'"+gridView[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();

}
}
toolStripProgressBar1.Value += 100 / gridView.RowCount;
}
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
MessageBox.Show("数据已经成功导出!!");
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Visible = false;

System.Diagnostics.Process.Start(strName);
}
}
}

}
}

我把功能放在这个类里了
需要添加一个引用
右边的资源管理器右键单击“引用”,选择添加引用-》com,找到microsoft excel 11 那个,添加进去
如果有个黄色的叹号,建议重装一次office

下面是如何使用
Export._gridView = GV;//这是指定DatagridView
Export._toolStripProgressBar1 = PB;//如果有滚动条的话加上,没有不设定也行
Export._objSet = this.propertyDataSet5.Fee_already.DataSet;//指定你的DatagridView使用的dataset
Export.ExportTOExcel2();//调用函数导出
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式