Solved

DELETE Cells In Excel 2002 With VB.NET

Posted on 2007-03-23
2
238 Views
Last Modified: 2010-04-23
I'm using Visual Studio 2005.  I would like to keep what I'm trying to do within VB since that is what I'm most familiar with.  I'm trying to make a connection to Excel 2002.  I can do this through ADO.NET.  I'm trying to DELETE cells A1 - O3.  You can't use the DELETE command with ADO.NET.  Is there another way of doing this?
0
Comment
Question by:dkraatz
2 Comments
 
LVL 21

Accepted Solution

by:
theGhost_k8 earned 125 total points
ID: 18779543
you can use:-
    CType(_worksheet.Rows(row), Excel.Range).Delete()


http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_21753891.html?sfQueryTermInfo=1+cell+delet+excel+vb.net#a16067725
answer from an expert:--
Here is part of a class that I am working on to encapsulate some of the more common requests for Excel automation code (work in progress):

Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices

Public Class ExcelWorksheet

  Private _app As Excel.Application
  Private _workbook As Excel.Workbook
  Private _worksheet As Excel.Worksheet

  Public Sub New()

    _app = New Excel.Application

  End Sub


  Protected Overrides Sub Finalize()
    MyBase.Finalize()

    _workbook.Close(SaveChanges:=False)

    _app.Quit()

    Marshal.ReleaseComObject(_app)
    Marshal.ReleaseComObject(_workbook)
    Marshal.ReleaseComObject(_worksheet)

  End Sub   'Finalize


  Public Sub Save()

    _workbook.Save()

  End Sub  'Save


  Public Sub DeleteRow(ByVal row As Integer)

    If row < 1 OrElse row > _worksheet.Rows.Count Then
      Throw New ArgumentOutOfRangeException("Row must be 1.." & _worksheet.Rows.Count)
    End If

    CType(_worksheet.Rows(row), Excel.Range).Delete()

  End Sub


  Public Function GetWorksheetData(ByVal fileName As String, _
   ByVal sheetName As String, ByVal fromCell As String, ByVal toCell As String, _
   ByVal firstRowHeader As Boolean) As DataTable

    _workbook = _app.Workbooks.Open(fileName)

    _worksheet = _workbook.Worksheets(sheetName)

    Dim rangeSelect As Excel.Range = _worksheet.Range(fromCell, toCell)

    Dim rowCount As Integer = rangeSelect.Rows.Count
    Dim columnCount As Integer = rangeSelect.Columns.Count

    Dim tableNew As New System.Data.DataTable(sheetName)

    For colIndex As Integer = 1 To columnCount

      Dim colName As String = "Column" & colIndex

      If firstRowHeader Then
        colName = CType(rangeSelect.Cells(1, colIndex), Excel.Range).Value
      End If

      tableNew.Columns.Add(New DataColumn(colName, GetType(String)))

    Next colIndex

    For rowIndex As Integer = 1 To rowCount

      Dim rowNew As DataRow = tableNew.NewRow

      For colIndex As Integer = 1 To columnCount
        rowNew(colindex - 1) = CType(rangeSelect.Cells(rowIndex, colindex), Excel.Range).Value
      Next

      tableNew.Rows.Add(rowNew)

    Next

    Return tableNew

  End Function   'GetWorksheetData

End Class

Bob
0
 

Author Comment

by:dkraatz
ID: 18801101
Thank you Ghost.  I don't really understand what all this does so a friend is breaking it down for me.  I appreciated you reply, and it looks like this is going to do the trick for me.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get Client IP on RDS - VB.NET 15 68
VB.net Open Folder in Windows Explorer 3 51
Help with converting xml file to excel using VB.NET 3 25
.NET 2008 VB and C# 6 39
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question