如何使用itextsharp使用sql server数据从c#windows窗体创建/导出PDF报告 [英] how to create /export PDF report from c# windows forms with sql server data using itextsharp
问题描述
我的名字在过去的10天里是令人头疼的,我突然想到如何使用itextsharp从c#windows表单和sql server数据创建PDF报告。
我在sql server 2008中有两个表。
表名:技术员
列名DataType AllowNulls
technician_id(主键)Int No
technician_dob date是
technician_sex nvarchar(10)是
row_upd_date datetime是
user_id Int是
表名:Techniciandetail
列名DataType AllowNulls
Technician_first_name为nvarchar(50)是
Technician_middle_name为nvarchar(50)是
Technician_last_name为nvarchar(50)是
ロw_upd_date datetime是
状态位是
agn(主键)Int No
technician_id Int Yes
user_id Int是
technician_type Int是
下面给出的是使用itextsharp的sql server 2008的c#代码:
hi my name is vishal for past 10 days i was breaking my head on how to create PDF report from c# windows forms with sql server data using itextsharp.
I have two tables in sql server 2008.
table name: Technician
Column Name DataType AllowNulls
technician_id(primary key) Int No
technician_dob date Yes
technician_sex nvarchar(10) Yes
row_upd_date datetime Yes
user_id Int Yes
table name:Techniciandetail
Column Name DataType AllowNulls
Technician_first_name nvarchar(50) Yes
Technician_middle_name nvarchar(50) Yes
Technician_last_name nvarchar(50) Yes
row_upd_date datetime Yes
status bit Yes
agn(primary key) Int No
technician_id Int Yes
user_id Int Yes
technician_type Int Yes
Given below is my c# code with sql server 2008 using itextsharp:
<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace DRRS_CSharp
{
public partial class frmPDFTechnician : Form
{
public frmExcelTechnician()
{
InitializeComponent();
}
public DataTable GetDataTable()
{
DataTable table = new DataTable("Techniciandetail" + "Technician");
DataColumn datacolumn_ID = new DataColumn("ID", typeof(Int32));
table.Columns.Add(datacolumn_ID);
DataColumn dataFname = new DataColumn("First Name", typeof(string));
table.Columns.Add(dataFname);
DataColumn dataMname = new DataColumn("Middle Name", typeof(string));
table.Columns.Add(dataMname);
DataColumn dataLname = new DataColumn("Last Name", typeof(string));
table.Columns.Add(dataLname);
DataColumn DOB = new DataColumn("DOB", typeof(DateTime));
table.Columns.Add(DOB);
DataColumn Sex = new DataColumn("Gender", typeof(string));
table.Columns.Add(Sex);
DataColumn Type = new DataColumn("Designation", typeof(Int32));
table.Columns.Add(Type);
DataRow drow = table.NewRow();
string connect = "Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true";
SqlConnection conn = new SqlConnection(connect);
string query = "Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
drow["ID"] = rdr[0].ToString();
drow["First Name"] = rdr[1].ToString();
drow["Middle Name"] =rdr[2].ToString();
drow["Last Name"] = rdr[3].ToString();
drow["DOB"] = rdr[4].ToString();
drow["Gender"] = rdr[5].ToString();
drow["Designation"] = rdr[6].ToString();
}
table.AcceptChanges();
return table;
}
private void btnExport_Click(object sender, EventArgs e)
{
Document doc = new Document(PageSize.A4.Rotate());
PdfWriter.GetInstance(doc, new FileStream("Technician22.pdf", FileMode.Create));
doc.Open();
Font font10 = FontFactory.GetFont("Arial", 10, BaseColor.BLACK);
Paragraph para = new Paragraph("Technician Details in PDF Document");
DataTable dt = GetDataTable();
PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPCell cell = null;
cell = new PdfPCell(new Phrase(new Chunk("ID", font10)));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("First Name", font10)));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("Middle Name", font10)));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("Last Name", font10)));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("DOB", font10)));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("Gender", font10)));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("Designation", font10)));
table.AddCell(cell);
for (int rows = 0; rows < dt.Rows.Count; rows++)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
cell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font10)));
table.AddCell(cell);
}
}
table.SpacingBefore = 15f;
doc.Add(para);
doc.Add(table);
doc.Close();
}
下面给出的是我的sql select查询:
选择t.technician_id,td.Technician_first_name,td .Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id = t.technician_id and td.status = 1;
上面的查询在某种程度上执行,因为我在PDF报告的表格中获得了列名。但问题是我只获得了列名,而在下面它没有被sql数据填充。我知道在我的编码中公共DataTable GetDataTable()函数有一些错误,但我不知道如何修复它?
我想要的是当用户点击btnExport时我需要来自技术人员table-technician_id,technician_dob,technician_sex的数据/值以及来自Techniciandetail table-Technician_first_name,Technician_middle_name,Technician_last_name和technician_type 的值在PDF报告中出现或嵌入或绑定到适当的列。
任何人都可以帮助我!我试过网但没有成功。任何有关解决此问题的帮助或指导都将非常感激。
Given below is my sql select query:
"Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1";
The above query executes to some extent as i get column names in table in PDF report. But the problem is that i get column names only and below it is not being filled by sql data. I know there is some mistake in public DataTable GetDataTable() function in my coding but i dont know how to fix it?
What i want is when user click btnExport i want datas/values from Technician table-technician_id,technician_dob,technician_sex and values from Techniciandetail table-Technician_first_name,Technician_middle_name,Technician_last_name and technician_type to be present or embedded or binded into appropriate columns in PDF Report.
Can anyone help me please! I have tried through net but with no success. Any help or guidance in solving this problem would be greatly appreciated.
推荐答案
您可以按照此链接获取代码中的帮助,如果您仍然卡在某处然后发布您的问题这里。
http://www.aspsnippets.com/Articles/How-to-generate-and-download-PDF-Report-from-database- in-ASPNet-using-iTextSharp-C-and-VBNet.aspx [ ^ ]
you can follow this link for help in your code, if you still stuck somewhere then post your problem here.
http://www.aspsnippets.com/Articles/How-to-generate-and-download-PDF-Report-from-database-in-ASPNet-using-iTextSharp-C-and-VBNet.aspx[^]
private void btnExport_Click(object sender, EventArgs e)
{
string SqlQuery = "select * from Techniciandetail";
SqlConnection conn = new SqlConnection("Data Source=WIN-A90OU0EVN8T;Initial Catalog=snsimpexpdb;User ID=sa;Password=sa");
conn.Open();
SqlCommand cmd = new SqlCommand(SqlQuery,conn);
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/pdfTechnician.pdf";
Document DC = new Document(PageSize.A4, 25, 25, 30, 30);
FileStream FS = File.Create(path);
PdfWriter.GetInstance(DC, FS);
DC.Open();
while (dr.Read())
{
SqlQuery = dr[0].ToString() + ' ' + dr[1].ToString() + ' ' + dr[2].ToString() + ' ' + dr[3].ToString() + ' ' + dr[4].ToString() + ' ' + dr[5].ToString() +' '+ dr[6].ToString()+' '+dr[7].ToString() +' '+ dr[8].ToString();
DC.Add(new Paragraph(SqlQuery));
}
DC.Close();
}
//R.RAJESH
//R.RAJESH
这篇关于如何使用itextsharp使用sql server数据从c#windows窗体创建/导出PDF报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!