Skip to content

Instantly share code, notes, and snippets.

@tatsuyaueda
Created March 23, 2016 04:42
Show Gist options
  • Save tatsuyaueda/4ed33c3cd3f1ae4f1035 to your computer and use it in GitHub Desktop.
Save tatsuyaueda/4ed33c3cd3f1ae4f1035 to your computer and use it in GitHub Desktop.
Excel 2010 File Format to Excel 2003 File Format(Using COM)
Module Module1
'' このプログラムはExcel 2010形式のファイルをExcel 2003形式に変更するプログラムです。
'' 内部でExcelのCOMオブジェクトを利用するため、実行する環境でExcel 2010形式のファイルを開くことが出来る必要があります。
Sub Main(ByVal CmdArgs() As String)
' ファイル名
Dim strFilePath As String = ""
' Xls変換後ファイル名
Dim strFilePathXls As String = ""
' 自ファイル名
Dim strExeFileName As String = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location)
' 自ファイル バージョン
Dim strFileVersion As String = System.Reflection.Assembly.GetExecutingAssembly().GetName.Version().ToString()
' アプリケーション COMオブジェクト
Dim xlApp As Excel.Application
' Book COMオブジェクト
Dim xlBooks As Excel.Workbooks
Dim xlBook As Excel.Workbook
Try
' Excelのオブジェクトを生成する
xlApp = New Excel.Application
Catch ex As System.Runtime.InteropServices.COMException
Console.WriteLine("==================================================")
Console.WriteLine(ex.Message)
Console.WriteLine(ex.StackTrace)
Console.WriteLine("==================================================")
Console.WriteLine("ExcelのCOMオブジェクトにアクセス出来ません。Excelがインストールされていない可能性があります。")
Console.WriteLine("プログラムを終了します。")
End
End Try
' 引数があるか確認
If CmdArgs.Count = 0 Then
' 引数がなければ利用方法を表示しプログラムを終了
Console.WriteLine("XLSX to XLS Ver." & strFileVersion)
Console.WriteLine("Usage: " & strExeFileName & " 対象ファイル")
End
End If
' 引数がある場合はファイルパスと仮定し、フルパスを取得
strFilePath = System.IO.Path.GetFullPath(CmdArgs(0))
' 拡張子なしのフルパスを取得
strFilePathXls = System.IO.Path.GetDirectoryName(strFilePath) & "\" & _
System.IO.Path.GetFileNameWithoutExtension(strFilePath) & ".xls"
' ファイルの存在を確認
If Not System.IO.File.Exists(strFilePath) Then
Console.WriteLine("指定されたファイルが存在しません。プログラムを終了します。")
End
End If
If CInt(xlApp.Version) < 11 Then
Console.WriteLine("Excel2003 以前の環境では動作しません。プログラムを終了します。")
End
End If
Console.WriteLine("変換元ファイル:" & strFilePath)
Console.WriteLine("変換後ファイル:" & strFilePathXls)
' Excelを開く
xlBooks = xlApp.Workbooks
' Excelワークブックを開く
xlBook = xlBooks.Open(strFilePath)
' Excel 2003形式で保存する
xlApp.DisplayAlerts = False
If CInt(xlApp.Version) > 11 Then
xlBook.SaveAs(strFilePathXls, Excel.XlFileFormat.xlExcel8)
Else
xlBook.SaveAs(strFilePathXls, Excel.XlFileFormat.xlExcel9795)
End If
' Excelを終了し、COMオブジェクト 解放
xlBook.Close(False)
MRComObject(xlBook)
MRComObject(xlBooks)
xlApp.Quit()
MRComObject(xlApp)
Console.WriteLine("ファイルの変換が完了しました。プログラムを終了します。")
End Sub
''' <summary>
''' COM オブジェクトの解放
''' </summary>
''' <param name="objCom"></param>
''' <remarks></remarks>
Public Sub MRComObject(ByRef objCom As Object)
Dim i As Integer
Try
If Not objCom Is Nothing AndAlso System.Runtime.InteropServices.Marshal.IsComObject(objCom) Then
Do
i = System.Runtime.InteropServices.Marshal.ReleaseComObject(objCom)
Loop Until i <= 0
End If
Catch
Finally
objCom = Nothing
End Try
End Sub
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment