2013-05-18 9 views
0

저는 ASP.net MVC 3을 사용합니다. 나는이 두 가지 요구 사항을 가지고 있습니다. 프리스트 (Frist)가 내 신청서에 인보이스를 작성 중입니다. pdf, word, excel 파일로 데이터를 내보내려고합니다. 나는 itextsharp dll을 다운로드했는데, 누구든지 나를 전화를 할 수있는 다른 대안을 dat에 ui에 PDF, 단어 및 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# 엑셀 예를 들어 대한

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

마찬가지로 인쇄 단추를 사용하여 프린터에 연결하는 방법을 알려줍니다. – gs11111

+1

정말 할 수 있다고 생각하지 않습니다. 또는 적어도 그렇게 간단하지는 않습니다. 이 기사를 살펴보십시오. http://forums.asp.net/t/1074317.aspx/2/10 – Christian