Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to format with a Xcelite writer

Tags:

java

excel

  1. How do I skip columns (include spacing in the middle of my data to make it readable) when writing a new sheet using Xcelite?

  2. Is it possible to write two tables on the same sheet with the 2nd table below the 1st?

  3. Is Xcelite even the right tool for me to be using? Most of the information that I was able to find on using Excel writing tools for Java is over a decade old. Xcelite is very easy to use but it doesn't seem very customizable and the only documentation I can find for it is their Github page and its light on details

like image 344
dbrewster Avatar asked Nov 27 '25 12:11

dbrewster


1 Answers

  1. Columns can be skipped by adding an unused field to the pojo and then using the Xcelite @Column annotation to set the value to a space (can't be an empty string) -
@Column(name = " ")
private final String space = null;
  1. Two tables can be written to the same spreadsheet. After writing the 1st table, you simply grab the same sheet via getSheet and then proceed as normally and the 2nd table be written right next to it. To place the 2nd table below the 1st, you need to modify the XceliteOptions via the setHeaderRowIndex. This needs to be done after writing the 1st dataset and before writing the 2nd.

public void write2Tables(List<Data> firstList, List<Stat> secondList, String sheetName) {
    Xcelite xcelite = new Xcelite()
    xcelite.createSheet(sheetName).getBeanWriter(Data.class).write(firstList);
    xcelite.write(outputFile)

    XceliteOptions options = new XceliteOptions();
    options.setHeaderRowIndex(firstList + 2); //single space between tables
    xcelite.setOptions(options);

    xcelite.getSheet(sheetName).getBeanWriter(Stat.class).write(secondList);
    xcelite.write(outputFile)
}
like image 77
dbrewster Avatar answered Nov 30 '25 01:11

dbrewster