.net如何将girdview中的数据导出到excel

来源:百度知道 编辑:UC知道 时间:2024/06/06 00:28:35
我想把gridview中的数据导出到excel表中,使得excel表里面呈现的只是数据,没有.net中的界面。我在网上搜到一个函数,可以把gridview导出到excel,但是是把整个界面都搬到excel里面了,我想对某些数据进行求和,不能实现。在这里,请教大家,如何把gridview里面的纯数据导入到excel里面,以便我求和等操作。下面是我找到的函数程序,可以把界面导出的,希望大家帮我改改,不胜感激。
public void gridexcel(GridView gd) //gridview数据导出到exvel gd为哪个gridview
{
try
{
if (gd.Rows.Count != 0)
{
System.Web.HttpContext con = System.Web.HttpContext.Current;
con.Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
con.Response.ContentType = "application/vnd.ms-excel";
con.Response.ContentEncoding = System.Text.Encoding.UTF7;
strWriter = new System.IO.StringWriter();
htmlWriter = new System.We

我用的是2008,office2003,在工程里面先添加引用,浏览com,之后找到microsoft excel组建。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Excel.Application app = new ApplicationClass();
app.Visible = true;
Excel.Workbook wbk = app.Workbooks.Open(@"c:\Book1.xls", System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing);
Excel.Worksheet wst = (Worksheet)wbk.Worksheets[1];
wbk.SaveAs(@"c:\aa.txt",XlFileFormat.xlUnicodeText,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
XlSaveAsAc