VB.Net To Excel Usual Stuff

AID: 5106
  • Status: Published

1560 points

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

http://www.microsoft.com/downloads/details.aspx?familyid=c41bd61e-3060-4f71-a6b4-01feba508e52&displaylang=en

And then extract it and then add Microsoft.Office.Interop.Excel as reference then start writing the things you want to accomplish using .net and Excel.

This Blog or Article helps you to write values to excel,Merge Cells,Protect Excel sheet with Passwords and Many more stuffs.
Import the Statement in to your form or Class
Imports Microsoft.Office.Interop.Excel

Private Sub WorkingWithExcel()

'Declaring variable for Excel sheet
Dim xlApp As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
'Set the File path
xlWorkBook = xlApp.Workbooks.Open("File Path")
xlWorkSheet = xlWorkBook.Worksheets("Sheet1$")
With xlWorkSheet
'ASSIGN VALUE TO A CELL. Here below the value is assigned to 8th Row 12th 'column
.Cells(8, 12).Value ="This is test sentence."

'Add Rows to the Excel sheet.Here it inserts the row at 10th position
.Range("A10").EntireRow.Insert()

'Delete Rows to the Excel sheet.Here it Deletes the 10th Row
.Range("A10").EntireRow.Delete()


'Below code Merges the Entire row
.Range("A" & RowNo).EntireRow.MergeCells = True

'If you want to Merge only Cells not rows then Below code does it.
'Below merges Cells from C to F of 10th ROW
.Range("C10:F10").MergeCells = True


End With

'Before Saving if you want to Protect the Excel sheet with Password then
'below code tells how to assign the password for Excel workbook
xlWorkBook.Password = "TEST"

'Save the WORKBOOK..
xlWorkBook.Save()

'Close the WorkBook
xlWorkBook.Close()
xlApp.Quit()
'Below Releases the Excel sheet from the system process.
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
End  Sub
''' <summary>
''' Releases the excel from the system process
''' </summary>
''' <param name="obj">
</param>

''' <remarks></remarks>
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub

If you have any query or suggestion to the code please send me a message or leave comment here..

Hope you liked this code .
Asked On
2011-03-27 at 03:11:16ID5106
Tags

VB.Net

Topic

Microsoft Visual Basic.Net

Views
921

Comments

Author Comment

by: Avinash_Desai on 2011-04-03 at 02:57:35ID: 25454

Thanks for publishing ...

Dear Readers

If any question or any errors please let me know so that i can correct it

Regards
Avinash Desai

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Visual Basic.NET Experts

  1. CodeCruiser

    1,541,075

    Genius

    8,400 points yesterday

    Profile
    Rank: Genius
  2. kaufmed

    303,871

    Wizard

    500 points yesterday

    Profile
    Rank: Genius
  3. Idle_Mind

    230,817

    Guru

    2,010 points yesterday

    Profile
    Rank: Savant
  4. nepaluz

    192,076

    Guru

    0 points yesterday

    Profile
    Rank: Sage
  5. PaulHews

    161,438

    Guru

    520 points yesterday

    Profile
    Rank: Genius
  6. BuggyCoder

    150,598

    Guru

    0 points yesterday

    Profile
    Rank: Sage
  7. JamesBurger

    123,179

    Master

    0 points yesterday

    Profile
    Rank: Sage
  8. emoreau

    112,211

    Master

    0 points yesterday

    Profile
    Rank: Genius
  9. Masteraco

    102,128

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  10. TheLearnedOne

    80,982

    Master

    0 points yesterday

    Profile
    Rank: Savant
  11. Dhaest

    63,803

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  12. MlandaT

    53,803

    Master

    2,100 points yesterday

    Profile
    Rank: Genius
  13. wdosanjos

    53,796

    Master

    0 points yesterday

    Profile
    Rank: Genius
  14. mlmcc

    53,048

    Master

    0 points yesterday

    Profile
    Rank: Savant
  15. RolandDeschain

    41,679

    10 points yesterday

    Profile
    Rank: Sage
  16. srosebabu

    31,025

    2,000 points yesterday

    Profile
    Rank: Guru
  17. mas_oz2003

    28,400

    0 points yesterday

    Profile
    Rank: Genius
  18. sedgwick

    27,350

    0 points yesterday

    Profile
    Rank: Genius
  19. jacko72

    26,596

    0 points yesterday

    Profile
    Rank: Genius
  20. tommyBoy

    25,850

    0 points yesterday

    Profile
    Rank: Genius
  21. dlmille

    22,160

    0 points yesterday

    Profile
    Rank: Genius
  22. imnorie

    21,664

    1,600 points yesterday

    Profile
    Rank: Genius
  23. Cluskitt

    21,418

    0 points yesterday

    Profile
    Rank: Wizard
  24. robert_schutt

    20,440

    0 points yesterday

    Profile
    Rank: Guru
  25. navneethegde

    20,332

    0 points yesterday

    Profile
    Rank: Wizard

Hall Of Fame