Pages - Menu

2016年12月22日 星期四

ReportViwer匯出pdf 或 圖片

     每天定時傳送chart給user,雖然已經寫了前端網站要給使用者查詢,

但卻還是要發送mail…,想了半天,後端要去拉前端的chart實在太難搞了。

最後只有下面四種解法…

1.使用WKhtmlTOpdf ,想直接再後台將網頁轉成pdf後寄出去,但因為有jQuery

的 Flotchart,導致匯出失敗…。

2.將Flotchart轉成圖片,瀏覽器自動下載。然後再寫一隻程式,去監控資料夾,

當檔案有新增時,將檔案擷取出來後再發送mail,似乎是個可行的方案。

3.開啟網頁,擷取桌面,再發送寄出,但有個罩門,如果網頁的資料慢了一點

出現,那擷取桌面就變成了笑話。

4.最後想出最終解,用報表吧…..ReportViwer,將資料丟進去,

產生chart後再轉成pdf後寄出。

於是,產生pdf及圖片的程式碼如下

Public Class RenderToPDF
    Public Sub Save(ByVal viewer As ReportViewer, ByVal savePath As String)
        Dim Bytes() As Byte = viewer.LocalReport.Render("PDF", "", Nothing, Nothing, Nothing, Nothing, Nothing)

        Using Stream As New FileStream(savePath, FileMode.Create)
            Stream.Write(Bytes, 0, Bytes.Length)
        End Using
    End Sub
End Class
Public Class RenderToImage
    Public Sub Save(ByVal rp1 As ReportViewer, ByVal savePath As String)
        Try
            Dim bbytes() As Byte = rp1.LocalReport.Render("Image", "<DeviceInfo><OutputFormat>PNG</OutputFormat></DeviceInfo>")
            My.Computer.FileSystem.WriteAllBytes(savePath, bbytes, False)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
End Class

沒有留言:

張貼留言