使用C#导出为CSV,TSV,EXcel,TSV for Excel,PDF [英] Export to CSV, TSV, EXcel, TSV for Excel, PDF using C#

查看:167
本文介绍了使用C#导出为CSV,TSV,EXcel,TSV for Excel,PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要导出为以下格式。

请分享一些我可以实现此目的的链接。代码需要在C#中。



•标签CSV格式

•TSV

•excel的TSV

•Excel

•PDF

Need to Export to the below formats.
Please share some link through which i can achieve this. The code needs to be in C#.

• Tab delineated CSV
• TSV
• TSV for excel
• Excel
• PDF

推荐答案

请参阅以下内容:
See the following:
  • To Export Excel
  • To Export PDF


我们做了一个注册表单,如果用户注册,那么详细信息将以pdf格式显示u以将数据保存为硬拷贝,即打印输出...





如果可以,请尝试使用此代码,但根据您的要求进行一些修改,



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.W eb.UI.WebControls;

使用System.Web.Security;

使用MySql.Data.MySqlClient;

使用System.IO;

使用iTextSharp.text.html.simpleparser;

使用iTextSharp.text.pdf;

使用iTextSharp.text;

使用CrystalDecisions.CrystalReports.Engine;

使用CrystalDecisions.Shared;

使用System.Data;

使用BAL;



命名空间补贴

{

公共部分类WebForm1:System.Web.UI.Page

{

DataTable dt;

ClassBAL obj = new ClassBAL();

protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{

string aadhaar = Session [adharno]。ToString(); // 521207150960; //

dt = obj.getfarmerinfo1(aadhaar);



string sno = dt.Rows [0] [SNo]。 ToString();

Session [sno] = sno.ToString();

lbladhar.Text = dt.Rows [0] [AadhaarID]。ToString ();

lbluname.Text = dt.Rows [0] [FarmerName]。ToString();

lblfathername.Text = dt.Rows [0] [ FatherOrHusbandName]。ToString();

lblvillage.Text = dt.Rows [0] [VillageName]。ToString();

lblmandal.Text = dt .Rows [0] [MandalOrDistrict]。ToString();

lblssidno.Text = dt.Rows [0] [SSIDNo]。ToString();

lblsubcast.Text = dt.Rows [0] [CastDetails]。ToString();

lblsurveyno.Text = dt.Rows [0] [LandSurveyNumber]。ToString();

lblpassbookno.Text = dt.Rows [0] [PattaPassbookNumber]。ToString();

l blprocate.Text = dt.Rows [0] [Requiredsubsidycategory]。ToString();

lblproname.Text = dt.Rows [0] [Requiredproduct]。ToString();

lblprice.Text = dt.Rows [0] [ActualPrice]。ToString();

lblsubsidy.Text = dt.Rows [0] [SubsidyPrice]。 ToString();

lblmobile.Text = dt.Rows [0] [MobileNumber]。ToString();



DateTime dtime = new DateTime();

dtime = DateTime.Now;

string DTime = dtime.ToString();

Session [DTime ] = DTime.ToString();

}

}



protected void btnprint_Click(object sender,EventArgs e)

{

Response.ContentType =application / pdf;

Response.AddHeader(content-disposition,attachment; filename = Panel.pdf);

Response.C ache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

pnlPerson.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());

//文档pdfDoc = new Document(PageSize.A4,10f, 10f,100f,0f);

文件pdfDoc =新文件(PageSize.A3,100f,10f,10f,10f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc) ;

PdfWriter.GetInstance(pdfDoc,Response.OutputStream);

pdfDoc.Open();



//// pdf添加到表格式

PdfPTable table1 = new PdfPTable(2);

//空格

string space = \\\
,\\\
;段落para2 =新段落(空格);

para2.Alignment = Element.ALIGN_CENTER; pdfDoc.Add(第2段);

//添加标题

字符串标题= @注册补助详情;

段落=新段落(标题);

para.Alignment = Element.ALIGN_CENTER;

pdfDoc.Add(para);

// DateTime

string dtime = Session [DTime]。ToString();

段para1 = new段落(dtime);

para1.Alignment = Element.ALIGN_RIGHT;

pdfDoc.Add(para1);

//序列号

string SNo =发票编号:;

段落parano =新段落(SNo); parasn​​o.Alignment = Element.ALIGN_LEFT; pdfDoc.Add(parasn​​o);

string sno = Session [sno]。ToString();

段落parasn​​o1 = new段落(sno); parasn​​o1.Alignment = Element.ALIGN_LEFT; pdfDoc.Add(parasn​​o1);



//空格

string space1 =\ n,\ n;段落para3 =新段落(space1);

para3.Alignment = Element.ALIGN_CENTER; pdfDoc.Add(para3);



// cell11

PdfPCell cell11 = new PdfPCell();

cell11.AddElement(新段落(Aadhaar卡号:));

table1.AddCell(cell11);

PdfPCell cell12 = new PdfPCell();

cell12.AddElement(new Paragraph(lbladhar.Text));

table1.AddCell(cell12);

// cell21

PdfPCell cell21 = new PdfPCell();

cell21.AddElement(new Paragraph(Farmer Name:));

table1.AddCell(cell21);

PdfPCell cell22 = new PdfPCell();

cell22.AddElement(new Paragraph(lbluname.Text));

table1.AddCell(cell22);

// cell31

PdfPCell cell31 = new PdfPCell();

cell31.AddElement(new Paragraph(Father / Husband Name:)) );

