POI Ruby Bindings(POI Ruby 绑定)

Intro(介绍)

The POI library can now be compiled as a Ruby extension, allowing the API to be called from Ruby language programs. Ruby users can therefore read and write OLE2 documents, such as Excel files with ease(POI 库现在可以编译为 Ruby 扩展,允许从 Ruby 语言程序调用 API。因此,Ruby 用户可以轻松读取和写入 OLE2 文档,例如 Excel 文件)

The bindings are generated by compiling POI with gcj, and generating the Ruby wrapper using SWIG. The aim is the keep the POI api as-is. However, where java standard library objects are used, an effort is made to transform them smoothly into Ruby objects. Therefore, where the POI API takes an OutputStream, you can pass an IO object. Where the POI works java.util.Date or java.util.Calendar object, you can work with a Ruby Time object.(通过使用gcj编译 POI 并使用SWIG生成 Ruby 包装器来生成绑定。目的是保持 POI api 原样。然而,为了在使用 java 标准库对象的地方,努力将它们平滑地转换为 Ruby 对象。因此,在 POI API 采用 OutputStream 的地方,您可以传递一个 IO 对象。 POI 使用 java.util.Date 或 java.util.Calendar 对象的地方,您可以使用 Ruby Time 对象。)

Getting Started(入门)

Pre-Requisites(先决条件)

The bindings have been developed with GCC 3.4.3 and Ruby 1.8.2. You are unlikely to get correct results with versions of GCC prior to 3.4 or versions of Ruby prior to 1.8. To compile the Ruby extension, you must have GCC (compiled with java language support), Ruby development headers, and SWIG. To run, you will need Ruby (obviously!) and libgcj , presumably from the same version of GCC with which you compiled.(这些绑定是使用 GCC 3.4.3 和 Ruby 1.8.2 开发的。使用 3.4 之前的 GCC 版本或 1.8 之前的 Ruby 版本,您不太可能获得正确的结果。要编译 Ruby 扩展,您必须具有 GCC(使用 Java 语言支持编译)、Ruby 开发头文件和 SWIG。要运行,您需要 Ruby和 libgcj ,尽量使用与您编译的 GCC 版本相同的版本。)

Subversion(颠覆)

The POI-Ruby module sits under the POI Subversion (viewvc). Running make inside that directory will create a loadable ruby extension poi4r.so in the release subdirectory. Tests are in the tests/ subdirectory, and should be run from the poi-ruby directory. Please read the tests to figure out the usage.(POI-Ruby 模块位于 POI Subversion (viewvc) 下。在该目录中运行 make 将在 release 子目录中创建一个可加载的 ruby 扩展 poi4r.so。测试项位于 tests/subdirectory,你应该从 poi-ruby 目录运行它。请阅读测试以了解其用法。)

Note that the makefile, though designed to work across Linux/OS X/Cygwin, has been tested only on linux. There are likely to be issues on other platform; fixes gratefully accepted!(请注意,makefile 虽然设计为跨 Linux/OS X/Cygwin 工作,但仅在 linux 上进行了测试。其他平台可能有问题;我们将很感激接受您的修复!)

Binary(二进制)

A version of poi4r.so is available here (broken link). Its been compiled on a linux box with GCC 3.4.3 and Ruby 1.8.2. It dynamically links to libgcj. No guarantees about working on any other box.(此处提供了 poi4r.so 的版本(断开的链接)。它是在带有 GCC 3.4.3 和 Ruby 1.8.2 的 linux 机器上编译的。它动态链接到 libgcj。不保证在任何其他盒子上工作。)

Usage(用法)

The following ruby code shows some of the things you can do with POI in Ruby(以下 ruby 代码显示了您可以在 Ruby 中使用 POI 执行的一些操作)

h=Poi4r::HSSFWorkbook.new
#Test Sheet Creation
s=h.createSheet("Sheet1")
#Test setting cell values
s=h.getSheetAt(0)
r=s.createRow(0)
c=r.createCell(0)
c.setCellValue(1.5)
c=r.createCell(1)
c.setCellValue("Ruby")
#Test styles
st = h.createCellStyle()
c=r.createCell(2)
st.setAlignment(Poi4r::HSSFCellStyle.ALIGN_CENTER)
c.setCellStyle(st)
c.setCellValue("centr'd")
#Date handling
c=r.createCell(3)
t1=Time.now
c.setCellValue(Time.now)
t2= c.getDateCellValue().gmtime
st=h.createCellStyle();
st.setDataFormat(Poi4r::HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"))
c.setCellStyle(st)
#Formulas
c=r.createCell(4)
c.setCellFormula("A1*2")
c.getCellFormula()
#Writing
h.write(File.new("test.xls","w"))

The tc_base_tests.rb file in the tests sub directory of the source distribution contains examples of simple uses of the API. The quick guide is the best place to learn HSSF API use. (Note however that none of the Drawing features are implemented in the Ruby binding.) See also the POI API documentation for more details.(源代码分发的 tests 子目录中的 tc_base_tests.rb 文件包含 API 的简单使用示例。快速指南是学习 HSSF API 使用的最佳场所。 (但是请注意,在 Ruby 绑定中没有实现任何绘图功能。)另请参阅POI API 文档以获取更多详细信息。)

Future Directions(未来发展方向)

TODO's(待办事项)

  • Implement support for reading Excel files (easy)(实现对读取 Excel 文件的支持(简单))
  • Expose POIFS API to read raw OLE2 files from Ruby(发布POIFS API 以从 Ruby 读取原始 OLE2 文件)
  • Expose HPSF API to read property streams(发布HPSF API 以读取属性流)
  • Tests... Tests... Tests...(测试...测试...测试...)

Limitations(限制)

  • Check operations in 64bit machines - Java primitive types are fixed irrespective of machine type, unlike C/C++ types. The wrapping code that converts C/C++ primitive types to/from Java types is making assumptions on type sizes that MAY be incorrect on wide architectures.(检查 64 位机器中的操作 - Java 原始类型是固定的,与机器类型无关,这与 C/C++ 类型不同。将 C/C++ 原始类型转换为 Java 类型/从 Java 类型转换的包装代码正在假设类型大小在广泛的体系结构上可能不正确。)
  • The current implementation is with the POI 2.0 release. The 2.5 release adds support for Excel drawing primitives, and thus has a dependency on java AWT. Since AWT is not very mature in gcj, leaving it out seemed to be the safer option.(当前的实现是 POI 2.0 版本。 2.5 版本增加了对 Excel 绘图原语的支持,因此依赖于 java AWT。由于 AWT 在 gcj 中不是很成熟,因此将其排除在外似乎是更安全的选择。)
  • Packaging - The current make file makes no effort to install the extension into the standard ruby directories. This should probably be packaged as a gem.(打包 - 当前的 make 文件不会将扩展安装到标准的 ruby 目录中。这可能应该被打包为一个gem)

by Avik Sengupta(艾维克·森古普塔)

 
中英文 | 中文 | 英文