001: /**********************************************************************************
002: *
003: * $Id: SpreadsheetDataFileWriterXls.java 22226 2007-03-06 17:42:53Z ray@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2007 The Regents of the University of California
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.jsf.spreadsheet;
022:
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.poi.hssf.usermodel.HSSFCell;
033: import org.apache.poi.hssf.usermodel.HSSFRow;
034: import org.apache.poi.hssf.usermodel.HSSFSheet;
035: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
036:
037: /**
038: *
039: */
040: public class SpreadsheetDataFileWriterXls implements
041: SpreadsheetDataFileWriter {
042: private static final Log log = LogFactory
043: .getLog(SpreadsheetDataFileWriter.class);
044:
045: public void writeDataToResponse(List<List<Object>> spreadsheetData,
046: String fileName, HttpServletResponse response) {
047: response.setContentType("application/vnd.ms-excel");
048: response.setHeader("Content-disposition",
049: "attachment; filename=" + fileName + ".xls");
050:
051: OutputStream out = null;
052: try {
053: out = response.getOutputStream();
054: getAsWorkbook(spreadsheetData).write(out);
055: out.flush();
056: } catch (IOException e) {
057: if (log.isErrorEnabled())
058: log.error(e);
059: } finally {
060: try {
061: if (out != null)
062: out.close();
063: } catch (IOException e) {
064: if (log.isErrorEnabled())
065: log.error(e);
066: }
067: }
068: }
069:
070: private HSSFWorkbook getAsWorkbook(
071: List<List<Object>> spreadsheetData) {
072: HSSFWorkbook wb = new HSSFWorkbook();
073: HSSFSheet sheet = wb.createSheet();
074: Iterator<List<Object>> dataIter = spreadsheetData.iterator();
075:
076: // By convention, the first list in the list contains column headers.
077: HSSFRow headerRow = sheet.createRow((short) 0);
078: List<Object> headerList = dataIter.next();
079: for (short i = 0; i < headerList.size(); i++) {
080: createCell(headerRow, i).setCellValue(
081: (String) headerList.get(i));
082: }
083:
084: short rowPos = 1;
085: while (dataIter.hasNext()) {
086: List<Object> rowData = dataIter.next();
087: HSSFRow row = sheet.createRow(rowPos++);
088: for (short i = 0; i < rowData.size(); i++) {
089: HSSFCell cell = createCell(row, i);
090: Object data = rowData.get(i);
091: if (data != null) {
092: if (data instanceof Double) {
093: cell
094: .setCellValue(((Double) data)
095: .doubleValue());
096: } else {
097: cell.setCellValue(data.toString());
098: }
099: }
100: }
101: }
102:
103: return wb;
104: }
105:
106: private HSSFCell createCell(HSSFRow row, short column) {
107: HSSFCell cell = row.createCell(column);
108: cell.setEncoding(HSSFCell.ENCODING_UTF_16);
109: return cell;
110: }
111:
112: }
|