无法将多页gridview数据导出为ex​​cel

时间:2013-07-12 05:34:59

标签: c# asp.net excel gridview export

我在将所有数据导出到Excel文件时遇到问题。我只设法从gridview的第一页导出数据。我是否知道导出数据的正确方法,包括其他页面中的数据?非常感谢。

    protected void bn_export_Click(object sender, EventArgs e)
{

    Response.Clear();
    Response.Buffer = true;

    Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.ms-excel";

    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    gv_getGameDetails.AllowPaging = false;


    //Change the Header Row back to white color
    gv_getGameDetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
    gv_getGameDetails.HeaderRow.Cells[0].Style.Add("background-color", " #262626");
    gv_getGameDetails.HeaderRow.Cells[1].Style.Add("background-color", " #262626");
    gv_getGameDetails.HeaderRow.Cells[2].Style.Add("background-color", " #262626");
    gv_getGameDetails.HeaderRow.Cells[3].Style.Add("background-color", " #262626");



    this.RemoveControls(gv_getGameDetails);
    gv_getGameDetails.RenderControl(hw);


    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助。它重复所有列和行并导出到Excel工作表。您需要添加Microsoft.Office.Interop.Excel

private void button2_Click(object sender, EventArgs e)
    {
        // creating Excel Application
        Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
        // creating new WorkBook within Excel application
        Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
        // creating new Excelsheet in workbook
        Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
        // see the excel sheet behind the program
        app.Visible = true;
        // get the reference of first sheet. By default its name is Sheet1.
        // store its reference to worksheet
        try
        {
             //Fixed:(Microsoft.Office.Interop.Excel.Worksheet)
            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
            // changing the name of active sheet
            worksheet.Name = "YourChosenSheetName";
            // storing header part in Excel
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }
            // storing Each row and column value to excel sheet
                 for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
            }

            // save the application
            string fileName = String.Empty;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "Excel files |*.xls|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFileDialog1.FileName;
                //Fixed-old code :11 para->add 1:Type.Missing
                workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            }
            else
                return;               

            // Exit from the application
            //app.Quit();
        }
        catch (System.Exception ex)
        {

        }
        finally
        {
            app.Quit();
            workbook = null;
            app = null;
        }
    }