1

Can anyone point me to some examples of Excel Interop usage within VB.NET?

I'm trying to open a select delimited file (.csv) and save it as an Excel XLSX file.

I've used Interop with Word but finding similar coding isn't the same.

When I convert to the XLSX I need to convert the fields to text to preserve leading zero's.

Any insight is appreciated..

The code below opens the csv file specified and saves it as an XLS. Now working to get all fields to import as text.

CODE BEING USED :

Dim newfile As String = System.IO.Path.GetDirectoryName(tbSingleFile.Text) & "\" & System.IO.Path.GetFileNameWithoutExtension(tbSingleFile.Text) & ".xls"
If System.IO.File.Exists(newfile) Then
    IO.File.Delete(newfile)
End If

Dim oExcelFile As Object

Try
    oExcelFile = GetObject(, "Excel.Application")
Catch
    oExcelFile = CreateObject("Excel.Application")
End Try

If xDisplayExcel.Checked = True Then
    oExcelFile.Visible = True
Else
    oExcelFile.Visible = False
End If

oExcelFile.Workbooks.Open(tbSingleFile.Text)
oExcelFile.displayalerts = False

oExcelFile.ActiveWorkbook.SaveAs(Filename:=newfile, FileFormat:=Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, CreateBackup:=False)

oExcelFile.ActiveWorkbook.Close(SaveChanges:=True)

oExcelFile.DisplayAlerts = True
oExcelFile.Quit()
oExcelFile = Nothing
4
  • VB.NET Excel Interop Example Apr 1, 2014 at 2:06
  • Got it where I can open the .csv file and save it as an XLS file but working on the following: - Setting fields as text when opening so numbers come in as they are - Saving as an XLSX file Apr 1, 2014 at 2:23
  • Microsoft itself recommends not to use Interop for office automation, use codeplex.com to search for other 3rd party tools that may help you in achieving the same without office support installed on the system or server Apr 1, 2014 at 3:31
  • I've been successfully able to open a CSV and output it as an XLSX type but still struggling with the FieldInfo:= parameter. CodePlex didn't have much along the lines of what I was looking for. I've got all other coding completed except this one function.. Apr 1, 2014 at 18:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.