从Java生成dynamicExcel

我们已经预先定义了Excel文档结构,其中包含大量的公式和macros。

在Excel下载过程中,通过Java应用程序,我们在Excel中用数据填充某些单元格。 用户打开Excel后下载后,embedded在其中的macros和公式将读取预填充的数据并相应地运行。

我们现在正在使用ExtenXLS从Java生成Dynamic Excel文档。 许可证是基于CPU的,不支持双核CPU的Box。 我们被迫购买更多的许可证。

有没有更好的工具,我们可以看看它是免费的,产品和支持成本是最小的(支持是必须的),许可证是简单的?

我非常喜欢使用Apache POI Project HSSF库( http://poi.apache.org/ ) – 使用起来相当简单。 我没有那么深入地使用它,但它似乎相当强大。 此外,还有JExcelAPI( http://sourceforge.net/projects/jexcelapi/ ),我没有使用过。

如果您的用户将有最新版本的Excel,手动调整XML文件格式并不难。 只需将现有文档保存为XML,然后find要replace的位置即可。

我在一个名为XLLoop的开源项目上工作 – 这个框架允许您将POJO函数作为Excel函数公开。

所以,而不是用数据填充Excel工作表,你可以创build一个函数下载数据,并填充到位。

使用Apache POI

Apache POI的 Maven依赖关系。

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> 

试试这个代码片段

 import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; /* * Here we will learn how to create Excel file and header for the same. */ public class CreateExcelFile { int rownum = 0; HSSFSheet firstSheet; Collection<File> files; HSSFWorkbook workbook; File exactFile; { workbook = new HSSFWorkbook(); firstSheet = workbook.createSheet("FIRST SHEET"); Row headerRow = firstSheet.createRow(rownum); headerRow.setHeightInPoints(40); } public static void main(String args[]) throws Exception { List<String> headerRow = new ArrayList<String>(); headerRow.add("Employee No"); headerRow.add("Employee Name"); headerRow.add("Employee Address"); List<String> firstRow = new ArrayList<String>(); firstRow.add("1111"); firstRow.add("Gautam"); firstRow.add("India"); List<List> recordToAdd = new ArrayList<List>(); recordToAdd.add(headerRow); recordToAdd.add(firstRow); CreateExcelFile cls = new CreateExcelFile(recordToAdd); cls.createExcelFile(); } void createExcelFile(){ FileOutputStream fos = null; try { fos=new FileOutputStream(new File("ExcelSheet.xls")); HSSFCellStyle hsfstyle=workbook.createCellStyle(); hsfstyle.setBorderBottom((short) 1); hsfstyle.setFillBackgroundColor((short)245); workbook.write(fos); } catch (Exception e) { e.printStackTrace(); } } CreateExcelFile(List<List> l1) throws Exception { try { for (int j = 0; j < l1.size(); j++) { Row row = firstSheet.createRow(rownum); List<String> l2= l1.get(j); for(int k=0; k<l2.size(); k++) { Cell cell = row.createCell(k); cell.setCellValue(l2.get(k)); } rownum++; } } catch (Exception e) { e.printStackTrace(); } finally { } } }