goo blog サービス終了のお知らせ 

Visual Studio VB.NET Tips 集

プログラミング集です。
メインはVisual Basic .NET でございます。

エクセルをPDFにしたい

2010-06-01 13:11:25 | 日記
Excel→PDF変換するためのツールがEcPDF2007.exeです。
ダウンロードはこのサイトからできます。
http://online-de.from.tv/dll/dwnlist.html

Excelはバージョン2007です

--------
 概要
--------
VB.NET 用 コンポーネント
ExcelをPDFに変換するアプリケーションです。
------------
 動作環境
------------
VB.NETのクラスライブラリが利用可能な環境
Excelは2007以降が必要です(重要:2007より前のバージョンは対応していません)。
----------------
 インストール
----------------
setup.exeを起動します、「次へ」で自動インストール作業が進んでいきます。
--------------------
 アンインストール
--------------------
コントロールパネルよりEcPDF2007を削除してください。

【使用方法】画面から操作
一個目のテキストボックスにエクセルファイルのフルパスを設定
二個目のテキストボックスにPDFのフルパスを設定
実行ボタンを押します。

【使用方法】コマンドプロンプトから操作
引数1→エクセルファイル名フルパス
引数2→PDF名フルパス
例)
C:\> EcPDF2007.exe "C:\Users\x300\Book1.xlsx" "C:\Users\x300\PdfTest.pdf"

PDFを作成する VB.NET

2010-06-01 08:11:57 | 日記
【PDF】PDF作成コンポーネント(PdfWriterLib.dll)をダウンロードし自作する方法
こちらからPdfWriteLib.dllをダウンロードします。
http://online-de.from.tv/dll/index.html

【コードサンプル】
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button27.Click
'==================
'文字列表示
'==================
Me.Pdf1.PdfSetBodyData(PdfWriterLib.BodyType.Text, "データ001")
'==================
'ヘッダー作成
'==================
Me.Pdf1.PdfSetHeader("ヘッダーです")
'==================
'フッター作成
'==================
Me.Pdf1.PdfSetFooter("フッターです")
'==================
'表(テーブル)作成
'==================
Dim dt As DataTable
dt = GetTable()
Me.Pdf1.PdfSetBodyData(PdfWriterLib.BodyType.Talbe, dt)
'==================
'画像張り付け
'==================
Me.Pdf1.PdfSetBodyData(PdfWriterLib.BodyType.Image, "C:\Users\x300\Dock.jpg")
'==================
'出力
'==================
Me.Pdf1.PdfCreate("pdf.pdf")
End Sub

''' <summary>
''' 表データ作成(サンプル用)
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function GetTable() As DataTable
Dim dt As DataTable
dt = New DataTable
dt.Columns.Add("Field1")
dt.Columns.Add("Field2")
Dim R As DataRow
R = dt.NewRow()
R.Item(0) = "123"
R.Item(1) = "234"
dt.Rows.Add(R)
Return dt
End Function

ExcelをPDFに変換する方法

2010-06-01 07:31:32 | 日記
まず仮想プリンタをインストールします。
以下の4つの仮想プリンタから好きなものを選択してください。
機能は多少違いますがPDF作成は可能です。
■PDFCreator
http://www.pdfforge.org/pdfcreator
PDFCreator
■Bullzip PDF Printer
http://www.bullzip.com/download.php
■Primo PDF
http://www.xlsoft.com/jp/products/primopdf/index.html
PrimoPDF
■CutePDF
http://www.cutepdf.com/

●基本的な動作
プリンタを一時的にPDFプリンタ(仮想プリンタ)に設定、
印刷処理を行う→PDFが出力される
出力後元のプリンタ設定に戻す
'■■■■■■■■■■■■■■■■■■■■■■■■■■
'【Excel操作の設定】
'[プロジェクト]→[参照の追加]→
'→[COM]タブ→[Microsoft Excel *.* ObjectLibrary] (*.*は実際は数値になっています)
' を参照設定する必要があります。
'■■■■■■■■■■■■■■■■■■■■■■■■■■
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Core
'----------------------------------------------
Dim xlApp As Excel.Application
xlApp = New Excel.Application()
xlApp.DisplayAlerts = False
xlApp.Visible = True

Dim xlBooks As Excel.Workbooks = xlApp.Workbooks
Dim xlBook As Excel.Workbook = xlBooks.Open("C:\変換元のエクセル.xls")
Dim xlSheets As Excel.Sheets = xlBook.Sheets
Dim xlSheet As Excel.Worksheet = DirectCast(xlSheets(1), Excel.Worksheet)
xlSheet.PrintOut(ActivePrinter:="ココに上記のプリンタ名")
If Not xlSheet Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet)
End If

If Not xlBook Is Nothing Then
Try
xlBook.Close()
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
End Try
End If

If Not xlApp Is Nothing Then
Try
xlApp.Quit()
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
End Try
End If