我正在尝试使用clojure docjure(https://github.com/mjul/docjure)编辑Excel工作表中的某些单元格。

docjure文档中显示了如何创建新的电子表格,但是我不想这样做。我想做的是读取Excel工作表的一些信息,计算一些新值,然后将它们写回到同一工作表的特定单元格中。

好,前两个步骤正在工作。我只是不知道如何写回值。

我怎样才能做到这一点?

最佳答案

查看测试通常很有用,因为它们也可以一定程度地记录代码。我找到了一个看起来相关的文件:https://github.com/mjul/docjure/blob/master/test/dk/ative/docjure/spreadsheet_test.clj#L126

在该文件中,我发现:

(deftest set-cell!-test
  (let [sheet-name "Sheet 1"
    sheet-data [["A1"]]
    workbook (create-workbook sheet-name sheet-data)
        a1 (-> workbook (.getSheetAt 0) (.getRow 0) (.getCell 0))]
    (testing "set-cell! for Date"
      (testing "should set value"
        (set-cell! a1 (july 1))
        (is (= (july 1) (.getDateCellValue a1))))
      (testing "should set nil"
        (let [^java.util.Date nil-date nil]
          (set-cell! a1 nil-date))
        (is (= nil (.getDateCellValue a1)))))
    (testing "set-cell! for String"
      (testing "should set value"
        (set-cell! a1 "foo")
        (is (= "foo" (.getStringCellValue a1)))))
    (testing "set-cell! for boolean"
      (testing "should set value"
        (set-cell! a1 (boolean true))
        (is (.getBooleanCellValue a1))))
    (testing "set-cell! for number"
      (testing "should set int"
        (set-cell! a1 (int 1))
        (is (= 1.0 (.getNumericCellValue a1))))
      (testing "should set double"
        (set-cell! a1 (double 1.2))
        (is (= 1.2 (.getNumericCellValue a1)))))))


我现在无法测试,但这是您想要的吗?

10-08 18:55