在OOO Calc电子表格中为每一行创建新的txt / html文件

时间:2014-08-02 06:03:31

标签: macros jekyll openoffice-calc

我想从Calc电子表格的每一行创建一个新的txt文件(真正的html文件)。我从Excel电子表格中找到了一些答案,但它们不适用于Calc。

我没有Excel。我正在使用OOO 4。

我在Calc中尝试了这个Excel宏但得到了一个错误 - Range是一种未知的数据类型。研究这似乎说Excel宏在OOO Calc中不能很好地工作。我发现了一些事情,如果我在选项中启用了“可执行代码”,Excel宏可能会起作用,但这没有帮助。下面的宏只假设2列,我试图让它作为一个起点。

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

我的工作表有多个列(我们可以说6个用于示例目的)。我想用列1中的值命名每个文件,然后让文件本身包含每个附加列的内容 - 每个列都在一个新行上。理想情况下,这将适用于空单元格(只是一个空行),所以我可以根据需要添加新的空行来分隔东西。

工作表有400行,所以我希望它最终为400个文件 - 每行一个。

电子表格包含来自CMS的博客内容 - 我查询数据库以获取标题,摘要,正文,类别等,并将这些内容放入Excel电子表格中。因此,给定单元格中的某些内容可能非常长并且包含html。其中还有逗号和标签,因此是Excel电子表格而不是CSV。

我的目标是使用我可以从中获取的各个文件来提供给Jekyll来重新创建我的博客。我知道Jekyll有进口商,但我的CMS不是其中之一(DNN / Ventrian)。我没有看到将excel表直接导入jekyll的方法。

我希望将所有数据导出到文件中 - 它不是特定范围或任何内容,整张表。

1 个答案:

答案 0 :(得分:0)

这似乎有效 - 虽然它有时会出错,但我认为因为我用作文件名的值存在问题。当我玩它时,我有i = 1到50,改变它控制它做了多少行。有一些注释掉的Print语句有助于查看正在发生的事情 - 它们只是在弹出一点点打印到屏幕上。

此修改自: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