/**
* Copyright (c) 2011-2013, kidzhou 周磊 (zhouleib1412@gmail.com)
*
* Licensed under the Apache 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.apache.org/licenses/LICENSE-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 com.jfinal.ext.render.excel;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import com.jfinal.log.Logger;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
@SuppressWarnings("serial")
public class PoiRender extends Render {
protected final Logger LOG = Logger.getLogger(getClass());
private final static String CONTENT_TYPE = "application/msexcel;charset=" + getEncoding();
private List<?> data;
private String[] headers;
private String sheetName = "sheet1";
private int cellWidth;
private String[] columns = new String[] {};
private String fileName = "file1.xls";
private int headerRow;
public PoiRender(List<?> data) {
this.data = data;
}
public static PoiRender me(List<?> data) {
return new PoiRender(data);
}
@Override
public void render() {
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
response.setContentType(CONTENT_TYPE);
OutputStream os = null;
try {
os = response.getOutputStream();
PoiKit.with(data).sheetName(sheetName).headerRow(headerRow).headers(headers).columns(columns)
.cellWidth(cellWidth).export().write(os);
} catch (Exception e) {
throw new RenderException(e);
} finally {
try {
if (os != null) {
os.flush();
os.close();
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
public PoiRender headers(String... headers) {
this.headers = headers;
return this;
}
public PoiRender headerRow(int headerRow) {
this.headerRow = headerRow;
return this;
}
public PoiRender columns(String... columns) {
this.columns = columns;
return this;
}
public PoiRender sheetName(String sheetName) {
this.sheetName = sheetName;
return this;
}
public PoiRender cellWidth(int cellWidth) {
this.cellWidth = cellWidth;
return this;
}
public PoiRender fileName(String fileName) {
this.fileName = fileName;
return this;
}
}