org.springframework.web.servlet.view.document.XlsView Maven / Gradle / Ivy
package org.springframework.web.servlet.view.document;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.dbunit.database.AmbiguousTableNameException;
import org.dbunit.dataset.Column;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.DefaultDataSet;
import org.dbunit.dataset.DefaultTable;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.datatype.DataType;
import org.dbunit.dataset.excel.XlsDataSetWriterCustom;
import org.springframework.data.domain.Page;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriUtils;
public class XlsView extends AbstractXlsView {
public static final String SHEETNAME = "sheetname";// table
public static final String HEADER = "header";// th
public static final String ROW = "row";// tr
public static final String DATA = "data";// td
public static final String INDEX = "index";
@Override
protected void buildExcelDocument(Map model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
List content = null;
Map meta = null;
String filename = null;
IDataSet dataSet = null;
Charset charset = null;
for (Entry entry : model.entrySet()) {
if (entry.getValue() instanceof Page) {
content = ((Page) entry.getValue()).getContent();
}
if (entry.getValue() instanceof List) {
content = (List) entry.getValue();
}
if (entry.getValue() instanceof Map) {
meta = (Map) entry.getValue();
}
if (entry.getValue() instanceof String) {
filename = (String) entry.getValue();
}
if (entry.getValue() instanceof IDataSet) {
dataSet = (IDataSet) entry.getValue();
}
if (entry.getValue() instanceof Charset) {
charset = (Charset) entry.getValue();
}
}
if (dataSet == null) {
Assert.notNull(content, "'content' must not be null");
Assert.notNull(meta, "'meta' must not be null");
dataSet = dataSet(filename, meta, content);
}
new XlsDataSetWriterCustom().write(dataSet, workbook);
if (StringUtils.hasText(filename)) {
HttpHeaders headers = new HttpHeaders();
try {
// Content-Disposition: attachment; filename="json.txt"; filename*=UTF-8''json.txt
headers.setContentDispositionFormData("attachment", UriUtils.encode(filename, StandardCharsets.UTF_8.name()), charset);
// response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
// headers.getFirst(HttpHeaders.CONTENT_DISPOSITION));
// response.setHeader("Content-Transfer-Encoding", "binary");
}
catch (UnsupportedEncodingException e) {
// ignore
}
}
// new XlsDataSet(file)
// workbook.createSheet("")
// workbook.createSheet(sheetname);
// String sheetName = (String) model.get("sheetname");
// List headers = (List) model.get("headers");
// List> results = (List>) model.get("results");
// List numericColumns = new ArrayList();
// if (model.containsKey("numericcolumns"))
// numericColumns = (List) model.get("numericcolumns");
// // BUILD DOC
// HSSFSheet sheet = workbook.createSheet(sheetName);
// sheet.setDefaultColumnWidth((short) 12);
// int currentRow = 0;
// short currentColumn = 0;
// // CREATE STYLE FOR HEADER
// HSSFCellStyle headerStyle = workbook.createCellStyle();
// HSSFFont headerFont = workbook.createFont();
// headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// headerStyle.setFont(headerFont);
// // POPULATE HEADER COLUMNS
// HSSFRow headerRow = sheet.createRow(currentRow);
// for (String header : headers) {
// HSSFRichTextString text = new HSSFRichTextString(header);
// HSSFCell cell = headerRow.createCell(currentColumn);
// cell.setCellStyle(headerStyle);
// cell.setCellValue(text);
// currentColumn++;
// }
// // POPULATE VALUE ROWS/COLUMNS
// currentRow++;// exclude header
// for (List result : results) {
// currentColumn = 0;
// HSSFRow row = sheet.createRow(currentRow);
// for (String value : result) {// used to count number of columns
// HSSFCell cell = row.createCell(currentColumn);
// if (numericColumns.contains(headers.get(currentColumn))) {
// cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
// cell.setCellValue(NumUtils.extractDoubleOrZero(value));
// }
// else {
// HSSFRichTextString text = new HSSFRichTextString(value);
// cell.setCellValue(text);
// }
// currentColumn++;
// }
// currentRow++;
// }
// Row row = sheet.createRow(0);
// CellStyle style = workbook.createCellStyle();
// style.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
// style.setFillPattern(CellStyle.ALIGN_FILL);
// Font font = workbook.createFont();
// font.setColor(IndexedColors.RED.getIndex());
// style.setFont(font);
// Cell cell1 = row.createCell(0);
// cell1.setCellValue("ID");
// cell1.setCellStyle(style);
// Cell cell2 = row.createCell(1);
// cell2.setCellValue("TITLE");
// cell2.setCellStyle(style);
// Cell cell3 = row.createCell(2);
// cell3.setCellValue("DESCRIPTION");
// cell3.setCellStyle(style);
// Cell cell4 = row.createCell(3);
// cell4.setCellValue("PUBDATE");
// cell4.setCellStyle(style);
// Cell cell5 = row.createCell(4);
// cell5.setCellValue("LINK");
// cell5.setCellStyle(style);
// Object ob = model.get("contents");
// if (ob instanceof List) {
// for (int i = 0; i < ((List) ob).size(); i++) {
// Object feedObj = ((List) ob).get(i);
// MyContent myContent = (MyContent) feedObj;
// row = sheet.createRow(i + 1);
// cell1 = row.createCell(0);
// cell1.setCellValue(myContent.getId());
// cell2 = row.createCell(1);
// cell2.setCellValue(myContent.getTitle());
// cell3 = row.createCell(2);
// cell3.setCellValue(myContent.getDescription());
// cell4 = row.createCell(3);
// cell4.setCellValue(myContent.getPubDate());
// cell5 = row.createCell(4);
// cell5.setCellValue(myContent.getLink());
// }
// }
}
/**
* @see org.springframework.expression.spel.support.StandardEvaluationContext#StandardEvaluationContext(Object)
*/
private IDataSet dataSet(String sheetname, Map meta, List content) {
List