tags:

views:

56

answers:

2

So we're using a third-party component for opening up and doing some work on excel sheets. However the tool cannot handle Excel 95 files. Does anyone know of a tool that can convert a Excel 95 file to a later format? It could point to an outside application...something command-line based would be best.

Also: We cannot use Excel COM Interop.

+2  A: 

You might can open it with Microsoft Access Database Engine 2010 and write it out with the DocumentFormat.OpenXml.Packaging.SpreadsheetDocument class to a xlsx file.

Google for ado.Net excel c# and you will find many samples on how to set your connection string and open up the Excel file. (E.g. http://sharpertutorials.com/accessing-excel-spreadsheets-within-c-adonet/)

In theory you should be able to iterate over the sheets and the rows/columns and write them out to the xlsx file in a similar fashion.

Mikael Svenson
+1  A: 

you can use a small Excel helper macro to .Open your 95-source and .SaveAs another format using a command line. You make use of the kernel32/GetCommandLine routine to get the cmdline arg's into your application.

To do this, create a workbook "Converter.xls" and insert the following code

module "ThisWorkbook"

Private Sub Workbook_Open()
Dim CmdRaw As Long, CmdLine As String, Idx As Integer
Dim MyFile As Workbook


    CmdRaw = GetCommandLine
    CmdLine = CmdToSTr(CmdRaw)
    ' Msgbox CmdLine
    Idx = InStr(1, CmdLine, "/e/")
    Set MyFile = Workbooks.Open(Mid(CmdLine, Idx + 3, 99))
    MyFile.SaveAs "Test2003.xls", xlExcel7
    Application.Quit
End Sub

module "Module1"

Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)

Function CmdToSTr(Cmd As Long) As String
Dim Buffer() As Byte
Dim StrLen As Long

   If Cmd Then
      StrLen = lstrlenW(Cmd) * 2
      If StrLen Then
         ReDim Buffer(0 To (StrLen - 1)) As Byte
         CopyMemory Buffer(0), ByVal Cmd, StrLen
         CmdToSTr = Buffer
      End If
   End If
End Function

Finally, call this by command line from the path containing your 95-XLS

C:\Progra~1\Micros~2\OFFICE11\excel Converter.xls /e/test95.xls

test95.xls will be opened and saved as test2003.xls in the xlExcel7 format.

So far the basic tech ... now you can be creative adding a 2nd cmd line argument (seperated by "/" - parse more intelligently, etc etc

Important: Excel must run on "relaxed security" to avoid the Macro warning message at startup - however the warning msg is GREAT as long as you debug (Disable allows you to go back to your code ;-) )

I quick & dirty tested this on Excel2003

Hope that helps

MikeD
If Excel is already installed, then interop would be a better approach, but as was mentioned in the question. Interop is not an option, so I assume installing Excel is not an option either.
Mikael Svenson
The exclusion was for Excel COM Interop (reasons could be: no support to VBA, only C#, .NET framework needed, etc.), not for Excel in total. So here comes a solution where you need Excel and nothing more, plus a technology that let you use it from a command line (batch processing)
MikeD