如何下载为pdf,word [英] how to download as pdf,word

查看:70
本文介绍了如何下载为pdf,word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些标签,我在按下按钮时存储数据我想让用户可以选择他想要下载数据,word,pdf等等?我从哪里开始?

i have some labels where i store data when pressing a button i want to make that the user can choose that he want to download the data in , word, pdf and so an? where do i start?

推荐答案

标签在某种意义上< asp:label xmlns:asp =#unknown>< / asp:标签>





将此添加到.aspx page





labels in the sense <asp:label xmlns:asp="#unknown"></asp:label> ?


add this to .aspx page


<form id="form1" runat="server">
   <asp:Label ID="lblData" runat="server" Text="<p> Because one of the constructors takes a variable number of arguments, you can pass any number of child elements. Of course, each of those child elements can contain their own child elements. For any element, you can add any number of attributes.</p><p>When adding XNode (including XElement) or XAttribute objects, if the new content has no parent, the objects are simply attached to the XML tree. If the new content already is parented, and is part of another XML tree, the new content is cloned, and the newly cloned content is attached to the XML tree. The last example in this topic demonstrates this.</p>"></asp:Label>
   <div>

       <asp:DropDownList ID="ddlChooseDropdown" runat="server">
           <asp:ListItem Text="PDF" Value="pdf" />
           <asp:ListItem Text="WORD" Value="docx" />
       </asp:DropDownList>
       <asp:Button Text="Generate File" ID="btnGenerate" runat="server" OnClick="btnGenerate_OnClick" />
   </div>
   </form>





------------------------- -------------------------------------------------- ---------



将此添加到.aspx.cs页面







protected void btnGenerate_OnClick(object sender,EventArgs e)

$



string sDirPath = Server.MapPath(〜/ PDF_WORD / Docs);

DirectoryInfo ObjSearchDir = new DirectoryInfo( sDirPath);

if(!ObjSearchDir.Exists)

{

ObjSearchDir.Create();

}

string strFileName = HttpContext.Current.Server.MapPath(〜/ PDF_WORD / Docs / Test。+ ddlChooseDropdown.SelectedValue.ToString()+);

FileInfo objfileinfo = new FileInfo(strFileName);

//第1步:创建文档对象

var document = new Document();

if(objfileinfo.Exists)

{

File.Delete(strFileName);

PdfWriter.GetInstance(document,new FileStream(strFileName,FileMode.Create));

}

else

PdfWriter.GetInstance(document,new FileStream(strFileName,FileMode.Create));

//第2步:

//我们创建一个侦听文档的编写器

// StringBuilder strB = new StringBuilder();

StringWriter sw = new StringWriter ();



document.Open();

HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);







lblData.RenderControl(hTextWriter);

String strHtml 1 = sw.ToString();







iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();

iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);

hw.Parse(new StringReader(strHtml1));



document.Close();



}



----------------------------- -------------------------------------------------- -----





需要为pdf创建添加itextsharp.dll



------------------------------------------------------------------------------------

add this to .aspx.cs page



protected void btnGenerate_OnClick(object sender, EventArgs e)
{

string sDirPath = Server.MapPath("~/PDF_WORD/Docs");
DirectoryInfo ObjSearchDir = new DirectoryInfo(sDirPath);
if (!ObjSearchDir.Exists)
{
ObjSearchDir.Create();
}
string strFileName = HttpContext.Current.Server.MapPath("~/PDF_WORD/Docs/Test." + ddlChooseDropdown.SelectedValue.ToString() + "");
FileInfo objfileinfo = new FileInfo(strFileName);
// step 1: creation of a document-object
var document = new Document();
if (objfileinfo.Exists)
{
File.Delete(strFileName);
PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
}
else
PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
// step 2:
// we create a writer that listens to the document
// StringBuilder strB = new StringBuilder();
StringWriter sw = new StringWriter();

document.Open();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);



lblData.RenderControl(hTextWriter);
String strHtml1 = sw.ToString();



iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(strHtml1));

document.Close();

}

------------------------------------------------------------------------------------


Need to add itextsharp.dll for pdf creation


add所有这些陈述到cs页面



add all these statements to cs page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
using System.Text.RegularExpressions;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.html;
using iTextSharp.text.xml;


这篇关于如何下载为pdf,word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