table1.AddCell(cell31);

PdfPCell cell32 = new PdfPCell();

cell32.AddElement(new Paragraph(lblfathername.Text));

table1.AddCell(cell32);

// cell41

PdfPCell cell41 = new PdfPCell();

cell41。 AddElement(new Paragraph(Village Name:));

table1.AddCell(cell41);

PdfPCell cell42 = new PdfPCell();

cell42.AddElement(new Paragraph(lblvillage.Text));

table1.AddCell(cell42);

// cell51

PdfPCell cell51 = new PdfPCell();

cell51.AddElement(new Paragraph(Mandal / District Name:));

table1.AddCell(cell51);

PdfPCell cell52 = new PdfPCell();

cell52.AddElement(new Paragraph(lblmandal.Text));

table1.AddCell(ce ll52);

// cell61

PdfPCell cell61 = new PdfPCell();

cell61.AddElement(new Paragraph(SSIDNo:) ));

table1.AddCell(cell61);

PdfPCell cell62 = new PdfPCell();

cell62.AddElement(new Paragraph(lblssidno) .Text));

table1.AddCell(cell62);

// cell71

PdfPCell cell71 = new PdfPCell();

cell71.AddElement(new Paragraph(Reservation Category:));

table1.AddCell(cell71);

PdfPCell cell72 = new PdfPCell() ;

cell72.AddElement(new Paragraph(lblsubcast.Text));

table1.AddCell(cell72);

// cell81

PdfPCell cell81 = new PdfPCell();

cell81.AddElement(new Paragraph(Land survey Number:));

table1.AddCell( CE ll81);

PdfPCell cell82 = new PdfPCell();

cell82.AddElement(new Paragraph(lblsurveyno.Text));

table1。 AddCell(cell82);

// cell91

PdfPCell cell91 = new PdfPCell();

cell91.AddElement(new Paragraph(Patta Passbook)编号:));

table1.AddCell(cell91);

PdfPCell cell92 = new PdfPCell();

cell92.AddElement(new段落(lblpassbookno.Text));

table1.AddCell(cell92);

// cell101

PdfPCell cell101 = new PdfPCell();

cell101.AddElement(new Paragraph(Product Category:));

table1.AddCell(cell101);

PdfPCell cell102 = new PdfPCell();

cell102.AddElement(new Paragraph(lblprocate.Text));

table1.AddCell(cell102);

// cell111

PdfPCell cell111 = new PdfPCell();

cell111.AddElement(new Paragraph(Product Name:));

table1.AddCell(cell111);

PdfPCell cell112 = new PdfPCell();

cell112.AddElement(new Paragraph(lblproname.Text));

table1.AddCell(cell112);

// cell121

PdfPCell cell121 = new PdfPCell();

cell121.AddElement(new Paragraph (产品实际价格:));

table1.AddCell(cell121);

PdfPCell cell122 = new PdfPCell();

cell122 .AddElement(new Paragraph(lblprice.Text));

table1.AddCell(cell122);

// cell131

PdfPCell cell131 = new PdfPCell();

cell131.AddElement(new Paragraph(Product Subsidy Price:));

table1.AddCell(c ell131);

PdfPCell cell132 = new PdfPCell();

cell132.AddElement(new Paragraph(lblsubsidy.Text));

