0

すべてのデータを Excel ファイルにエクスポートする際に問題があります。グリッドビューの最初のページからデータをエクスポートすることしかできませんでした。他のページも含めてデータをエクスポートする正しい方法を教えてください。本当にありがとう。

    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();
4

1 に答える 1

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;
        }
    }
于 2013-07-12T09:06:02.550 に答える