| View previous topic :: View next topic |
| Author |
Message |
Tiksu General User

Joined: 31 Dec 2010 Posts: 48
|
Posted: Thu Apr 12, 2012 7:11 am Post subject: [Solved] Read text file into the current calc file |
|
|
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 |
|
 |
patel Power User

Joined: 14 Apr 2012 Posts: 54 Location: Italy
|
Posted: Sat Apr 14, 2012 3:15 am Post subject: |
|
|
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 |
|
 |
Tiksu General User

Joined: 31 Dec 2010 Posts: 48
|
Posted: Mon Apr 16, 2012 11:46 am Post subject: |
|
|
Thank you, importCSV is a perfect solution to the problem. It works like a charm  |
|
| Back to top |
|
 |
|
|
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
|