【发布时间】:2013-07-04 12:41:44
【问题描述】:
我需要从对象列表生成的表中生成/导出 excel 文件。我可以看到 Play 1.x 有一个模块,但 Play 2.x 没有。我找到了一个可能的解决方案,但它是用 scala 写的(我认为):http://aishwaryasinghal.wordpress.com/2012/05/17/generating-excel-in-play-2/
我已尝试实现此功能,但我认为我的导入不起作用。
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();
}
它找不到 XSSFWorkbook、Sheet、Row 和 Cell。大家知道问题出在哪里吗?
我可以使用另一种解决方案并只用干净的 Java 编写它吗?还是 Javascript?
而且我知道我必须稍后迭代我的列表才能在我的 excel 文件中获取一些东西。这只是尝试看看它是否有效。
【问题讨论】:
-
你的类路径中有 apache-poi 库吗?这就是这些类的来源。
-
如果您想在生成 excel 文件方面获得更多 scala-ish 体验,您可能想尝试github.com/folone/poi.scala
-
只有 poi-examples-3.9-20121203.jar 我应该放在类路径中吗? Eclipse:属性>Java 构建路径>库>添加 JAR?我应该把文件放在哪里?在播放框架库映射?
-
谢谢@Kayaman,它似乎有效!
标签: java playframework playframework-2.0 export-to-excel