Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate/Export excel file from Java Play Framework 2.0 List<object>

I need to generate/export a excel file from a table that is generated from a list of objects. I can see that there is a module for Play 1.x but not for Play 2.x and. I found a possible solution but it is written in scala (I think) here: http://aishwaryasinghal.wordpress.com/2012/05/17/generating-excel-in-play-2/

I've tried to implement this but I think my imports doesn't work.

import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.*;

import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.openxml4j.opc.OPCPackage;

public static void generateExcel(List<Infoobject> list) {
        File file = new File("mydata.xlsx");
        FileOutputStream fileOut = new FileOutputStream(file);
        XSSFWorkbook wb = new XSSFWorkbook();
            //Workbook wb = new XSSFWorkbook(); Doesn't work either
        Sheet sheet = wb.createSheet("Sheet1");
        int rNum = 0;
        Row row = sheet.createRow(rNum);
        int cNum = 0;
        Cell cell = row.createCell(cNum);
        cell.setCellValue("My Cell Value");
        wb.write(fileOut);
        fileOut.close();
    }

It can't find either XSSFWorkbook, Sheet, Row and Cell. Do you guys know what the problem could be?

Can I use an another solution and just write it in clean Java? Or Javascript maybe?

And I do know that I have to iterate my List later to get some stuff in my excel file. This is just a try to see if it works.

like image 811
Kungen Avatar asked Mar 08 '26 02:03

Kungen


1 Answers

You just need to add the dependencies to the build.sbt:

libraryDependencies ++= Seq(
    javaJdbc,
    cache,
    javaEbean,
    "org.apache.poi" % "poi" % "3.8",
    "org.apache.poi" % "poi-ooxml" % "3.9"
)

Then, you need to regenerate your project (eg. play idea), and run as normal. The first time you run it will load all the new dependencies from Maven, and you should be good to go.

like image 70
sean.boyer Avatar answered Mar 11 '26 06:03

sean.boyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!