java excel web 开源_easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiSingleExcelView.java · ...

/**

* Copyright 2013-2015 JueYue (qrb.jueyue@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 cn.afterturn.easypoi.view;

import java.util.Collection;

import java.util.List;

import java.util.Map;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Workbook;

import org.springframework.stereotype.Controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;

import cn.afterturn.easypoi.excel.ExcelExportUtil;

import cn.afterturn.easypoi.excel.entity.ExportParams;

import cn.afterturn.easypoi.excel.export.ExcelExportServer;

/**

* @author JueYue on 14-3-8. Excel 生成解析器,减少用户操作

*/

@SuppressWarnings("unchecked")

@Controller(NormalExcelConstants.EASYPOI_EXCEL_VIEW)

public class EasypoiSingleExcelView extends MiniAbstractExcelView {

public EasypoiSingleExcelView() {

super();

}

@Override

protected void renderMergedOutputModel(Map model, HttpServletRequest request,

HttpServletResponse response) throws Exception {

String codedFileName = "临时文件";

Workbook workbook = null;

if (model.containsKey(NormalExcelConstants.MAP_LIST)) {

List> list = (List>) model

.get(NormalExcelConstants.MAP_LIST);

if (list.size() == 0) {

throw new RuntimeException("MAP_LIST IS NULL");

}

workbook = ExcelExportUtil.exportExcel(

(ExportParams) list.get(0).get(NormalExcelConstants.PARAMS), (Class>) list.get(0)

.get(NormalExcelConstants.CLASS),

(Collection>) list.get(0).get(NormalExcelConstants.DATA_LIST));

for (int i = 1; i < list.size(); i++) {

new ExcelExportServer().createSheet(workbook,

(ExportParams) list.get(i).get(NormalExcelConstants.PARAMS), (Class>) list

.get(i).get(NormalExcelConstants.CLASS),

(Collection>) list.get(i).get(NormalExcelConstants.DATA_LIST));

}

} else {

workbook = ExcelExportUtil.exportExcel(

(ExportParams) model.get(NormalExcelConstants.PARAMS),

(Class>) model.get(NormalExcelConstants.CLASS),

(Collection>) model.get(NormalExcelConstants.DATA_LIST));

}

if (model.containsKey(NormalExcelConstants.FILE_NAME)) {

codedFileName = (String) model.get(NormalExcelConstants.FILE_NAME);

}

if (workbook instanceof HSSFWorkbook) {

codedFileName += HSSF;

} else {

codedFileName += XSSF;

}

if (isIE(request)) {

codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");

} else {

codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");

}

response.setHeader("content-disposition", "attachment;filename=" + codedFileName);

ServletOutputStream out = response.getOutputStream();

workbook.write(out);

out.flush();

}

}

一键复制

编辑

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值