OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[Solved] Read text file into the current calc file

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Tiksu
General User
General User


Joined: 31 Dec 2010
Posts: 48

PostPosted: Thu Apr 12, 2012 7:11 am    Post subject: [Solved] Read text file into the current calc file Reply with quote

I succeeded to get my text file opened by calc with the code

Code:

sub read_file()
Dim PropVal As New com.sun.star.beans.PropertyValue
Propval(0).Name = "FilterName"
Propval(0).Value = "Text - txt - csv (StarCalc)"
Propval(0).Name =   "Filtername"
Propval(1).Name = "FilterOptions"
Propval(1).Value = "9,34,utf-8"   
url = convertToUrl("/home/lenovo/folders/my_folder/my_data.csv")
oDoc = StarDesktop.loadComponentFromURL(url, "_blank", 0,  Array(Propval())   )
oDoc.Close (True)
end sub


But things would be more simple, if I could get the file read into the current calc sheet. How could you get the data in the current sheet?

Tiksu


Last edited by Tiksu on Mon Apr 16, 2012 11:47 am; edited 1 time in total
Back to top
View user's profile Send private message
patel
Power User
Power User


Joined: 14 Apr 2012
Posts: 54
Location: Italy

PostPosted: Sat Apr 14, 2012 3:15 am    Post subject: Reply with quote

you can read file and put the lines into the cells

Code:
REM  *****  BASIC  *****

sub readFile
dim document   as object, Cell as object, filename as string, i as integer, n as integer
Doc = ThisComponent
oSheet = Doc.getSheets().getByIndex(0)
filename="file://home/lenovo/folders/my_folder/my_data"
n = FreeFile()                                      'Next free file number
Open filename For input As #n 
col=0
for row=2 to 5
Cell = Sheet.getCellByPosition(col, row)
Line input #n, ostring
Cell.string=ostring
next
Close #n
end sub


try this also

Code:
Sub ImportCSV
Dim oDoc as Object, oSheet as Object
   fileName = ConvertToURL("/home/lenovo/folders/my_folder/my_data")
   Doc = thisComponent
   Sheet = Doc.getSheets().getByIndex(0)
      sURL = ConvertToURL(fileName)
      sOrigin = ""
      sFilter = "Text - txt - csv (StarCalc)"
      sOpc = "44, , , ,"
'     sOpc = "ASCII CODE Field Separator, ASCII CODE Text Delimiter, Character Set, Starting Line"
      nModo = com.sun.star.sheet.SheetLinkMode.NORMAL
      Sheet.link(sURL, sOrigin, sFilter, sOpc, nModo)
      Sheet.setLinkMode(com.sun.star.sheet.SheetLinkMode.NONE)
End Sub
Back to top
View user's profile Send private message
Tiksu
General User
General User


Joined: 31 Dec 2010
Posts: 48

PostPosted: Mon Apr 16, 2012 11:46 am    Post subject: Reply with quote

Thank you, importCSV is a perfect solution to the problem. It works like a charm Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group