0

I use ASP.net MVC 3. I have these two requirement. Frist one is creating invoice in my application. I want to export the datas to pdf , word , excel file. I downloaded itextsharp dll, can anyone tel me is there anyother alternative to datas in the ui to pdf, word and excel document? Second is I need print the document after clicking the print button. How to connect printer with the print button in the exported document ?

gs11111
  • 649
  • 2
  • 17
  • 49

1 Answers1

2

You can use a snippet.

This one is great. Take a look on it.

This is an example of usage:

HTML

<asp:GridView ID="GridView1" runat="server"
  AutoGenerateColumns = "false" Font-Names = "Arial"
  Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
  HeaderStyle-BackColor = "green" AllowPaging ="true"  
  OnPageIndexChanging = "OnPaging" >
 <Columns>
  <asp:BoundField ItemStyle-Width = "150px" DataField = "CustomerID"
  HeaderText = "CustomerID" />
<asp:BoundField ItemStyle-Width = "150px" DataField = "City"
  HeaderText = "City"/>
<asp:BoundField ItemStyle-Width = "150px" DataField = "Country"
  HeaderText = "Country"/>
<asp:BoundField ItemStyle-Width = "150px" DataField = "PostalCode"
  HeaderText = "PostalCode"/>
 </Columns>
</asp:GridView>

C# for pdf example

protected void btnExportPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
 "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.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(); 
}

C# for excel example

protected void btnExportExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;

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

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

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

//Apply style to Individual Cells
GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");  

for (int i = 0; i < GridView1.Rows.Count;i++ )
{
GridViewRow row = GridView1.Rows[i];

//Change Color back to white
row.BackColor = System.Drawing.Color.White;

//Apply text style to each Row
row.Attributes.Add("class", "textmode");

//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
    row.Cells[0].Style.Add("background-color", "#C2D69B");
    row.Cells[1].Style.Add("background-color", "#C2D69B");
    row.Cells[2].Style.Add("background-color", "#C2D69B");
    row.Cells[3].Style.Add("background-color", "#C2D69B");  
}
}
GridView1.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();
}