table1。 AddCell(cell132);

// cell141

PdfPCell cell141 = new PdfPCell();

cell141.AddElement(new Paragraph(Mobile Number) :));

table1.AddCell(cell141);

PdfPCell cell142 = new PdfPCell();

cell142.AddElement(new Paragraph (lblmobile.Text));

table1.AddCell(cell142);



pdfDoc.Add(table1);



// htmlparser.Parse(sr);

pdfDoc.Close();

// Response.Write(pdfDoc) ;

// Response.End();

}



protected void btnback_Click(object sender,EventArgs e)

{

Response.Redirect (DAOHomePage.aspx);

}

}

}
hi , we have did one register form , if the user register then the details will shows u in pdf format to save the data as hardcopy , i.e print out ...


try this code if u can, but make some modifications as per your requirements,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using MySql.Data.MySqlClient;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.text;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using BAL;

namespace Subsidy
{
public partial class WebForm1 : System.Web.UI.Page
{
DataTable dt;
ClassBAL obj = new ClassBAL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string aadhaar = Session["adharno"].ToString(); //"521207150960"; //
dt = obj.getfarmerinfo1(aadhaar);

string sno = dt.Rows[0]["SNo"].ToString();
Session["sno"] = sno.ToString();
lbladhar.Text = dt.Rows[0]["AadhaarID"].ToString();
lbluname.Text = dt.Rows[0]["FarmerName"].ToString();
lblfathername.Text = dt.Rows[0]["FatherOrHusbandName"].ToString();
lblvillage.Text = dt.Rows[0]["VillageName"].ToString();
lblmandal.Text = dt.Rows[0]["MandalOrDistrict"].ToString();
lblssidno.Text = dt.Rows[0]["SSIDNo"].ToString();
lblsubcast.Text = dt.Rows[0]["CastDetails"].ToString();
lblsurveyno.Text = dt.Rows[0]["LandSurveyNumber"].ToString();
lblpassbookno.Text = dt.Rows[0]["PattaPassbookNumber"].ToString();
lblprocate.Text = dt.Rows[0]["Requiredsubsidycategory"].ToString();
lblproname.Text = dt.Rows[0]["Requiredproduct"].ToString();
lblprice.Text = dt.Rows[0]["ActualPrice"].ToString();
lblsubsidy.Text = dt.Rows[0]["SubsidyPrice"].ToString();
lblmobile.Text = dt.Rows[0]["MobileNumber"].ToString();

DateTime dtime = new DateTime();
dtime = DateTime.Now;
string DTime = dtime.ToString();
Session["DTime"] = DTime.ToString();
}
}

