Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        
        GridView2.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();这种方法就算了。
好像OWC可以不知道还有什么方法
请各位不惜吝啬!!!

解决方案 »

  1.   

    可以将GridView对应的source DataTable导出http://topic.csdn.net/u/20091127/13/4c7091b8-31d4-44bf-b21c-476cffde7873.html
      

  2.   

    以前写的一个例子
    #region[把Gridview中的数据导出到Excel]
        /// <summary>
        /// 把Gridview中的数据导出到Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bt_out_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
            //gaoyang [10/21/2006] 经测试如果设置为 GetEncoding("GB2312"),导出的文件将会出现乱码。
            Response.ContentEncoding = System.Text.Encoding.UTF7;        //设置输出文件类型为excel文件
            Response.ContentType = "application/ms-excel";
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.gv_information.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }
        #endregion
      

  3.   

     可以引用EXCEL的组件 操作EXCEL进行下载,
     
      

  4.   

    //======================================================================
    //
    //        Copyright : ...............
    //        All rights reserved
    //
    //        Filename : HmExcelAssist
    //        Description : Excel操作类, 将DataTable中的数据保存至Excel
    //
    //        created by 枫中玫瑰 at  2009-10-09 16:00:09
    //
    //======================================================================using System;
    using System.Text;
    using System.Data;
    using System.Threading;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using Excel = Microsoft.Office.Interop.Excel;namespace HmDataPrinter
    {
        /// <summary>
        /// Excel操作类
        /// </summary>
        public class HmExcelAssist
        {
            public static string saveFileName = string.Empty;           // 用于保存EXCEL文件的文档名
            public static DataTable dtDataSource = new DataTable();     // Excel数据源
            /// <summary>
            /// 实现DataGridView向DataTable的转换
            /// </summary>
            /// <param name="dvSource">DataGridView</param>
            public static void GridViewToTable(DataGridView dvSource)
            {
                dtDataSource = new DataTable();            DataColumn col;                     // 设置列
                for (int i = 0; i < dvSource.Columns.Count; i++)
                {
                    col = new DataColumn();
                    col.ColumnName = dvSource.Columns[i].HeaderText;
                    dtDataSource.Columns.Add(col);
                }            DataRow dr;                         // 设置行
                for (int i = 0; i < dvSource.Rows.Count; i++)
                {
                    dr = dtDataSource.NewRow();
                    for (int j = 0; j < dvSource.Columns.Count; j++)
                    {
                        dr[j] = 1 == j ? "'" + dvSource.Rows[i].Cells[j].Value.ToString() :
                            dvSource.Rows[i].Cells[j].Value.ToString();
                    }
                    dtDataSource.Rows.Add(dr);
                }
            }        
            /// <summary>
            /// 保存记录至Excel
            /// </summary>
            public static void SaveRecordToExcel()
            {
                saveFileName = string.Format("{0}{1}记录", DateTime.Now.Date.ToString("yyyyMMdd"),
                    saveFileName);                                                  // 设置默认的保存文件名            SaveFileDialog saveDialog = new SaveFileDialog();                   // 保存文件对话框
                saveDialog.DefaultExt = "xls";
                saveDialog.Filter = "Excel文件|*.xls";
                saveDialog.FileName = saveFileName;            if (saveDialog.ShowDialog().Equals(DialogResult.Cancel))            // 单击了'取消'按钮,则返回
                    return;            saveFileName = saveDialog.FileName;                                 // 设置新的保存文件名
                Thread tdSaveRecord = new Thread(new ThreadStart(SaveRecord));      // 启用线程保存数据
                tdSaveRecord.Start();
            }        #region Excel操作中私有的方法
            /// <summary>
            /// 读取信息并保存记录
            /// </summary>
            private static void SaveRecord()
            {
                string strMsg = string.Empty;                                       // 提示信息
                MessageBoxIcon msgIcon = MessageBoxIcon.Information;                // 设置消息框的图标            if (dtDataSource.Rows.Count == 0)
                {
                    MessageBox.Show("没有要保存的数据!", "操作提示", MessageBoxButtons.OK, msgIcon);
                    return;
                }            if (ExportExcel())                                                  // 保存数据至Excel文件中
                {
                    strMsg = "记录导出完毕!";
                }
                else
                {
                    strMsg = "记录导出出错,请重试!";
                    msgIcon = MessageBoxIcon.Error;
                }
                MessageBox.Show(strMsg, "操作提示", MessageBoxButtons.OK, msgIcon);
            }
            /// <summary>
            /// 将 DataTable 数据保存至 Excel 文件中
            /// </summary>
            private static bool ExportExcel()
            {
                if (saveFileName.IndexOf(":") < 0) return false; //被点了取消            Excel.Application xlApp = new Excel.Application();
                object missing = System.Reflection.Missing.Value;            if (xlApp == null)
                {
                    MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                    return false;
                }
                Excel.Workbooks workbooks = xlApp.Workbooks;
                Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];    //取得sheet1
                Excel.Range range;            string strCaption = saveFileName.Remove(0, saveFileName.LastIndexOf('\\') + 1);
                strCaption = strCaption.Remove(strCaption.Length - 4, 4);            long totalCount = dtDataSource.Rows.Count;
                long rowRead = 0;
                float percent = 0;            range = (Excel.Range)worksheet.get_Range(worksheet.Cells[1, 1],
                    worksheet.Cells[2, dtDataSource.Columns.Count]);                    //标题占用前两行
                range.Merge(missing);                                                   //合并
                range.Font.Bold = true;                                                 //粗体设置
                range.Font.Size = 16;                                                   //字体大小设置 
                range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;              //水平对齐设置 
                range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;                //垂直对齐设置
                //range.FormulaR1C1 = 公式;             //公式设置
                //range.ColumnWidth = 宽度;             //列宽设置
                //range.RowHeight = 行高;               //行高  
                worksheet.Cells[1, 1] = strCaption;            //写入字段
                for (int i = 0; i < dtDataSource.Columns.Count; i++)
                {
                    worksheet.Cells[4, i + 1] = dtDataSource.Columns[i].ColumnName;
                    range = (Excel.Range)worksheet.Cells[4, i + 1];
                    range.Interior.ColorIndex = 15;
                    range.Font.Bold = true;
                }            //写入数值
                for (int r = 0; r < dtDataSource.Rows.Count; r++)
                {
                    for (int i = 0; i < dtDataSource.Columns.Count; i++)
                    {
                        worksheet.Cells[r + 5, i + 1] = dtDataSource.Rows[r][i];
                    }
                    rowRead++;
                    percent = ((float)(100 * rowRead)) / totalCount;
                    Application.DoEvents();
                }
                worksheet.SaveAs(saveFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing);            range = worksheet.get_Range(worksheet.Cells[4, 1],
                    worksheet.Cells[dtDataSource.Rows.Count + 4, dtDataSource.Columns.Count]);
                range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
                range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
                range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;            if (dtDataSource.Columns.Count > 1)
                {
                    range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
                }
                workbook.Close(missing, missing, missing);
                xlApp.Quit();            return true;
            }
            #endregion
        }
    }
      

  5.   

    lz建议你用三方控件  我用这个很放便 ,格式自己可以设置,可以导出成多个sheel表   CarlosAg.ExcelXmlWriter.dll
      

  6.   

    http://blog.csdn.net/hartkevindarkhorse/archive/2008/11/14/3289085.aspx
      

  7.   

    可以换DEV ASPX系列的试试 ,用一句话就搞定 
      

  8.   

    NPOI
      

  9.   

    http://blog.csdn.net/davidcoffee/archive/2011/03/30/6290375.aspx