如何导出为pdf,word,excel文件

时间:2013-05-18 13:37:11

标签: asp.net-mvc printing invoice export-to-pdf

我使用ASP.net MVC 3。 我有这两个要求。 第一个是在我的申请中创建发票。我想将数据导出为pdf,word,excel文件。我下载了itextsharp dll,任何人都可以告诉我,在ui到pdf,word和excel文件中是否还有其他数据替代方案? 其次,我需要在单击打印按钮后打印文档。如何使用导出文档中的打印按钮连接打印机?

1 个答案:

答案 0 :(得分:2)

您可以使用代码段。

This one很棒。看一看。

这是一个使用示例:

<强> 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();
}