package com.dxhy.base.controller;

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

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.DateUtils;

/**
 * 导入勾选模板导出
 *
 * @author kangzq
 * @date 2019年5月11日
 */
@RestController
public class BaseModelExportController extends AbstractController {

    /**
     * 导入勾选导出模板下载
     *
     * @param response
     *            请求参数
     * @throws IOException
     *             异常
     */

    @GetMapping("/drgx/dcmb")
    public void modelExport(HttpServletResponse response) throws IOException {

        /*
         * 文件名前缀
         */
        String fileNamePrefix = "importCheck";
        /*
         * 文件名后缀
         */
        String fileNameSurfix = ".xls";
        /*
         * 文件名中间分隔符
         */
        String fileSplit = "_";
        // 导出文件名

        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFSheet sheet = wb.createSheet("sheet1");
        sheet.setColumnWidth(0, 2500);
        sheet.setColumnWidth(1, 5000);
        sheet.setColumnWidth(2, 5000);
        sheet.setColumnWidth(3, 5000);
        sheet.setColumnWidth(4, 5000);
        sheet.setColumnWidth(5, 5000);
        HSSFRow row1 = sheet.createRow(0);
        // 创建单元格并设置单元格内容
        row1.createCell(0).setCellValue("序号");
        row1.createCell(1).setCellValue("发票代码");
        row1.createCell(2).setCellValue("发票号码");
        row1.createCell(3).setCellValue("开票日期");
        row1.createCell(4).setCellValue("金额");
        row1.createCell(5).setCellValue("抵扣税额");

        // 输出Excel文件
        OutputStream output = response.getOutputStream();
        response.reset();
        String ftpFileName = fileSplit + fileNamePrefix + fileSplit + DateUtils.getStringDateShort() + fileSplit
            + (new Date()).getTime() + fileNameSurfix;
        response.setHeader("Content-disposition", "attachment; filename=" + ftpFileName);
        response.setContentType("application/msexcel");
        wb.write(output);
        output.close();

    }

}
