Skip to content

Major Java APIs

KT edited this page Dec 29, 2018 · 6 revisions

Read Excel file

Read Excel to get XlBean

  • XlBean read(InputStream)
  • XlBean read(File)
InputStream in = XlBeanReaderTest.class.getResourceAsStream("TestBook_format.xlsx");

XlBeanReader reader = new XlBeanReader();
XlBean bean = reader.read(in);

Read Excel to get XlBean and Definitions

  • XlBeanReaderContext readContext(InputStream)
  • XlBeanReaderContext readContext(File)
InputStream in = XlBeanReaderTest.class.getResourceAsStream("TestBook_option.xlsx");

XlBeanReader reader = new XlBeanReader();
XlBeanReaderContext context = reader.readContext(in);
XlBean bean = context.getXlBean();
DefinitionRepository definitions = context.getDefinitions();

XlBean APIs

XlBean itself is a implementation of java.util.Map so it supports Object get(String) by default. In addition to standard Map methods, XlBean implements conversion APIs for your convenience.

Convert to XlBean

  • XlBean bean(String key)
  • List<XlBean> beans(String key)

Convert to primitive values

  • String string(String key)
  • List<String> strings(String key)
  • Boolean bool(String key)
  • List<Boolean> bools(String key)
  • Integer integer(String key)
  • List<Integer> integers(String key)
  • Long lng(String key)
  • List<Long> longs(String key)
  • Double dbl(String key)
  • List<Double> doubles(String key)
  • Short shrt(String key)
  • List<Short> shorts(String key)
  • Float flt(String key)
  • List<Float> floats(String key)
  • Character character(String key)
  • List<Character> characters(String key)

Convert to other Classes

XlBean object can be converted to instance of any class by using of methods, including nested beans. Internally it uses org.xlbean.converter.BeanConverter#toBean(Object) so that detail specification of this conversion is defined by this class.

  • <T> T of(Class<T> destinationClass)
  • <T> T beanOf(String sourceKey, Class<T> destinationClass): Convert an object retrieved by sourceKey to destinationClass
  • <T> List<T> listOf(String sourceKey, Class<T> destinationClass): Convert a List retrieved by sourceKey to a List of destinationClass

Set value to XlBean

XlBean supports only String, XlBean or List as values by default. If you want to add other type of values, then convert those values to String, XlBean or List by yourself or use set method instead. set method will use org.xlbean.converter.BeanConverter#toMap(Object) internally to convert any type of value to supported types.

  • void set(Object obj)
  • void set(String key, Object obj)