2013-05-18 144 views
0

我使用ASP.net MVC 3. 我有這兩個要求。第一個是在我的應用程序中創建發票。我想將數據導出爲pdf,word,excel文件。我下載了itextsharp dll,任何人都可以打電話給我,是否有任何替代數據的ui中的pdf,word和excel文件? 二是我需要點擊打印按鈕後打印文檔。如何將打印機連接到導出文檔中的打印按鈕?如何導出爲pdf,word,excel文件

回答

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#爲PDF例如

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# Excel文件例如

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

Aslo告訴我如何使用打印按鈕連接打印機。 – gs11111

+1

我不認爲它真的可以做到。或者,至少不是那麼簡單。看看這篇文章:http://forums.asp.net/t/1074317.aspx/2/10 – Christian

相關問題