Create new txt/html file for each row in OOO Calc spreadsheet

谁都会走 提交于 2020-01-16 01:51:05

问题


I would like to create a new txt file (html file really) from each row of a Calc spreadsheet. I have found a few answers that do this from an Excel spreadsheet but they do not work with Calc.

I do not have Excel. I am using OOO 4.

I tried this Excel macro in Calc but get an error - Range is an unknown data type. Researching this seems to say that Excel macros won't work well in OOO Calc. I had found something saying if I enabled 'Executable Code' in Options, Excel macros may work, but that didn't help. The Macro below assumes only 2 columns, I was trying to get it to work as a starting point.

Outputting Excel rows to a series of text files

Sub Export_Files()
Dim sExportFolder, sFN
Dim rArticleName As Range
Dim rDisclaimer As Range
Dim oSh As Worksheet
Dim oFS As Object
Dim oTxt As Object

'sExportFolder = path to the folder you want to export to
'oSh = The sheet where your data is stored
sExportFolder = "C:\Disclaimers"
Set oSh = Sheet1

Set oFS = CreateObject("Scripting.Filesystemobject")

For Each rArticleName In oSh.UsedRange.Columns("A").Cells
    Set rDisclaimer = rArticleName.Offset(, 1)

    'Add .txt to the article name as a file name
    sFN = rArticleName.Value & ".txt"
    Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & sFN, 2, True)
    oTxt.Write rDisclaimer.Value
    oTxt.Close
Next
End Sub

My sheet has multiple columns (we can say 6 for example purposes). I would like to name each file with the value in Column 1, and then have the file itself contain each additional columns' content - each on a new line. Ideally this would work with empty cells (would just be a blank line) so I could add new blank lines as needed to separate stuff.

The sheet has 400 rows, so I want that to end up as 400 files - one for each row.

The spreadsheet contains the content of my blog from a CMS - I queried the database to get the title, summary, body, categories etc and put those into the excel spreadsheet. So some of the content in a given cell may be very long and contain html. There are also commas and tabs in it, thus the Excel Spreadsheet rather than a CSV.

My goal is to use the individual files I can get out of this to feed into Jekyll to recreate my blog. I am aware there are importers for Jekyll, but my CMS wasn't one of them (DNN/Ventrian). I didn't see a way to import an excel sheet directly into jekyll.

I want all data to export into the files - it is not a certain range or anything, the whole sheet.


回答1:


This seems to work - though it sometimes errors, I think because the value that I am using as the filename has a problem. I have i = 1 to 50 while I play with it, changing it controls how many rows it does. There are a couple of commented out Print statements that were helpful in seeing what was going on - they just print to the screen in a little pop up.

This is modified from: https://forum.openoffice.org/en/forum/viewtopic.php?t=34074

  sub saveas2
 Dim oDocOptions(0) as New com.sun.star.beans.PropertyValue

'stores the document objects
Dim oDoc as Object
Dim oSheet as Object
Dim sSavePath as String
Dim sFileName as string
Dim sFullPath as String
Dim sSaveLink as String
Dim oSaveOptions(1) as New com.sun.star.beans.PropertyValue
Dim c as Integer
 for c = 1 to 50

'stores the script doc's open settings


'setup the settings to open the crm script file with
 oDocOptions(0).Name = "Hidden"
 oDocOptions(0).Value = True
 ' print c
 ' print (thisComponent.getSheets.getByName("Query").getCellRangeByName("E" & c).getString)
 'open a blank spreadsheet to build the script with
  oDoc = starDesktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, oDocOptions())

  'setup the first sheet of the new doc
  oSheet = oDoc.sheets().getByIndex(0)

  'print (thisComponent.getSheets.getByName("Query").getCellRangeByName("E" & c).getString)
   oSheet.getCellRangeByName("A6").String = (thisComponent.getSheets.getByName("Query").getCellRangeByName("A" & c).getString)   
   oSheet.getCellRangeByName("A1").String = (thisComponent.getSheets.getByName("Query").getCellRangeByName("E" & c).getString)
   oSheet.getCellRangeByName("A2").String = (thisComponent.getSheets.getByName("Query").getCellRangeByName("C" & c).getString)
   oSheet.getCellRangeByName("A3").String = (thisComponent.getSheets.getByName("Query").getCellRangeByName("F" & c).getString)
   oSheet.getCellRangeByName("A4").String = (thisComponent.getSheets.getByName("Query").getCellRangeByName("G" & c).getString)

  'rebuild the options to save CRM doc with


  'set the options to save the file
   oSaveOptions(0).Name = "FilterName"
   oSaveOptions(0).Value = "Text - txt - csv (StarCalc)" 'THIS ALLOWS IT TO BE SAVED AS A TXT FILE
   oSaveOptions(1).Name = "FilterOptions"
   oSaveOptions(1).Value = "59,0,11,1," 'THIS IS THE FORMATTING I USED TO ACHIEVE THE NO DELIMITER AND ASCII FORMAT

   'store the save location


    sFileName = (thisComponent.getSheets.getByName("Query").getCellRangeByName("A" & c).getString)
    sSavePath = "j:\test\"
    sFullpath = sSavePath & sFileName & ".html"
    'Print sFullPath

   'build the link of the file to get
    sSaveLink = ConvertToURL(sFullPath)

    'save the CRM script file
  oDoc.storeAsURL(sSaveLink , oSaveOptions())

  'close the CRM script file
   oDoc.Close(True)

   next c
   end sub


来源:https://stackoverflow.com/questions/25092195/create-new-txt-html-file-for-each-row-in-ooo-calc-spreadsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!