在使用OSWorkflow框架的项目中,我有任务将用户权限导出到Excel报表。我从数据库中取出它们,完成了所有必要的检查和操作。在这一步,我有包含其他集合的Collection,它们也包含集合等等。
在我看来,报告结构如下: USER1 Permission1(例如期刊类型) JournalType1 新的(这些日记操作可用) 编辑 删除 视图 JournalType2 视图 Permission2 ConnectionPermission 真正 Permission3 NavigationPermission menu_documents archiveCases archiveReport JournalGroups USER2 ... 等等。
所以我必须通过我的收藏集迭代并将它们导出为excel。 我知道我将使用递归进行迭代。但我不知道哪个框架可以导出到excel使用。 任何人都可以通过类似于我的对象展示迭代的例子并导出到excel吗? 就像我的结构在调试器中看起来一样:
答案 0 :(得分:0)
经过一些研究,我意识到递归在这里不起作用,因为我的对象中有不同的集合(首先我考虑的是某种双向递归,一个是由地图列出的第二个但是无法实现它)。我知道,可能它不是最好的解决方案,但我不得不使用迭代。 所以我在这里有两个任务,首先通过所有数据迭代,然后第二次导出到excel。我将它们分开并首先用输出到控制台编写迭代(以便于调试) 然后我使用Apache POI添加了excel导出。 迭代包含if {},因为集合具有不同的深度级别。 这是代码:
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;
}
可能有人会发现它很有用。