protected void btnprint_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
//Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
Document pdfDoc = new Document(PageSize.A3, 100f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();

////pdf adding to table format
PdfPTable table1 = new PdfPTable(2);
//empty space
string space = "\n,\n"; Paragraph para2 = new Paragraph(space);
para2.Alignment = Element.ALIGN_CENTER; pdfDoc.Add(para2);
//adding Header
string Header = @"REGISTERED SUBSIDY DETAILS";
Paragraph para = new Paragraph(Header);
para.Alignment = Element.ALIGN_CENTER;
pdfDoc.Add(para);
//DateTime
string dtime = Session["DTime"].ToString();
Paragraph para1 = new Paragraph(dtime);
para1.Alignment = Element.ALIGN_RIGHT;
pdfDoc.Add(para1);
//Serial No
string SNo = "Invoice Number :";
Paragraph parasno = new Paragraph(SNo); parasno.Alignment = Element.ALIGN_LEFT; pdfDoc.Add(parasno);
string sno = Session["sno"].ToString();
Paragraph parasno1 = new Paragraph(sno); parasno1.Alignment = Element.ALIGN_LEFT; pdfDoc.Add(parasno1);

//empty space
string space1 = "\n,\n"; Paragraph para3 = new Paragraph(space1);
para3.Alignment = Element.ALIGN_CENTER; pdfDoc.Add(para3);

//cell11
PdfPCell cell11 = new PdfPCell();
cell11.AddElement(new Paragraph("Aadhaar Card Number :"));
table1.AddCell(cell11);
PdfPCell cell12 = new PdfPCell();
cell12.AddElement(new Paragraph(lbladhar.Text));
table1.AddCell(cell12);
//cell21
PdfPCell cell21 = new PdfPCell();
cell21.AddElement(new Paragraph("Farmer Name :"));
table1.AddCell(cell21);
PdfPCell cell22 = new PdfPCell();
cell22.AddElement(new Paragraph(lbluname.Text));
table1.AddCell(cell22);
//cell31
PdfPCell cell31 = new PdfPCell();
cell31.AddElement(new Paragraph("Father/Husband Name :"));
table1.AddCell(cell31);
PdfPCell cell32 = new PdfPCell();
cell32.AddElement(new Paragraph(lblfathername.Text));
table1.AddCell(cell32);
//cell41
PdfPCell cell41 = new PdfPCell();
cell41.AddElement(new Paragraph("Village Name :"));
table1.AddCell(cell41);
PdfPCell cell42 = new PdfPCell();
cell42.AddElement(new Paragraph(lblvillage.Text));
table1.AddCell(cell42);
//cell51
PdfPCell cell51 = new PdfPCell();
cell51.AddElement(new Paragraph("Mandal/District Name :"));
table1.AddCell(cell51);
PdfPCell cell52 = new PdfPCell();
cell52.AddElement(new Paragraph(lblmandal.Text));
table1.AddCell(cell52);
//cell61
PdfPCell cell61 = new PdfPCell();
cell61.AddElement(new Paragraph("S.S.I.D.No :"));
table1.AddCell(cell61);
PdfPCell cell62 = new PdfPCell();
cell62.AddElement(new Paragraph(lblssidno.Text));
table1.AddCell(cell62);
//cell71
PdfPCell cell71 = new PdfPCell();
cell71.AddElement(new Paragraph("Reservation Category :"));
table1.AddCell(cell71);
PdfPCell cell72 = new PdfPCell();
cell72.AddElement(new Paragraph(lblsubcast.Text));
table1.AddCell(cell72);
//cell81
PdfPCell cell81 = new PdfPCell();
cell81.AddElement(new Paragraph("Land Survey Number :"));
table1.AddCell(cell81);
PdfPCell cell82 = new PdfPCell();
cell82.AddElement(new Paragraph(lblsurveyno.Text));
table1.AddCell(cell82);
//cell91
PdfPCell cell91 = new PdfPCell();
cell91.AddElement(new Paragraph("Patta Passbook Number :"));
table1.AddCell(cell91);
PdfPCell cell92 = new PdfPCell();
cell92.AddElement(new Paragraph(lblpassbookno.Text));
table1.AddCell(cell92);
//cell101
PdfPCell cell101 = new PdfPCell();
cell101.AddElement(new Paragraph("Product Category :"));
table1.AddCell(cell101);
PdfPCell cell102 = new PdfPCell();
cell102.AddElement(new Paragraph(lblprocate.Text));
table1.AddCell(cell102);
//cell111
PdfPCell cell111 = new PdfPCell();
cell111.AddElement(new Paragraph("Product Name :"));
table1.AddCell(cell111);
PdfPCell cell112 = new PdfPCell();
cell112.AddElement(new Paragraph(lblproname.Text));
table1.AddCell(cell112);
//cell121
PdfPCell cell121 = new PdfPCell();
cell121.AddElement(new Paragraph("Product Actual Price :"));
table1.AddCell(cell121);
PdfPCell cell122 = new PdfPCell();
cell122.AddElement(new Paragraph(lblprice.Text));
table1.AddCell(cell122);
//cell131
PdfPCell cell131 = new PdfPCell();
cell131.AddElement(new Paragraph("Product Subsidy Price :"));
table1.AddCell(cell131);
PdfPCell cell132 = new PdfPCell();
cell132.AddElement(new Paragraph(lblsubsidy.Text));
table1.AddCell(cell132);
//cell141
PdfPCell cell141 = new PdfPCell();
cell141.AddElement(new Paragraph("Mobile Number :"));
table1.AddCell(cell141);
PdfPCell cell142 = new PdfPCell();
cell142.AddElement(new Paragraph(lblmobile.Text));
table1.AddCell(cell142);

pdfDoc.Add(table1);

// htmlparser.Parse(sr);
pdfDoc.Close();
// Response.Write(pdfDoc);
// Response.End();
}

protected void btnback_Click(object sender, EventArgs e)
{
Response.Redirect("DAOHomePage.aspx");
}
}
}


这篇关于使用C#导出为CSV,TSV,EXcel,TSV for Excel,PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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