HSSFWorkbook 생성 excel 중국어 이름 다운로드 디코드

package com.abing;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;


public class ExportExcelDataTool {


    private final HSSFWorkbook  workbook;

    private final HSSFSheet     sheet;

    private final HSSFCellStyle cellStyle;


    public ExcelDataModelTool() {
        workbook = new HSSFWorkbook();
        sheet = workbook.createSheet();
        workbook.setSheetName(0, " ");

        Font font = workbook.createFont();
        font.setColor(HSSFColor.RED.index);
        cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
    }

    /**
     * 
     * @param response
     */
    public void exportExcel(HttpServletResponse response) {
        createSheet(sheet);
        exportExcel2Client(workbook, response);
    }

    /**  
     * @param sheet 
     */
    private void createSheet(HSSFSheet sheet) {
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0, 1);
        cell.setCellValue(" ");

        row = sheet.createRow(1);
        cell = row.createCell(0, 1);
        cell.setCellValue("42013019923000000037792");
    }


    private void exportExcel2Brower(HSSFWorkbook workbook, HttpServletResponse response) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String nowDate = sdf.format(new Date());
        String fileName = "attachment; filename= -" + nowDate + ".xls";
        OutputStream out = null;
        try {
            response.setHeader("Content-disposition", new String(fileName.getBytes("gbk"),
                "ISO-8859-1"));
            response.setContentType("application/msexcel;charset=GBK");
            out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            //
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    //
                }
            }
        }
    }
}

좋은 웹페이지 즐겨찾기