Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > ปัญหาการ export ข้อมูลจาก datagridview ไป pdf ด้วย itextsharp แล้วภาษาไทยหายครับ


 

[.NET] ปัญหาการ export ข้อมูลจาก datagridview ไป pdf ด้วย itextsharp แล้วภาษาไทยหายครับ

 
Topic : 122086



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์



คือต้องการนำข้อมูลจากตาราง datagrid view ในรูป

ในที่นี้ใช้ itextsharp มาช่วยด้วย



ให้กดปุ่ม ปริ้น แล้วออกมาเป็น ไฟล์ pdf แต่ว่าภาษาไทยหายไปดังรูปครับ



เลยอยากทราบวิธีแก้ครับ

หรือถ้าไม่ได้จริงๆ ให้ออกมาเป็น excel ก็ได้ครับ

นี่ code ครับ

Code (VB.NET)
001.Imports System.Data.SqlClient
002.Imports iTextSharp.text
003.Imports iTextSharp.text.pdf
004.Imports System.IO
005.Imports System.Text
006.Imports iTextSharp.text.html
007.Imports iTextSharp.text.html.simpleparser
008. 
009. 
010. 
011.Public Class frm_print_last
012. 
013.    Friend cn As New SqlConnection("Data source=.\SQLExpress; Initial Catalog=Inventory_Project;Integrated Security=sspi;")
014.    Friend cmd As New SqlCommand
015.    Friend DA As New SqlDataAdapter
016.    Friend DS As New DataSet
017.    Friend sql As String
018. 
019. 
020. 
021.    Public Sub open_database()
022. 
023.        If cn.State = ConnectionState.Closed Then cn.Open()
024. 
025.    End Sub
026. 
027.    Friend Sub load_print()
028. 
029.        open_database()
030.        sql = "select * from Finishgood where สถานะการสั่ง='สั่ง'"
031.        DA = New SqlDataAdapter(sql, cn)
032.        DS = New DataSet
033.        DA.Fill(DS, "table")
034.        datagrid_print.DataSource = DS.Tables("table")
035. 
036.    End Sub
037. 
038. 
039. 
040.    Private Sub frm_print_last_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
041.        load_print()
042. 
043. 
044.        Timer1.Start()
045. 
046.    End Sub
047. 
048.    Friend Sub add_date()
049.        Dim i As Integer = datagrid_print.CurrentRow.Index
050.        Dim d1 As Date = Now.ToShortDateString
051.        Dim fg_datesum As Date = datagrid_print.Item(15, i).Value
052. 
053.        'if fg_datesum = d1 and สถานะการสั่ง = 'สั่ง' then
054.        'elseif fg_datesum <> d1 and สถานะการสั่ง = 'สั่ง' then
055. 
056.        sql = "UPDATE Finishgood set fg_datesum = CASE WHEN (fg_datesum = '" & d1 & "' and สถานะการสั่ง = 'สั่ง') THEN (DATEADD(dd,เวลาระหว่างการสั่ง,fg_datesum)) WHEN (fg_datesum <> '" & d1 & "' and สถานะการสั่ง = 'สั่ง') THEN (DATEADD(dd,0,fg_datesum)) END"
057. 
058.        cmd = New SqlCommand(sql, cn)
059.        cmd.ExecuteNonQuery()
060. 
061. 
062. 
063.    End Sub
064. 
065.    Friend Sub return_status()
066. 
067.        'if สถานะการสั่ง = 'สั่ง' and นโยบายการสั่งสินค้า = 'T,Q' then
068.        'elseif สถานะการสั่ง = 'สั่ง' and นโยบายการสั่งสินค้า = 'T,M' then
069.        'elseif สถานะการสั่ง = 'รอการสั่ง' and นโยบายการสั่งสินค้า = 'T,Q' then
070.        'elseif สถานะการสั่ง = 'รอการสั่ง' and นโยบายการสั่งสินค้า = 'T,M' then
071.        'elseif สถานะการสั่ง = 'สั่ง' and นโยบายการสั่งสินค้า = 'S,Q' then
072.        'elseif สถานะการสั่ง = 'สั่ง' and นโยบายการสั่งสินค้า = 'S,M' then
073.        'elseif สถานะการสั่ง = 'รอการสั่ง' and นโยบายการสั่งสินค้า = 'S,Q' then
074.        'elseif สถานะการสั่ง = 'รอการสั่ง' and นโยบายการสั่งสินค้า = 'S,M' then
075.        'end if
076. 
077. 
078.        open_database()
079. 
080.        sql = "update Finishgood set สถานะการสั่ง= CASE WHEN ((สถานะการสั่ง= 'สั่ง') AND (นโยบายการสั่งสินค้า = 'T,Q')) THEN 'รอการสั่ง' WHEN ((สถานะการสั่ง= 'สั่ง') AND (นโยบายการสั่งสินค้า = 'T,M')) THEN 'รอการสั่ง' WHEN ((สถานะการสั่ง= 'รอการสั่ง') AND (นโยบายการสั่งสินค้า = 'T,Q')) THEN 'รอการสั่ง' WHEN ((สถานะการสั่ง= 'รอการสั่ง') AND (นโยบายการสั่งสินค้า = 'T,M')) THEN 'รอการสั่ง' WHEN ((สถานะการสั่ง= 'สั่ง') AND (นโยบายการสั่งสินค้า = 'S,Q')) THEN 'รอรับสินค้า' WHEN ((สถานะการสั่ง= 'สั่ง') AND (นโยบายการสั่งสินค้า = 'S,M')) THEN 'รอรับสินค้า' WHEN ((สถานะการสั่ง= 'รอการสั่ง') AND (นโยบายการสั่งสินค้า = 'S,Q')) THEN 'รอการสั่ง' WHEN ((สถานะการสั่ง= 'รอการสั่ง') AND (นโยบายการสั่งสินค้า = 'S,M')) THEN 'รอการสั่ง' END"
081.        cmd = New SqlCommand(sql, cn)
082.        cmd.ExecuteNonQuery()
083. 
084.    End Sub
085. 
086.    Friend Sub save_pdf()
087.        'Creating iTextSharp Table from the DataTable data
088.        Dim pdfTable As New PdfPTable(datagrid_print.ColumnCount)
089.        pdfTable.DefaultCell.Padding = 3
090.        pdfTable.WidthPercentage = 30
091.        pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
092.        pdfTable.DefaultCell.BorderWidth = 1
093. 
094.        'Adding Header row
095.        For Each column As DataGridViewColumn In datagrid_print.Columns
096.            Dim cell As New PdfPCell(New Phrase(column.HeaderText))
097.            cell.BackgroundColor = New iTextSharp.text.Color(240, 240, 240)
098.            pdfTable.AddCell(cell)
099. 
100.        Next
101. 
102.        'Adding DataRow
103.        For Each row As DataGridViewRow In datagrid_print.Rows
104.            For Each cell As DataGridViewCell In row.Cells
105.                pdfTable.AddCell(cell.Value.ToString())
106.            Next
107.        Next
108. 
109.        'Exporting to PDF
110.        Dim folderPath As String = "C:\PDFs\"
111.        If Not Directory.Exists(folderPath) Then
112.            Directory.CreateDirectory(folderPath)
113.        End If
114.        Using stream As New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
115.            Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
116.            PdfWriter.GetInstance(pdfDoc, stream)
117.            pdfDoc.Open()
118.            pdfDoc.Add(pdfTable)
119.            pdfDoc.Close()
120.            stream.Close()
121.        End Using
122. 
123.    End Sub
124. 
125. 
126. 
127.    Friend Sub open_file()
128. 
129.        Dim Path As String = Application.ExecutablePath
130.        Path = "C:\PDFs\DataGridViewExport.pdf"
131.        Dim myProcess As System.Diagnostics.Process = New Process
132.        myProcess.StartInfo.FileName = Path
133.        myProcess.Start()
134. 
135.    End Sub
136. 
137.    Private Sub btn_print_Click(sender As System.Object, e As System.EventArgs) Handles btn_print.Click
138.        return_status()
139.        add_date()
140. 
141.        save_pdf()
142.        open_file()
143. 
144.    End Sub
145. 
146. 
147. 
148.    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
149.        Label2.Text = Now.ToShortDateString
150.    End Sub
151.End Class




Tag : .NET, Ms SQL Server 2008, VB.NET, VS 2010 (.NET 4.x), Windows



ประวัติการแก้ไข
2016-03-23 01:41:47
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-03-23 01:40:39 By : elanwalker View : 1710 Reply : 2
 

 

No. 1



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ลองหาวิธีแปลงให้เป็น UTF-8 ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-03-23 09:59:20 By : mr.win
 

 

No. 2



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2016-03-23 09:59:20
รายละเอียดของการตอบ ::
มีตัวอย่างการแปลงไหมครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-03-23 15:47:02 By : elanwalker
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ปัญหาการ export ข้อมูลจาก datagridview ไป pdf ด้วย itextsharp แล้วภาษาไทยหายครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่