Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
hye, i have made this code which i have tried separately from my current web and it work perfectly but when i combine it with my web, i didn't get the output that was generated in the gridview. here is the code that i had used.
C#
protected void Wordbtn_Click(object sender, EventArgs e)
{
    if (maxdata.Checked == true)
    {
        wordmax();
    }
    if (curdata.Checked == true)
    {
        wordcur();
    }        
}
protected void excelbtn_Click(object sender, EventArgs e)
{
    if (maxdata.Checked == true)
    {
        excelmax();
    }
    if (curdata.Checked == true)
    {
        excelcur();
    }
}
protected void pdfbtn_Click(object sender, EventArgs e)
{
    if (maxdata.Checked == true)
    {
        pdfmax();
    }
    if (curdata.Checked == true)
    {
        pdfcur();
    }
}
public void wordmax()
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=MaxdataExport.doc");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-word ";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    gridmaxdata.AllowPaging = false;
    gridmaxdata.DataBind();
    gridmaxdata.RenderControl(hw);

    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}
public void wordcur()
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=CurrentDataExport.doc");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-word ";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    gridcurdata.AllowPaging = false;
    gridcurdata.DataBind();
    gridcurdata.RenderControl(hw);

    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}
public void excelmax()
{
    Response.Clear();
    Response.Buffer = true;

    Response.AddHeader("content-disposition",
     "attachment;filename=MaxDataExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
   
    gridmaxdata.AllowPaging = false;
    gridmaxdata.DataBind();

    //Change the Header Row back to white color
    gridmaxdata.HeaderRow.Style.Add("background-color", "#FFFFFF");


    gridmaxdata.RenderControl(hw);

    //style to format numbers to string
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}
public void excelcur()
{
    Response.Clear();
    Response.Buffer = true;

    Response.AddHeader("content-disposition",
     "attachment;filename=CurrentdataExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    gridcurdata.AllowPaging = false;
    gridcurdata.DataBind();

    //Change the Header Row back to white color
    gridcurdata.HeaderRow.Style.Add("background-color", "#FFFFFF");


    gridcurdata.RenderControl(hw);
    //---------------------------------------------------------------------------------
    //style to format numbers to string
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}
public void pdfmax()
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=MaxDataExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    gridmaxdata.AllowPaging = false;
    gridmaxdata.DataBind();
    gridmaxdata.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}
public void pdfcur()
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=CurrentdataExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    gridcurdata.AllowPaging = false;
    gridcurdata.DataBind();
    gridcurdata.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

this is the code that i used to create the datagridview
C#
public void maxdatatable()
{
    string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\inetpub\\wwwroot\\webradiation\\App_Data\\Radiation.mdf;Integrated Security=True;User Instance=True";

    //Bind SQLDataSource to GridView for max data
    // Create SQLDataSource.
    SqlDataSource sqlDataSource1 = new SqlDataSource();
    sqlDataSource1.ID = "SqlDataSource123";
    this.Page.Controls.Add(sqlDataSource1);

    // Bind ConnectionString to SQLDataSource.
    sqlDataSource1.ConnectionString = connectionString;

    // Retrieve records
    sqlDataSource1.SelectCommand = "SELECT top 30 [date], [data] FROM [loc1] WHERE (([data] >= '2') AND (([date] >= '" + combdatetime11.ToString() + "') AND ([date] <= '" + combdatetime21.ToString() + "'))) ORDER BY [data] DESC, [date] DESC";

    gridmaxdata.DataSource = sqlDataSource1;
    gridmaxdata.DataBind();
}

public void currdata()
{
    string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\inetpub\\wwwroot\\webradiation\\App_Data\\Radiation.mdf;Integrated Security=True;User Instance=True";
    // Create SQLDataSource.
    SqlDataSource sqlDataSource2 = new SqlDataSource();
    sqlDataSource2.ID = "SqlDataSource12";
    this.Page.Controls.Add(sqlDataSource2);

    // Bind ConnectionString to SQLDataSource.
    sqlDataSource2.ConnectionString = connectionString;

    // Retrieve records 
    sqlDataSource2.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([date] >= '" + combdatetime11.ToString() + "') AND ([date] < '" + combdatetime21.ToString() + "'))";

    gridcurdata.DataSource = sqlDataSource2;
    gridcurdata.DataBind();
}


for the word document, the output that i got is
<div> </div>


the excel is like this
 <style> .textmode { mso-number-format:\@; } </style><div>

<div>


the pdf file i got an error saying that the document does not have pages.

do you not where did i do wrong?
Posted
Updated 1-Jan-14 23:17pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900