简体   繁体   English

Clojure。 创建打开文档文件的示例

[英]Clojure. Example creating an Open Document File

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.");
    }
  }
 }

You will need to import the ODF Kit library in your dependencies:您需要在依赖项中导入 ODF Kit 库:

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

Clojure version: 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))))))

Result:结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从Clojure调用实例方法。 - Unable to call an instance method from Clojure. 如何使用getInputStream()获取Clojure中的POST请求数据。 我在Reflector中收到了NullPointerException - How to use getInputStream() to get POST request data in Clojure. I am getting an NullPointerException in Reflector 从clojure项目创建可执行jar文件? - Creating an executable jar file from a clojure project? 在Clojure中创建flexmark扩展 - Creating a flexmark extension in Clojure 创建一个.bat文件以打开一个文件并读取它,然后打开一个.java文件 - Creating a .bat file to open a file and read it then open a .java file 函数result-document()使文件保持打开状态 - Function result-document() leaves file open 创建Word文档时文件损坏 - Corrupted file when creating Word document 使用 Intent.ACTION_OPEN_DOCUMENT 从特定文件夹打开文件 - Open file from specific folder with Intent.ACTION_OPEN_DOCUMENT 读取大文件的最佳方法(例如非常大的文本文档) - The best way to read a huge file (for example a very large text document) Eclipse创建.gitignore文件并且开放资源不起作用 - Eclipse creating .gitignore file and Open Resource not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM