org.sakaiproject.jsf.spreadsheet.SpreadsheetDataFileWriterXlsx Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsf-spreadsheet Show documentation
Show all versions of jsf-spreadsheet Show documentation
This provides some utility classes to support downloading
of spreadsheet-style data as an XLS or CSV file. It has
been broken out into its own artifact to avoid adding a
POI dependency to the standard Sakai JSF components JAR.
/**********************************************************************************
*
* $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 lombok.extern.slf4j.Slf4j;
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.IndexedColors;
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;
/**
*
*/
@Slf4j
public class SpreadsheetDataFileWriterXlsx implements SpreadsheetDataFileWriter {
public void writeDataToResponse(List> 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> spreadsheetData) {
Workbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet();
CellStyle headerCs = wb.createCellStyle();
Iterator> dataIter = spreadsheetData.iterator();
// Set the header style
headerCs.setBorderBottom(BorderStyle.THICK);
headerCs.setFillBackgroundColor(IndexedColors.BLUE_GREY.getIndex());
// 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
© 2015 - 2024 Weber Informatics LLC | Privacy Policy