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

两盒软妹~` 提交于 2019-12-21 20:50:22

问题


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.


回答1:


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.




回答2:


Try using Jasper Report it is a good way for exporting data from models into EXCEL file/Report. If you want some samples i can send you them ;)



来源:https://stackoverflow.com/questions/17470597/generate-export-excel-file-from-java-play-framework-2-0-listobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!