Microsoft.Office.Interop.Excel - How to save Excel (Visible cells only) to CSV in vb.net - vb.net

Currently I can save Excel to CSV as code below.
However, it's save all data which is including invisible cells.
Dim excelApplication As Microsoft.Office.Interop.Excel.Application
Dim workbook As Microsoft.Office.Interop.Excel.Workbook
excelApplication = New Microsoft.Office.Interop.Excel.Application
excelApplication.Visible = False
excelApplication.DisplayAlerts = False
workbook = excelApplication.Workbooks.Open(in_InputFilePath)
workbook.SaveAs(in_OutputFilePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV)
workbook.Close()
excelApplication.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication)

Dim excelApplication As Microsoft.Office.Interop.Excel.Application
Dim workbook1 As Microsoft.Office.Interop.Excel.Workbook
Dim workbook1worksheet1 As Microsoft.Office.Interop.Excel.Worksheet
Dim workbook1worksheet1range1 As Microsoft.Office.Interop.Excel.Range
Dim workbook2 As Microsoft.Office.Interop.Excel.Workbook
Dim workbook2worksheet1 As Microsoft.Office.Interop.Excel.Worksheet
Dim workbook2worksheet1range1 As Microsoft.Office.Interop.Excel.Range
excelApplication = New Microsoft.Office.Interop.Excel.Application
excelApplication.Visible = False
excelApplication.DisplayAlerts = False
workbook1 = excelApplication.Workbooks.Open(in_InputFilePath)
workbook1worksheet1 = CType(workbook1.Sheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
workbook1worksheet1.Select(Type.Missing)
workbook1worksheet1range1 = workbook1worksheet1.UsedRange.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeVisible, Type.Missing)
workbook1worksheet1range1.Copy
workbook2 = excelApplication.Workbooks.Add()
workbook2worksheet1 = CType(workbook2.Sheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
workbook2worksheet1range1 = workbook2worksheet1.Range("A1")
workbook2worksheet1range1.PasteSpecial()
workbook2.SaveAs(in_OutputFilePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV)
workbook2.Save()
workbook2.Close()
workbook1.Close()
excelApplication.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook2worksheet1range1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook2worksheet1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook2)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook1worksheet1range1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook1worksheet1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication)

Related

how to import data from excel vb using Microsoft.Office.Interop.Excel

Sorry for bad English
i can export data to excel file this code
Imports Excel = Microsoft.Office.Interop.Excel
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlWorkBook = xlApp.Workbooks.Open("MY EXCEL PATH")
xlWorkSheet = xlWorkBook.Sheets("SHEET1")
With xlWorkSheet
.Range("D8").Value = "1"
.Range("D9").Value = "2"
End With
xlApp.Visible = True
I want to import data(D8,D9) from excel file to any text box .Can u help me? Thank
I am not sure where you want to import the data, but this should work and give you the value.
Imports Excel = Microsoft.Office.Interop.Excel
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim strOne As String
Dim strTwo As String
xlWorkBook = xlApp.Workbooks.Open("MY EXCEL PATH")
xlWorkSheet = xlWorkBook.Sheets("SHEET1")
With xlWorkSheet
strOne = .Range("D8").Value 'This will give you the value of cell D8 in strOne
strTwo = .Range("D9").Value 'This will give you the value of cell D9 in strTwo
End With
MsgBox strOne
MsgBox strTwo
'your code continue...

Copy information from a Excel file to another Excel file using SSIS VB2010, What wrong with these code

