| re: ASP.NET with Excel and Database
Howdy Bob,
|| Is there any example code from coverting a csv
|| file to an excel sheet?
I reckon there is. :-)
<code>
Imports System.Runtime.InteropServices
Public Const xlNormal As Integer = -4143
Public Sub LetsChangeThatFile
Dim oExcel As Excel.Application
Dim oWorkSheet As Worksheet
Try
oExcel = New Excel.Application
oExcel.Visible = True 'Optional but good for debugging.
oExcel.Workbooks.Open ("C:\Tmp\Foo.csv")
oWorksheet = DirectCast (oExcel.ActiveSheet, Excel.Worksheet)
oWorksheet.SaveAs ("C:\Tmp\Foo.xls", xlNormal)
oExcel.Quit
Catch
Dim Up As New Exception ("Bad file - Uuurghh")
Throw Up
Finally
If oWorksheet Is Nothing = False Then
Marshal.ReleaseComObject (oWorksheet)
oExcel.Quit 'oExcel will be valid here.
End If
If oExcel Is Nothing = False Then
Marshal.ReleaseComObject (oExcel)
End If
End Try
End Sub
</code>
As you san see it's only about three lines of useful code - but with a lot
of packaging. You'll get an Excel dialogue about overwriting if the target
file already exists.
Regards,
Fergus |