Skip to content

Commit

Permalink
SAK-33378 Jsf xlsx (sakaiproject#4809)
Browse files Browse the repository at this point in the history
* SpreadsheetDataFileWriterXl: @deprecated
SpreadsheetDataFileWriterXlsx: add
usermebership-tool: Export

* sections/sections-app/src/java/org/sakaiproject/tool/section/jsf/backingbean/RosterBean.java XLSX
  • Loading branch information
axxter99 authored and jonespm committed Sep 19, 2017
1 parent 73d6739 commit c0bb6b2
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 8 deletions.
5 changes: 5 additions & 0 deletions jsf/jsf-spreadsheet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>poi</artifactId>
<version>${sakai.poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${sakai.poi.version}</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
import org.slf4j.LoggerFactory;

/**
*
* @deprecated use {@link org.sakaiproject.jsf.spreadsheet.SpreadsheetDateFileWriterXlsx} , this will be removed after 12 - Oct 2017
*/
@Deprecated
public class SpreadsheetDataFileWriterXls implements SpreadsheetDataFileWriter {
private static final Logger log = LoggerFactory.getLogger(SpreadsheetDataFileWriter.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**********************************************************************************
*
* $Id$
*
***********************************************************************************
*
* Copyright (c) 2007, 2008 The Sakai Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**********************************************************************************/

package org.sakaiproject.jsf.spreadsheet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.sakaiproject.component.cover.ServerConfigurationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
*/
public class SpreadsheetDataFileWriterXlsx implements SpreadsheetDataFileWriter {
private static final Logger log = LoggerFactory.getLogger(SpreadsheetDataFileWriter.class);

public void writeDataToResponse(List<List<Object>> spreadsheetData, String fileName, HttpServletResponse response) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
SpreadsheetUtil.setEscapedAttachmentHeader(response, fileName + ".xlsx");

OutputStream out = null;
try {
out = response.getOutputStream();
getAsWorkbook(spreadsheetData).write(out);
out.flush();
} catch (IOException e) {
log.error(e.getMessage());
} finally {
try {
if (out != null) out.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}

private Workbook getAsWorkbook(List<List<Object>> spreadsheetData) {
Workbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet();
CellStyle headerCs = wb.createCellStyle();
Iterator<List<Object>> dataIter = spreadsheetData.iterator();

// Set the header style
headerCs.setBorderBottom(BorderStyle.THICK);
//TODO
headerCs.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);

// Set the font
CellStyle cellStyle = null;
String fontName = ServerConfigurationService.getString("spreadsheet.font");
if (fontName != null) {
Font font = wb.createFont();
font.setFontName(fontName);
headerCs.setFont(font);
cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
}

// By convention, the first list in the list contains column headers.
Row headerRow = sheet.createRow((short)0);
List<Object> headerList = dataIter.next();
for (short i = 0; i < headerList.size(); i++) {
Cell headerCell = createCell(headerRow, i);
headerCell.setCellValue((String)headerList.get(i));
headerCell.setCellStyle(headerCs);
//TODO
//sheet.autoSizeColumn(i);
}

short rowPos = 1;
while (dataIter.hasNext()) {
List<Object> rowData = dataIter.next();
Row row = sheet.createRow(rowPos++);
for (short i = 0; i < rowData.size(); i++) {
Cell cell = createCell(row, i);
Object data = rowData.get(i);
if (data != null) {
if (data instanceof Double) {
cell.setCellValue(((Double)data).doubleValue());
} else {
cell.setCellValue(data.toString());
}
if (cellStyle != null) {
cell.setCellStyle(cellStyle);
}
}
}
}

return wb;
}

private Cell createCell(Row row, short column) {
Cell cell = row.createCell(Integer.valueOf(column).intValue());
return cell;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;

import javax.faces.application.Application;
import javax.faces.component.UIColumn;
Expand All @@ -43,17 +43,17 @@
import javax.faces.model.SelectItem;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.myfaces.custom.sortheader.HtmlCommandSortHeader;
import org.sakaiproject.jsf.spreadsheet.SpreadsheetDataFileWriterXls;
import org.sakaiproject.jsf.spreadsheet.SpreadsheetDataFileWriterXlsx;
import org.sakaiproject.jsf.spreadsheet.SpreadsheetUtil;
import org.sakaiproject.section.api.coursemanagement.CourseSection;
import org.sakaiproject.section.api.coursemanagement.EnrollmentRecord;
import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
import org.sakaiproject.section.api.coursemanagement.SectionEnrollments;
import org.sakaiproject.tool.section.decorator.EnrollmentDecorator;
import org.sakaiproject.tool.section.jsf.JsfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
Expand Down Expand Up @@ -371,7 +371,7 @@ public void export(ActionEvent event){
spreadsheetData.add(row);
}
String spreadsheetName = getDownloadFileName(getCourse().getTitle());
SpreadsheetUtil.downloadSpreadsheetData(spreadsheetData, spreadsheetName, new SpreadsheetDataFileWriterXls());
SpreadsheetUtil.downloadSpreadsheetData(spreadsheetData, spreadsheetName, new SpreadsheetDataFileWriterXlsx());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sakaiproject.jsf.spreadsheet.SpreadsheetDataFileWriterCsv;
import org.sakaiproject.jsf.spreadsheet.SpreadsheetDataFileWriterXls;
import org.sakaiproject.jsf.spreadsheet.SpreadsheetDataFileWriterXlsx;
import org.sakaiproject.jsf.spreadsheet.SpreadsheetUtil;
import org.sakaiproject.util.ResourceLoader;

Expand All @@ -50,7 +50,7 @@ public class Export {
* @param prefixFileName A filename prefix. This will be appended with unique data and the proper file extension.
*/
public static void writeAsXls(List<List<Object>> content, String prefixFileName) {
SpreadsheetUtil.downloadSpreadsheetData(content, getFileName(prefixFileName), new SpreadsheetDataFileWriterXls());
SpreadsheetUtil.downloadSpreadsheetData(content, getFileName(prefixFileName), new SpreadsheetDataFileWriterXlsx());
}

public static void writeAsCsv(List<List<Object>> content, String prefixFileName) {
Expand Down

0 comments on commit c0bb6b2

Please sign in to comment.