I am making an SSIS that will:
Copy data from SQL Server SQL to Excel file
Then from the Excel File into a another preexisting Excel file with some formulas
I did the first part --Copy from SQL to Excel1-- which works fine. Then I tried to copy from the Excel1 file to the other using script task in Visual Basic 2010.
I want to somebody to check this program and tell me what is wrong with it in trying to copy information from a Excel1 to a Excel2. When I executed I get the error DTS scrip Task Runtime Error and it does nothing.
Public Sub Main()
Dim oExcel As Microsoft.Office.Interop.Excel.Application,
oExel2 As Microsoft.Office.Interop.Excel.Application
Dim Obook As Microsoft.Office.Interop.Excel.Workbook,
Obook2 As Microsoft.Office.Interop.Excel.Workbook,
Osheet As Microsoft.Office.Interop.Excel.Worksheet,
Osheet2 As Microsoft.Office.Interop.Excel.Worksheet
'Programing
'From Excel Spreadsheet
oExcel = New Microsoft.Office.Interop.Excel.Application()
oExcel.Visible = False
Obook = New Microsoft.Office.Interop.Excel.Workbook("C:\Documents\AT1.xls")
Osheet = DirectCast(Obook.Sheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
'To Excel Spreadsheet
oExel2 = New Microsoft.Office.Interop.Excel.Application()
oExel2.Visible = False
Obook2 = New Microsoft.Office.Interop.Excel.Workbook("C:\Documents\A2.xls")
Osheet2 = DirectCast(Obook2.Sheets("January"), Microsoft.Office.Interop.Excel.Worksheet)
Osheet2.Range("B11", "R16").Value = Osheet.Range("A3", "Q8").Value
'Close
Osheet = Nothing
Obook.Close(False)
oExcel.Quit()
Osheet2 = Nothing
Obook.Close(False)
oExel2.Quit()
Dts.TaskResult = ScriptResults.Success
End Sub
You should not really use Excel Interop in SSIS as it implies that you must install Excel on the server and that is not generally recommended. However this will do the trick...
Public Sub Main()
Dim oExcel As Microsoft.Office.Interop.Excel.Application
Dim Obook As Microsoft.Office.Interop.Excel.Workbook, _
Obook2 As Microsoft.Office.Interop.Excel.Workbook, _
Osheet As Microsoft.Office.Interop.Excel.Worksheet, _
Osheet2 As Microsoft.Office.Interop.Excel.Worksheet
oExcel = New Microsoft.Office.Interop.Excel.Application()
oExcel.SheetsInNewWorkbook = 1
oExcel.DisplayAlerts = False
oExcel.Visible = False
Obook = oExcel.Workbooks.Open("C:\Documents\AT1.xls")
Osheet = Obook.Sheets("Sheet1")
Obook2 = oExcel.Workbooks.Add()
Osheet2 = Obook2.Worksheets.Item(1)
Osheet2.Name = "January"
Osheet2.Range("B11", "R16").Value = Osheet.Range("A3", "Q8").Value
Obook2.SaveAs("C:\Documents\A2.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal)
Obook.Close()
Obook2.Close()
oExcel.Quit()
End Sub
It would not be my choice, I'd probably use OLEDB instead.
To simply overwrite an existing workbook then...
Public Sub Main(ByVal Arguments() As String)
Dim oExcel As Microsoft.Office.Interop.Excel.Application
Dim Obook As Microsoft.Office.Interop.Excel.Workbook, _
Obook2 As Microsoft.Office.Interop.Excel.Workbook, _
Osheet As Microsoft.Office.Interop.Excel.Worksheet, _
Osheet2 As Microsoft.Office.Interop.Excel.Worksheet
oExcel = New Microsoft.Office.Interop.Excel.Application()
oExcel.SheetsInNewWorkbook = 1
oExcel.DisplayAlerts = False
oExcel.Visible = False
Obook = oExcel.Workbooks.Open("C:\Documents\AT1.xls")
Osheet = Obook.Sheets("Sheet1")
Obook2 = oExcel.Workbooks.Open("C:\Documents\A2.xls")
Osheet2 = Obook2.Sheets("January")
Osheet2.Range("B11", "R16").Value = Osheet.Range("A3", "Q8").Value
Obook2.Save()
Obook.Close()
Obook2.Close()
oExcel.Quit()
End Sub

How to manipulate an excel workbook with Excel.Workbook

So, I can't find any methods with file opening in Excel.Workbook or Excel.Application, and I am wondering why.
I have path to an Excel file, and I would like to manipulate it. I understand I have to use Excel.Application, Excel.Workbook, and Excel.Worksheet. The file to the Excel file is ExcelFilePath, what should I do to make it possible?
You first need to add the reference to Microsoft.Office.Interop.Excel to your project in order to use the Excel API, then fill the variables:
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWS As Excel.Worksheet
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open(sFilePath)
xlWS = CType(xlWorkBook.Worksheets(sheetNameOrIndex), Excel.Worksheet)
And then you only need to use the methods they have. For example:
Dim sVar as String = xlWS.Range("C5").Value.ToString()
Try this: from my project
Dim xapp As New Microsoft.Office.Interop.Excel.Application
Dim wb As Workbook = xapp.Workbooks.Add
Dim ws As Worksheet = wb.Worksheets(1)
ws.Activate()
'Fill header of the sheet----------------------------------
For i As Integer = 1 To dgvcustomer.Columns.Count
ws.Cells(1, i) = dgvcustomer.Columns(i - 1).HeaderText
Next
'End header------------------------------------------------
Dim Dgrow, Dgcell, Dgcol As Integer
Dgrow = 1
Dgcell = 1
'Fill Sheet ------------------------------------------------------------------------------------------------
While (Dgrow <= dgvcustomer.Rows.Count)
Dgcol = 1
While (Dgcol <= ws.UsedRange.Columns().Count)
ws.Cells(Dgrow + 1, Dgcol).value = dgvcustomer.Rows(Dgrow - 1).Cells(ws.Cells(1, Dgcol).value).Value
Dgcol += 1
End While
Dgrow += 1
End While
'End fill sheet---------------------------------------------------------------------------------------------
wb.SaveAs(dlgSaveFile.FileName)
wb.Close()
xapp.Quit()

Getting TXT file to Excel

I have the following code to get a text file to an Excel workbook.
Dim XlApp As Excel.Application = New Excel.Application
Dim XlWbk As Excel.Workbook = XlApp.Workbooks.Add
Dim SheetName As String = "Sheet1"
Dim XlWkst As Excel.Worksheet = FindSheet(XlWbk, SheetName)
Dim XlRng As Excel.Range = Nothing
If XlWkst Is Nothing Then
XlWkst = DirectCast(XlWbk.Sheets.Add(After:=XlWbk.Sheets(XlWbk.Sheets.Count), _
Count:=1, Type:=Excel.XlSheetType.xlWorksheet), Excel.Worksheet)
End If
Dim Lines() As String = File.ReadAllLines("C:\TEMP\FlowgateNNL_Mar2011_base.txt")
Dim RC(Lines.Length - 1)() As String
For I As Integer = 0 To Lines.Length - 1
RC(I) = Lines(I).Split(CChar(vbTab))
Next
XlRng = XlWkst.Range("a1")
XlRng.Value = RC
XlApp.Visible = True
This method seems to be the fastest way to read and parse a CSV file for dumping to Excel on my computer. it is choking on XlRng.Value = RC. Excel doesn't seem to like RC.
Any ideas for getting Excel to accept the data?
You are setting the value of a cell to an array that contains an entire CSV? I'm guessing this throws a type error.
This is probably more along the lines of what you want.
Dim XlApp As Excel.Application = New Excel.Application
Dim XlWbk As Excel.Workbook = XlApp.Workbooks.Add
Dim SheetName As String = "Sheet1"
Dim XlWkst As Excel.Worksheet = FindSheet(XlWbk, SheetName)
Dim XlRng As Excel.Range = Nothing
If XlWkst Is Nothing Then
XlWkst = DirectCast(XlWbk.Sheets.Add(After:=XlWbk.Sheets(XlWbk.Sheets.Count), _
Count:=1, Type:=Excel.XlSheetType.xlWorksheet), Excel.Worksheet)
End If
Dim Lines() As String = File.ReadAllLines("C:\TEMP\FlowgateNNL_Mar2011_base.txt")
For I As Integer = 0 To Lines.Length - 1
XlRng = XlWkst.Range("a1").Offset(I, 0)
Dim RC() As String = Lines(I).Split(CChar(vbTab))
For J As Integer = 0 To RC.Length - 1
XlRng = XlRng.Offset(0, J)
XlRng.Value = RC(J)
Next
Next
XlApp.Visible = True
Did you try simply opening the CSV file directly as in:
Dim XlApp As Excel.Application = New Excel.Application
Dim XlWbk As Excel.Workbook = XlApp.Workbooks.Open("C:\TEMP\FlowgateNNL_Mar2011_base.txt")
XlApp.Visible = True
If you have a valid CSV file do yourself a favor and use a specialised CSV datareader which can handle all the weird problems of CSVs (quotes etc.)
Personally, I like http://kbcsv.codeplex.com/ very much - it is simple to use and fast.
Then just dump the interop part and use the EPPLUS Library to write the data to an excel file - it is magnitudes faster and mind boggingly simple.
Your workflow would look like this:
Use KBCSV to load the data to a datatable.
Use EPPLUS's method "LoadFromDataTable" to generate a new excel file and save it.
Done!

Modifying a template based excel in VB.NET

I am tyring to run code like this
xlWorkSheet.Cells(rownumber, 1) = "Some Value"
the problem is when I open the excel document the cell value is not updated.
any help will be appreciated
I use the following code to open the file
Dim pathtofile As String = Server.MapPath("~/UserControls/Upload/MyUpload.xlsx")
Dim xlApp As Excel.Application =New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlworkbooks As Excel.Workbooks
Dim xlWorksheets As Excel.Worksheets
Dim misValue As Object = System.Reflection.Missing.Value
Dim xlWorkSheet As Excel.Worksheet = Nothing
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlworkbooks = xlApp.Workbooks
xlWorkBook = xlworkbooks.Open(pathtofile )
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
You need to save, close, and release the com objects to make sure excel doesn't remain running as a process.
Sub save(filename As String) ' Saves the sheet under a specfic name
xlWorkBook.SaveAs(filename)
End Sub
Sub closexl()
xlWorkBooks.Close()
xlApp.Quit()
End Sub
Sub cleanup() ' Releases all of the com objects to object not have excel running as a process after this method finishes
GC.Collect()
GC.WaitForPendingFinalizers()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
End Sub