【问题标题】:Clojure. Example creating an Open Document File克洛朱尔。创建打开文档文件的示例
【发布时间】:2020-01-28 20:08:20
【问题描述】:
import java.net.URI;

import org.odftoolkit.simple.TextDocument;
import org.odftoolkit.simple.table.Cell;
import org.odftoolkit.simple.table.Table;
import org.odftoolkit.simple.text.list.List;

public class HelloWorld {
    public static void main(String[] args) {
        TextDocument outputOdt;
    try {
        outputOdt = TextDocument.newTextDocument();

        // add image
        outputOdt.newImage(new URI("odf-logo.png"));

        // add paragraph
        outputOdt.addParagraph("Hello World, Hello Simple ODF!");

        // add list
        outputOdt.addParagraph("The following is a list.");
        List list = outputOdt.addList();
        String[] items = {"item1", "item2", "item3"};
        list.addItems(items);

        // add table
        Table table = outputOdt.addTable(2, 2);
        Cell cell = table.getCellByPosition(0, 0);
        cell.setStringValue("Hello World!");

        outputOdt.save("HelloWorld.odt");
    } catch (Exception e) {
        System.err.println("ERROR: unable to create output file.");
    }
  }
 }

【问题讨论】:

    标签: java clojure libreoffice


    【解决方案1】:

    您需要在依赖项中导入 ODF Kit 库:

     [org.odftoolkit/simple-odf "0.9.0-RC1"]]
    

    Clojure 版本:

    (ns clj-odf.core
     (:import java.net.URI
              org.odftoolkit.simple.TextDocument
              org.odftoolkit.simple.table.Cell
              org.odftoolkit.simple.table.Table
              org.odftoolkit.simple.text.list.List))
    
    (defn generate-odf [filename]
      (let [outputOdt (TextDocument/newTextDocument)
            uri       (URI. "/home/manuel/Documents/lisplogo_fancy_256.png")
            items     (into-array String ["Primer Asunto" "Segundo punto" "Tercer negocio" "Too long list"])]
     (try
       (println "Generating ODF file")
       (.addParagraph outputOdt "Hello World, taradazo Hello Simple ODF!")
       (.newImage outputOdt uri)
       (.addParagraph outputOdt "Hello World, taradazo Hello Simple ODF AFTER IMAGE!")
       (.addParagraph outputOdt "The following is a list.")
    
       ;; doto macro, first argument is passed to all the next nested forms  
       (doto (.addList outputOdt) 
        (.addItems items))
    
    
       ;; ".." threading macro, Expands into a member access (.) of the first member on the first
       ;; argument, followed by the next member on the result, etc.
      (.. outputOdt
         (addTable 2 2)
         (getCellByPosition 0 1)
         (setStringValue "I'm in some cell table!"))
    
    
       (.save outputOdt filename)
       (catch Exception e (str "ERROR: unable to create output file: " (.getMessage e))))))
    

    结果:

    【讨论】:

    • FWIW 你可以使用doto: (doto (TextDocument/newTextDocument) (.addParagraph "Some text!") (.newImage uri) (-> (.addList) (.addItems items)) ... (.save filename)) 使这更清晰
    猜你喜欢
    • 2018-10-15
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    相关资源
    最近更新 更多