gpt4 book ai didi

Java 将集合的集合导出到 Excel

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:12 24 4
gpt4 key购买 nike

在使用 OSWorkflow 框架的项目中,我的任务是将用户权限导出到 Excel 报告。我从数据库中获取了它们,完成了所有必要的检查和操作。在此步骤中,我有包含其他集合的集合,它们也包含集合等等。

我认为报告的结构如下: 用户1 权限1(例如期刊类型) 期刊类型1 新(这些可通过此日记操作获得) 编辑 删除 看法 期刊类型2 看法 权限2 连接权限 真的 权限3 导航权限 菜单_文档 归文件例 文件报告 期刊组 用户2...等等。

所以我必须迭代我的集合并将它们导出到 Excel。我知道我必须使用递归进行迭代。但我不知道导出到excel使用哪个框架。谁能展示一些类似于我的对象的迭代示例并导出到 Excel?这是我的结构在调试器中的样子: enter image description here

最佳答案

经过一番研究,我意识到递归在这里不起作用,因为我的对象中有不同的集合(首先我正在考虑某种双向递归,一种是列表,第二种是 map ,但无法实现它)。我知道,这可能不是最好的解决方案,但我必须使用迭代。所以我在这里有两个任务,首先迭代所有数据,然后导出到 Excel。我将它们分开并首先编写迭代并输出到控制台(为了更容易调试)然后我使用 Apache POI 添加了 Excel 导出。迭代包含 if`s{},因为集合具有不同的深度级别。这是代码:

private void exportPermissionDataToExcel(SortedMap allPermissions, HttpServletResponse response) {

PropertyManager props = ResourcesProperties.getInstance();

HSSFWorkbook wb = null;
try {

String headerString = "attachment; filename=\"" + EXCEL_FILE_NAME + ".xls\";";
wb = new HSSFWorkbook();

CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);

HSSFSheet workSheet = wb.createSheet(EXCEL_FILE_NAME);
workSheet.createRow(1);

int columnNumber = 0;

for (Object pair : allPermissions.entrySet()) {

Object key = ((Map.Entry) pair).getKey();
Object values = ((Map.Entry) pair).getValue();

int rowNumber = 0;
Row row = workSheet.createRow(rowNumber);
Cell cell = row.createCell(columnNumber);
rowNumber++;

if (key instanceof User) {
cell.setCellValue(((User) key).getFullName() + " (" + ((User) key).getUsername() + ")");
} else {
cell.setCellValue(((Group) key).getName());
}
cell.setCellStyle(style);

for (Object permissionsList : (List) values) {
for (Object permissionValue : ((Map) permissionsList).entrySet()) {

Object key1 = ((Map.Entry) permissionValue).getKey();
Object listOfValues1 = ((Map.Entry) permissionValue).getValue();

columnNumber = 1;
row = workSheet.createRow(rowNumber);
cell = row.createCell(columnNumber);
rowNumber++;
cell.setCellValue(props.getProperty(key1.toString()));
cell.setCellStyle(style);

for (Object permissionsList1 : (List) listOfValues1) {

for (Object permissionMap : (List) permissionsList1) {

for (Object permissionMap1 : ((Map) permissionMap).entrySet()) {

Object key2 = ((Map.Entry) permissionMap1).getKey();
Object listOfValues2 = ((Map.Entry) permissionMap1).getValue();

columnNumber = 2;
//if property not found returns null, some records can be already transalted, translations for translations cannot be found => null
String message2 = props.getProperty(key2.toString());
if (message2 == null) {
rowNumber = createCellAndSetValue(workSheet, row, cell, rowNumber, columnNumber,key2.toString());
} else {
rowNumber = createCellAndSetValue(workSheet, row, cell, rowNumber, columnNumber, message2);
}

if (!key1.equals("lv.alise.usermanager.permissions.ConnectPermission")) {

for (Object permissionValue3 : (List) listOfValues2) {
if (key1.equals("lv.alise.usermanager.permissions.WorkflowPermission")) {

for (Object permissionMap2 : ((Map) permissionValue3).entrySet()) {

Object key3 = ((Map.Entry) permissionMap2).getKey();
Object listOfValues3 = ((Map.Entry) permissionMap2).getValue();

columnNumber = 3;
rowNumber = createCellAndSetValue(workSheet, row, cell, rowNumber, columnNumber,props.getProperty(key3.toString()) );

for (Object permissionValue4 : (List) listOfValues3) {
columnNumber = 4;
rowNumber = createCellAndSetValue(workSheet, row, cell, rowNumber, columnNumber,props.getProperty(permissionValue4.toString()) );
}
}
} else {
columnNumber = 3;
rowNumber = createCellAndSetValue(workSheet, row, cell, rowNumber, columnNumber,props.getProperty(permissionValue3.toString()) );
}
}
}
}
}
}
}
}
}

response.setContentType("application/octetstream");
response.setHeader("Content-Disposition", headerString);
OutputStream out = response.getOutputStream();
wb.write(out);
out.close();
wb.close();

} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private int createCellAndSetValue(HSSFSheet workSheet, Row row, Cell cell, int rowNumber, int columnNumber, String value) {
row = workSheet.createRow(rowNumber);
cell = row.createCell(columnNumber);
rowNumber++;
cell.setCellValue(value);
return rowNumber;
}

也许有人会发现它有用。

关于Java 将集合的集合导出到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45441964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com