com.qihang.winter.poi.excel.view.JeecgTemplateWordView.java Source code

Java tutorial

Introduction

Here is the source code for com.qihang.winter.poi.excel.view.JeecgTemplateWordView.java

Source

/**
 * 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 com.qihang.winter.poi.excel.view;

import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.view.AbstractView;

/**
 * Word?
 * 
 * @author Zerrion
 * @date 2014630 ?9:15:49
 */
@SuppressWarnings("unchecked")
@Controller(com.qihang.winter.poi.excel.entity.vo.TemplateWordConstants.JEECG_TEMPLATE_WORD_VIEW)
public class JeecgTemplateWordView extends AbstractView {

    private static final String CONTENT_TYPE = "application/msword";

    public JeecgTemplateWordView() {
        setContentType(CONTENT_TYPE);
    }

    public boolean isIE(HttpServletRequest request) {
        return (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0
                || request.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11.0") > 0) ? true : false;
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String codedFileName = ".docx";
        if (model.containsKey(com.qihang.winter.poi.excel.entity.vo.TemplateWordConstants.FILE_NAME)) {
            codedFileName = (String) model
                    .get(com.qihang.winter.poi.excel.entity.vo.TemplateWordConstants.FILE_NAME) + ".docx";
        }
        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);
        XWPFDocument document = com.qihang.winter.poi.word.WordExportUtil.exportWord07(
                (String) model.get(com.qihang.winter.poi.excel.entity.vo.TemplateWordConstants.URL),
                (Map<String, Object>) model
                        .get(com.qihang.winter.poi.excel.entity.vo.TemplateWordConstants.MAP_DATA));
        ServletOutputStream out = response.getOutputStream();
        document.write(out);
        out.flush();
    }
}