com.envisioncn.it.super_sonic.showcase.evaluation.utils.ExcelUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.envisioncn.it.super_sonic.showcase.evaluation.utils.ExcelUtils.java

Source

/******************************************************************************
 * @File name : ExcelUtils.java
 *
 * @Author : bo.chen
 *
 * @Date : 2016825 ?6:31:33
 *
 * @Copyright Notice: Copyright (c) 2016 Envision, Inc. All Rights Reserved.
 *            This software is published under the terms of the Envision
 *            Software License version 1.0, a copy of which has been included
 *            with this distribution in the LICENSE.txt file.
 *
 *****************************************************************************/
package com.envisioncn.it.super_sonic.showcase.evaluation.utils;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

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

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

    public static final String DOWNFILENAME = "Challenger?.xlsx";
    //
    public static final String ALL = "0";
    // ?
    public static final String MDEVA = "1";
    // ?
    public static final String TDEVA = "2";
    // ?
    public static final String SEND = "1";
    // 
    public static final String GET = "2";

    public static final String XLS = ".xls";

    public static final String XLSX = ".xlsx";

    public static final String VERSION2003 = "Version2003";

    public static final String VERSION2007 = "Version2007";

    public static final String VERSION = VERSION2003;

    // 
    public static final String APPLICATION = "3";

    /*
     * Excel2003Excel2007
     */
    public static String initType(String fileName) {
        String version = VERSION;

        if (fileName != null) {
            int index = fileName.indexOf(".");
            String suffex = fileName.substring(index);

            if (XLS.equals(suffex)) {
                version = VERSION2003;

            } else if (XLSX.equals(suffex)) {
                version = VERSION2007;
            }
        }
        return version;
    }

    /*
     * ???xlsxlsx
     */
    public static Workbook createWorkbook(String fileName, InputStream in) throws IOException {
        String type = initType(fileName);
        Workbook workbook = null;

        if (type.equals(VERSION2003)) {
            workbook = new HSSFWorkbook(in);
        } else if (type.equals(VERSION2007)) {
            workbook = new XSSFWorkbook(in);
        }

        return workbook;
    }

    /**
     * Excel
     * 
     * @param hssfCell
     *            Excel??
     * @return Excel??
     */
    @SuppressWarnings("static-access")
    public static String getValue(Cell Cell) {

        // 
        if (Cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {

            return String.valueOf(Cell.getBooleanCellValue());
            // 
        } else if (Cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {

            return String.valueOf(new BigDecimal(Cell.getNumericCellValue()));
            // 
        } else {

            return String.valueOf(Cell.getStringCellValue());
        }
    }

    /*
     * ?(?code????)
     */
    public static void createTitle(XSSFRow headerRow, String code) {
        if (code.equals(MDEVA)) {
            headerRow.createCell(0).setCellValue("?id");
            headerRow.createCell(1).setCellValue("?");
            headerRow.createCell(2).setCellValue("??");
            headerRow.createCell(3).setCellValue("");
            headerRow.createCell(4).setCellValue("");
            headerRow.createCell(5).setCellValue("");
            headerRow.createCell(6).setCellValue("?");
            headerRow.createCell(7).setCellValue("??");
            headerRow.createCell(8).setCellValue("");
            headerRow.createCell(9).setCellValue("");
            headerRow.createCell(10).setCellValue("");
            headerRow.createCell(11).setCellValue("?");
            headerRow.createCell(12).setCellValue("");
            headerRow.createCell(13).setCellValue("?");
            headerRow.createCell(14).setCellValue("?");
            headerRow.createCell(15).setCellValue("");
            headerRow.createCell(16).setCellValue("?");
            headerRow.createCell(17).setCellValue("");
            headerRow.createCell(18).setCellValue("");
        } else if (code.equals(TDEVA)) {
            headerRow.createCell(0).setCellValue("?id");
            headerRow.createCell(1).setCellValue("?");
            headerRow.createCell(2).setCellValue("??");
            headerRow.createCell(3).setCellValue("");
            headerRow.createCell(4).setCellValue("");
            headerRow.createCell(5).setCellValue("");
            headerRow.createCell(6).setCellValue("?");
            headerRow.createCell(7).setCellValue("??");
            headerRow.createCell(8).setCellValue("");
            headerRow.createCell(9).setCellValue("");
            headerRow.createCell(10).setCellValue("");
            headerRow.createCell(11).setCellValue("?");
            headerRow.createCell(12).setCellValue("");
            headerRow.createCell(13).setCellValue("?");
            headerRow.createCell(14).setCellValue("?");
            headerRow.createCell(15).setCellValue("");
            headerRow.createCell(16).setCellValue("?");
            headerRow.createCell(17).setCellValue("");
        } else if (code.equals(ALL)) {
            headerRow.createCell(0).setCellValue("id");
            headerRow.createCell(1).setCellValue("?");
            headerRow.createCell(2).setCellValue("??");
            headerRow.createCell(3).setCellValue("");
            headerRow.createCell(4).setCellValue("");
            headerRow.createCell(5).setCellValue("");
            headerRow.createCell(6).setCellValue("?");
            headerRow.createCell(7).setCellValue("??");
            headerRow.createCell(8).setCellValue("");
            headerRow.createCell(9).setCellValue("");
            headerRow.createCell(10).setCellValue("");
            headerRow.createCell(11).setCellValue("?");
            headerRow.createCell(12).setCellValue("");
            headerRow.createCell(13).setCellValue("?");
            headerRow.createCell(14).setCellValue("?");
            headerRow.createCell(15).setCellValue("");
            headerRow.createCell(16).setCellValue("?");
            headerRow.createCell(17).setCellValue("");
            headerRow.createCell(18).setCellValue("");
        }
    }

    /*
     * 
     */
    public static void downFile(HttpServletResponse response, HttpServletRequest request, XSSFWorkbook xssfWorkbook)
            throws IOException {
        // -----
        // ??Content-TypeContent-Disposition
        // ??
        String downFilename = DOWNFILENAME;
        // ?MIME
        String contentType = request.getServletContext().getMimeType(downFilename);
        // MIME?
        response.setContentType(contentType);
        // ?
        String agent = request.getHeader("user-agent");
        // ????
        downFilename = FileUtils.encodeDownloadFilename(downFilename, agent);
        // ????
        String contentDisposition = "attachment;filename=" + downFilename;
        // ?????
        response.setHeader("Content-Disposition", contentDisposition);

        // excel?
        xssfWorkbook.write(response.getOutputStream());
    }
}