?
Solved

DELETE Cells In Excel 2002 With VB.NET

Posted on 2007-03-23
2
Medium Priority
?
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
K V earned 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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