| View previous topic :: View next topic |
| Author |
Message |
Bombadillo Newbie

Joined: 03 Jan 2005 Posts: 4
|
Posted: Mon Jan 03, 2005 5:34 pm Post subject: Loading csv files into different sheets |
|
|
Hi, I've started learning basic and I'm trying to write a macro that loads different csv files into different sheets of the same spreadsheet. The problem is that I don't know how to reproduce the "Insert Sheet->From File" behaviour. The only function I found is loadComponentFromURL, but it's not what I need (at least I think...). Can anyone help me, please?
P.S.: As csv files are generated by a C++ program I wrote, how difficult would it be to use OpenOffice API C++ Binding to create a spreadsheet directly from that program? |
|
| Back to top |
|
 |
uros Super User


Joined: 22 May 2003 Posts: 601 Location: Slovenia
|
|
| Back to top |
|
 |
Bombadillo Newbie

Joined: 03 Jan 2005 Posts: 4
|
Posted: Tue Jan 04, 2005 5:48 am Post subject: Thanks |
|
|
Thank you very much uros . It works perfectly (and it's easier than I thought)! |
|
| Back to top |
|
 |
Danad OOo Advocate

Joined: 22 Feb 2004 Posts: 293 Location: Brasil
|
Posted: Tue Jan 04, 2005 8:05 pm Post subject: |
|
|
Another way ( XSheetLinkable ) that works for me:
| Code: |
Sub Ascii_2_CalcSheets
oDoc = thisComponent
' 2nd sheet
oPlan = oDoc.getSheets().getByIndex(1)
' set arguments
sURL = "C:\Documents and Settings\danad\My Documents\some_asc_file.txt"
sURL = ConvertToURL ( sURL )
sOrigem = ""
sFiltro = "Text - txt - csv (StarCalc)"
sOpc = "59,,0,1,1/2/2/1/3/2"
nModo = com.sun.star.sheet.SheetLinkMode.NORMAL
' link file
oPlan.link(sURL, sOrigem, sFiltro, sOpc, nModo)
' reset link
oPlan.setLinkMode(com.sun.star.sheet.SheetLinkMode.NONE)
' 3rd sheet
oPlan = oDoc.getSheets().getByIndex(2)
' same arguments, except last column
sOpc = "59,,0,1,1/2/2/1/3/9"
oPlan.link(sURL, sOrigem, sFiltro, sOpc, nModo)
' reset link
oPlan.setLinkMode(com.sun.star.sheet.SheetLinkMode.NONE)
End Sub
|
HTH |
|
| Back to top |
|
 |
uros Super User


Joined: 22 May 2003 Posts: 601 Location: Slovenia
|
Posted: Tue Jan 04, 2005 10:47 pm Post subject: |
|
|
Hi Danad!
Great job, much better then my way!
Uros |
|
| Back to top |
|
 |
Bombadillo Newbie

Joined: 03 Jan 2005 Posts: 4
|
Posted: Wed Jan 05, 2005 8:12 am Post subject: |
|
|
I've tried Danad's solution and its quite faster (I have to load nearly a hundred of csv files), thank you very much Now a final question: as some sheets contain data from different files, is there a way to link external data into a sheet starting from a given position? I've tried to look at documentation but I haven't found anything, so for the moment I simply create a temporary sheet and then copy data from it (like in uros' code).
Bye! |
|
| Back to top |
|
 |
Danad OOo Advocate

Joined: 22 Feb 2004 Posts: 293 Location: Brasil
|
Posted: Wed Jan 05, 2005 6:00 pm Post subject: |
|
|
Uros,
Thanks for the kind words.
Bombadillo,
Look for ( dev's guide ):
- method insertAtPosition of interface XAreaLinks
- property AreaLinks
- struct CellAddress
Code is almost the same as previous sample.
Last edited by Danad on Thu Jan 06, 2005 5:21 pm; edited 1 time in total |
|
| Back to top |
|
 |
Bombadillo Newbie

Joined: 03 Jan 2005 Posts: 4
|
Posted: Thu Jan 06, 2005 7:13 am Post subject: |
|
|
Thank you very much Danad  |
|
| Back to top |
|
 |
rufus General User

Joined: 18 May 2007 Posts: 20 Location: Bangkok, Thailand
|
Posted: Tue May 22, 2007 5:15 am Post subject: |
|
|
Danad,
Could you explain more detail about inserting data at current position. I found the insertAtPosition method in Developers Guide, but i did not understand how to use it.
Thank you. |
|
| Back to top |
|
 |
Danad OOo Advocate

Joined: 22 Feb 2004 Posts: 293 Location: Brasil
|
|
| Back to top |
|
 |
rufus General User

Joined: 18 May 2007 Posts: 20 Location: Bangkok, Thailand
|
Posted: Tue May 22, 2007 9:49 pm Post subject: |
|
|
Danad,
I tried your code, but it works not correctly for me. I need to import CSV-file into Calc in "DOS/OS2 866 Cyrr" codepage at 6th row.
When i use this code:
sub ImportCSVIntoSheet
oDoc = thisComponent
' 1st sheet
oPlan = oDoc.getSheets().getByIndex(0)
' set arguments
sURL = "file:///C:/Zakaz/Zakaz_oo/zakaz.csv"
'sURL = ConvertToURL ( sURL )
sOrigem = ""
sFiltro = "Text - txt - csv (StarCalc)"
sOpc = "44,34,30,1,"
nModo = com.sun.star.sheet.SheetLinkMode.NORMAL
' link file
oPlan.link(sURL, sOrigem, sFiltro, sOpc, nModo)
' reset link
oPlan.setLinkMode(com.sun.star.sheet.SheetLinkMode.NONE)
end sub
it imports in correct codepage ("44,34,30,1," parameters are responsible for this), but at 1st row. But when i try this:
Sub ex_Area_Link
Dim oDoc As Object
Dim oPlan As Object
Dim oLinks As Object
Dim aPos As New com.sun.star.table.CellAddress
oDoc = ThisComponent
oPlan = oDoc.getSheets().getByIndex(0)
sURL = "file:///C:/Zakaz/Zakaz_oo/zakaz.csv"
sFonte = "Planilha2.A1:E5"
sFiltro = "Text - txt - csv (StarCalc)"
sOpcoes = "44,34,30,1,"
aPos.Sheet = 0
aPos.Column = 0
aPos.Row = 5
oLinks = oDoc.AreaLinks
oLinks.insertAtPosition(aPos, sURL, sFonte, sFiltro, sOpcoes)
oLinks.removeByIndex(oLinks.Count - 1)
End Sub
it imports at 6th row, BUT in wrong codepage, like this:
22040560942 à ¦¥áª¨¥ Ž‚ˆƒŽ €âà æ¨â 36
22040560946 à ¦¥áª¨¥ Ž‚ˆƒŽ €âà æ¨â 40
22040560948 à ¦¥áª¨¥ Ž‚ˆƒŽ €âà æ¨â 42
22040561940 à ¦¥áª¨¥ Ž‚ˆƒŽ ¥¦¥¢ë© 34
22040561942 à ¦¥áª¨¥ Ž‚ˆƒŽ ¥¦¥¢ë© 36
How can i fix it? |
|
| Back to top |
|
 |
Danad OOo Advocate

Joined: 22 Feb 2004 Posts: 293 Location: Brasil
|
|
| Back to top |
|
 |
rivella Newbie

Joined: 01 Feb 2011 Posts: 3
|
|
| Back to top |
|
 |
|