首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何导出到pdf、word、excel文件

如何导出到pdf、word、excel文件
EN

Stack Overflow用户
提问于 2013-05-18 13:37:10
回答 1查看 4.9K关注 0票数 0

我使用ASP.net MVC 3,我有这两个需求。首先,在我的应用程序中创建发票。我想导出数据到pdf,word,excel文件。我下载了itextsharp,有人能告诉我,除了ui到pdf、word和excel文档中的数据,还有其他的替代方法吗?第二,我需要在单击“打印”按钮后打印文档。如何将打印机与导出文档中的“打印”按钮连接?

EN

回答 1

Stack Overflow用户

发布于 2013-05-18 13:41:44

你可以用一个片段。

This one很棒。看一看。

这是使用的一个例子:

代码语言:javascript
复制
<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#为例

代码语言:javascript
复制
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(); 
}

excel示例的C#

代码语言:javascript
复制
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();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16624875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档