locked
export to excel RRS feed

  • Question

  • Hi,

    I am using ascx control. in that i placed one gridview(data is coming from another ascx control). i need to export that data into an excel sheet.

    But , i am getting 

    'GridView' must be placed inside a form tag with runat=server.

    For that i am using  

    public override void VerifyRenderingInServerForm(Control control) { return; }

    But Error is "'UserControls_ASSet_Search_Results.VerifyRenderingInServerForm(System.Web.UI.Control)': no suitable method found to override "

    Can anyone plz help in this.?


    • Edited by saidireddy.v Wednesday, January 7, 2015 5:24 AM
    • Moved by Kristin Xie Thursday, January 15, 2015 9:09 AM asp.net related
    Wednesday, January 7, 2015 5:23 AM

Answers

All replies

  • public override void VerifyRenderingInServerForm(Control control)
    {
    }

    Just include VerifyRenderingInServerForm without any code inside it.

    It all Happenz Sendil

    Wednesday, January 7, 2015 7:15 AM
  • public override void VerifyRenderingInServerForm(Control control)
            {
                /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
                   server control at run time. */
                VerifyRenderingInServerForm();
            }

    No luck

    Still getting same error. 

    Thank you for reading my question. give me any other suggestion.

    Wednesday, January 7, 2015 7:22 AM
  • Copy all of your code to identify the issue

    It all Happenz Sendil

    Wednesday, January 7, 2015 8:24 AM
  • Please see this sample.

    // Export to Excel with formatting . . .
            private void button5_Click(object sender, EventArgs e)
            {
                const int WORKSHEETSTARTROW = 1;
                const int WORKSHEETSTARTCOL = 1;
    
                var excelApp = new Excel.Application();
                excelApp.Visible = true;
                Excel.Workbook excelbk = excelApp.Workbooks.Add(Type.Missing);
                Excel.Worksheet xlWorkSheet1 = (Excel.Worksheet)excelbk.Worksheets["Sheet1"];
                int worksheetRow = WORKSHEETSTARTROW;
                for (int rowCount = 0; rowCount < dataGridView1.Rows.Count - 1; rowCount++)
                {
                    int worksheetcol = WORKSHEETSTARTCOL;
                for (int colCount = 0; colCount < dataGridView1.Columns.Count - 1; colCount++)
                {
                    Excel.Range xlRange = (Excel.Range)xlWorkSheet1.Cells[WORKSHEETSTARTROW, worksheetcol];
                    xlRange.Value2 = dataGridView1.Columns[colCount].Name;
                    worksheetcol += 1;
                
    
                        if (dataGridView1.Rows[rowCount].Cells[colCount].Style.Font != null)
                        {
                            xlRange.Font.Bold = dataGridView1.Rows[rowCount].Cells[colCount].Style.Font.Bold;
                            xlRange.Font.Italic = dataGridView1.Rows[rowCount].Cells[colCount].Style.Font.Italic;
                            xlRange.Font.Underline = dataGridView1.Rows[rowCount].Cells[colCount].Style.Font.Underline;
                            xlRange.Font.FontStyle = dataGridView1.Rows[rowCount].Cells[colCount].Style.Font.FontFamily;
                        }
                        worksheetcol += 1;
                    }
                    worksheetRow += 1;
    
                }
            }
    

    Also, see this one.

    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 worksheet = workbook.Sheets["Sheet1"]; worksheet = workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Exported from gridview"; // 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 workbook.SaveAs("c:\\output.xls",Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive , Type.Missing, Type.Missing, Type.Missing, Type.Missing); // Exit from the application app.Quit(); }

    using System;

    using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; using Excel = Microsoft.Office.Interop.Excel; using System.Data.SqlClient; namespace WindowsFormsApplication6 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // ALL CODE HERE } }


    Make sure you use the right setup, as described directly above . . .


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Sunday, January 11, 2015 12:22 AM
  • Use this one Export Gridview Data to Excel in ASP.NET

    http://www.codeproject.com/Tips/477436/Export-Gridview-Data-to-Excel-in-ASP-NET

    chanmm


    chanmm

    • Proposed as answer by chanmmMVP Sunday, January 11, 2015 1:58 AM
    • Marked as answer by Just Karl Wednesday, April 15, 2015 8:57 PM
    Sunday, January 11, 2015 1:52 AM
  • Hello,

    In addition to chanmm's answer, this should be asked in Microsoft's ASP.Net forums:

    http://forums.asp.net/

    As Microsoft's ASP.Net forums are on a different platform, we cannot move the question for you.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Wednesday, April 15, 2015 8:57 PM