Problem:Export Data To Excel File

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

45 Replies - 3283 Views - Last Post: 01 December 2011 - 02:56 PM Rate Topic: -----

#1 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Problem:Export Data To Excel File

Posted 30 November 2011 - 07:13 AM

I'm using vb2008
I ried this code to print data from my project to Excel file if the File is open . but didn't success
Dim exl As New Excel.Application
    Dim exlWorkSheet As Excel.Worksheet
            Try
                Dim fs As IO.FileStream = File.Open("C:\ta1.xlsx", FileMode.Open)
                fs.Dispose()
                MsgBox("Not open")
            Catch ex As IOException
                exl.Workbooks.Add("C:\ta1.xlsx")
                exlWorkSheet = exl.Worksheets("sheet")
                exlWorkSheet.Cells(2, 6).Value = "Open"
            End Try 


Is This A Good Question/Topic? 0
  • +

Replies To: Problem:Export Data To Excel File

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,021
  • Joined: 12-June 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 07:43 AM

Can you explain more on what "didn't success " means? Did it bomb out? Give different results? Work in an unexpected manner?
Was This Post Helpful? 0
  • +
  • -

#3 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 08:13 AM

Check our tutorial section

EDIT: oops wrong link :)
http://www.dreaminco...bnet-tutorials/

This post has been edited by NoBrain: 30 November 2011 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#4 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 08:42 AM

View Postmodi123_1, on 30 November 2011 - 07:43 AM, said:

Can you explain more on what "didn't success " means? Did it bomb out? Give different results? Work in an unexpected manner?

To be more clearly:
This code worked with me. but if I want to execute it again when the file is open, it will open another file and so..
I want to modify this code to work by print "Hello" to the file when the file is open. I hope I'm clear
Dim exl As New Excel.Application
        Dim exlWorkSheet As Excel.Worksheet
        exl.Visible = True
        exl.Workbooks.Open("C:\ta1.xlsx")
        exlWorkSheet = exl.Worksheets("sheet")
        exlWorkSheet.Cells(2, 6).Value = "Hello"

This post has been edited by Mangore: 30 November 2011 - 08:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,021
  • Joined: 12-June 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 08:47 AM

So.. you want a never ending cascade of:
try to open file_1
if file_1 is open, try opening file_2
add some text to file_2
if file_2 is open, try opening file_3
add some text to file_3
if file_3 is open, try opening file_4
add some text to file_4
....

?
Was This Post Helpful? 0
  • +
  • -

#6 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 09:08 AM

View Postmodi123_1, on 30 November 2011 - 08:47 AM, said:

So.. you want a never ending cascade of:
try to open file_1
if file_1 is open, try opening file_2
add some text to file_2
if file_2 is open, try opening file_3
add some text to file_3
if file_3 is open, try opening file_4
add some text to file_4
....

?

No
I want if File1 is open
Try to add some text to File1.
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,073
  • Joined: 02-July 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 09:39 AM

You don't need a filestream to load your excel file.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,021
  • Joined: 12-June 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 09:44 AM

I still don't get your problem. Why not move your code up to the try part so i actually tries to execute.
 
        Dim exl As New Excel.Application
        Dim exlWorkSheet As Excel.Worksheet
     Try
            'Dim fs As IO.FileStream = File.Open("C:\ta1.xlsx", FileMode.Open)
            'fs.Dispose()
            'MsgBox("Not open")
            exl.Workbooks.Add("C:\ta1.xlsx")
            exlWorkSheet = exl.Worksheets("sheet")
            exlWorkSheet.Cells(2, 6).Value = "Open"
            '-- needs save here
        Catch ex As IOException
            MsgBox(ex.Message)
        Finally
            '-- needs object clean up here.. quits, closes, disposes, etc.
        End Try



Was This Post Helpful? 0
  • +
  • -

#9 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 10:19 AM

I'm sorry. because I don't know how to explain. because my english it's not very well
so I'll try to explain from the first what I want step by step

1-I have a file exel called ta1 in C hard
2- I want to check if this file is open
3-if this file is not open open it and print(export) data to it
4- if this file open. print(export) data to it

I hope I'm clear!!
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,021
  • Joined: 12-June 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 10:35 AM

A few things - .NET gets pissy when trying to open and modify a work book already opened. At best it'll throw you an exception, at worst it'll crash. That option is gone. All that leaves is your existing code that adds a record to the file!
Was This Post Helpful? 0
  • +
  • -

#11 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 10:59 AM

My englsih it's not very well I can't understand what do you mean?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,021
  • Joined: 12-June 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 11:03 AM

You cannot use code to open an already opened excel work book. It throws an exception.

That leaves opening an unopened excel work book - which you already have the code for.
Was This Post Helpful? 0
  • +
  • -

#13 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 11:20 AM

I don't want to open a already opened excel work book. I want to export data to it
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,021
  • Joined: 12-June 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 12:09 PM

You want to write data to it.. good.. your code already is doing that.
Was This Post Helpful? 0
  • +
  • -

#15 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: Problem:Export Data To Excel File

Posted 30 November 2011 - 12:16 PM

View Postmodi123_1, on 30 November 2011 - 12:09 PM, said:

You want to write data to it.. good.. your code already is doing that.

I want to write data to it without open a new file my last code write data with a new file. so I tried this code but didn't success
Dim exl As New Excel.Application
Dim exlWorkSheet As Excel.Worksheet
exlWorkSheet = exl.Workbooks("ta1").Worksheets("Sheet")
exlWorkSheet.Cells(2, 6).Value = "hello"

This post has been edited by Mangore: 30 November 2011 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »