Results 1 to 3 of 3

Thread: if you want a code to transfer data from within a programe to excel take this

  1. #1

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    if you want a code to transfer data from within a programe to excel take this

    take this code if you want to convert or transfer data from whithin your programe to excel sheet
    memas
    ' go add reference com and import microsoft.office.12 object library
    'and declare your textboxes and labels like system.windows.forms.textbox
    Option Strict On


    Imports Microsoft.Office.Interop
    Imports System.IO

    Public Class Form1
    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    xlApp.Visible = True
    ' xlWorkBook = xlApp.Workbooks.Add(misValue)
    Dim filename As String
    filename = "C:\VBtoExcelDemo.xls"


    If File.Exists(filename) Then
    xlWorkBook = xlApp.Workbooks.Open(filename)
    Else
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    End If
    xlWorkSheet = CType(xlWorkBook.Sheets("sheet1"), Excel.Worksheet)
    'xlWorkBook.Sheets("Sheet1").Cells(1, 1).Value = "Meow!"
    'Make sure our object variable is dest
    For i As Long = 1 To 65536
    If CType(CType(xlWorkSheet.Cells(i, 1), Excel.Range).Value, String) = "" Then


    ''1,2,3... etc is the column number of the excel row
    CType(xlWorkSheet.Cells(i, 1), Excel.Range).Value = TextBox1.Text
    CType(xlWorkSheet.Cells(i, 2), Excel.Range).Value = TextBox2.Text
    CType(xlWorkSheet.Cells(i, 3), Excel.Range).Value = TextBox3.Text
    CType(xlWorkSheet.Cells(i, 4), Excel.Range).Value = TextBox4.Text
    CType(xlWorkSheet.Cells(i, 5), Excel.Range).Value = TextBox5.Text
    CType(xlWorkSheet.Cells(i, 6), Excel.Range).Value = TextBox6.Text
    CType(xlWorkSheet.Cells(i, 7), Excel.Range).Value = TextBox7.Text
    CType(xlWorkSheet.Cells(i, 8), Excel.Range).Value = TextBox8.Text
    CType(xlWorkSheet.Cells(i, 9), Excel.Range).Value = TextBox9.Text
    CType(xlWorkSheet.Cells(i, 10), Excel.Range).Value = TextBox10.Text
    CType(xlWorkSheet.Cells(i, 11), Excel.Range).Value = TextBox11.Text
    CType(xlWorkSheet.Cells(i, 12), Excel.Range).Value = TextBox12.Text
    CType(xlWorkSheet.Cells(i, 13), Excel.Range).Value = TextBox13.Text
    CType(xlWorkSheet.Cells(i, 14), Excel.Range).Value = TextBox14.Text
    CType(xlWorkSheet.Cells(i, 15), Excel.Range).Value = TextBox15.Text
    CType(xlWorkSheet.Cells(i, 16), Excel.Range).Value = TextBox16.Text

    Exit For


    End If

    Next



    '__Excel.ActiveWorkbook.Close(save,this.opt,this.opt);


    If File.Exists(filename) Then
    xlWorkBook.Save()
    Else
    xlWorkBook.SaveAs(filename)
    End If
    'xlWorkBook.SaveAs("C:\vbexcel.xls")
    'xlWorkBook.Save()


    ' MsgBox("Done")
    'If xlApp Is Nothing Then
    ' xlApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    ' xlApp.Visible = True
    ' xlAppWorkbook = DirectCast(xlApp.Workbooks.Add(), Excel.Workbook)
    'End If


    'xlApp.Range("a1").Value = TextBox1.Text
    'xlApp.Range("b1").Value = TextBox2.Text
    'xlApp.Range("c1").Value = TextBox3.Text
    'xlApp.Range("d1").Value = TextBox4.Text





    End Sub


    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

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    xlApp = New Excel.ApplicationClass
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    On Error Resume Next

    xlWorkBook.Close()
    xlApp.Quit()

    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    End Sub
    End Class

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: if you want a code to transfer data from within a programe to excel take this

    First, use
    [ code ]
    [ /code ]

    Code:
    'some code
    tags when posting code.

    Second, this section of forums is for asking questions mostly. If you wanted to share code take a look at the Code Bank section.

    Third, the code you posted looks like VB.Net (Visual Studio 2002 >), and this section of the forum is for VB classic (VB6 and older).

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: if you want a code to transfer data from within a programe to excel take this

    Thread moved from the 'VB6' forum to the 'CodeBank VB.Net' forum

    (thanks for letting us know baja_yu )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width