利用封装实现文件下载


public class CloudCustDownloadVO {

    /**
     * 下载文件名称
     */
    private String outputFileName;

    /**
     * 下载文件内容
     */
    private byte[] data;

    /**
     * Getter method for property <tt>outputFileName</tt>.
     *
     * @return property value of outputFileName
     */
    public String getOutputFileName() {
        return outputFileName;
    }

    /**
     * Setter method for property <tt>outputFileName</tt>.
     *
     * @param outputFileName value to be assigned to property outputFileName
     */
    public void setOutputFileName(String outputFileName) {
        this.outputFileName = outputFileName;
    }

    /**
     * Getter method for property <tt>data</tt>.
     *
     * @return property value of data
     */
    public byte[] getData() {
        return data;
    }

    /**
     * Setter method for property <tt>data</tt>.
     *
     * @param data value to be assigned to property data
     */
    public void setData(byte[] data) {
        this.data = data;
    }
}
 public Result<CloudCustDownloadVO> downloadRecod(Date startTime,Date endTime) throws Exception {
        Result<CloudCustDownloadVO> result = new Result<>();
  
           HashMap<String, Object> custParam = Maps.newHashMap();
           custParam.put("gmtStart", startTime);
           custParam.put("gmtEnd", endTime);
             
          List<questionRecordDO> questionRecordDOS = 
             questionRecordDAO.selectByQuery(custParam);
            
            
            //写文件
            Workbook wb = assembleQuestionRecord(questionRecordDOS);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            try {
                wb.write(os);
            } catch (IOException e) {
                logger.error(e);
                throw new Exception("下载试算结果错误,文件写入异常");
            }
            CloudCustDownloadVO downloadVO = new CloudCustDownloadVO();
            downloadVO.setData(os.toByteArray());
            downloadVO.setOutputFileName("result_" + DateUtil.getLongDateString(startTime) + "-"
                    + DateUtil.getLongDateString(endTime) + ".xlsx");

            result.setSuccess(true);
            result.setResultObj(downloadVO);

        return result;
    }
 
    private Workbook assembleQuestionRecord(List<questionRecordDO> questionRecordDOList){

        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("sheet name");
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 填充头部
        fillCellString(sheet, 0, 0, "ID");
        fillCellString(sheet, 0, 1, "code");

        // 填充内容
        int rowNum = 1;
        for (questionRecordDO questionRecord : questionRecordDOList) {

            fillCellString(sheet, rowNum, 0, questionRecord.getId());
            fillCellString(sheet, rowNum, 1, questionRecord.getCode());

            rowNum ++;
        }

        return workbook;
    }


    /**
     * 填充 cell string
     *
     * @param sheet
     * @param rowNum
     * @param colNum
     * @param value
     */
    private void fillCellString(Sheet sheet, int rowNum, int colNum, String value) {

        sheet.setColumnWidth(colNum, 3500);

        Row row = sheet.getRow(rowNum);
        if (row == null) {
            row = sheet.createRow(rowNum);
        }
        if (row.getCell(colNum) == null) {
            row.createCell(colNum);
        }
        Cell cell = row.getCell(colNum);

        cell.setCellValue(value);
    }
public class Result<T> implements Serializable {


    /** 是否成功。 */
    private boolean           success;

    /** 错误上下文。 */
    private ErrorContext      errorContext;

    /** 对象实例。 */
    private T                 resultObj;

    /**
     * 构造函数。
     */
    public Result() {
    }

    /**
     * 构造函数。
     * @param success 是否成功。
     * @param errorContext 错误上下文。
     * @param resultObj 对象实例。
     */
    public Result(boolean success, ErrorContext errorContext, T resultObj) {

        this.success = success;
        this.errorContext = errorContext;
        this.resultObj = resultObj;
    }

    /**
     * Getter method for property <tt>success</tt>.
     * 
     * @return property value of success
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * Setter method for property <tt>success</tt>.
     * 
     * @param success value to be assigned to property success
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * Getter method for property <tt>errorContext</tt>.
     * 
     * @return property value of errorContext
     */
    public ErrorContext getErrorContext() {
        return errorContext;
    }

    /**
     * Setter method for property <tt>errorContext</tt>.
     * 
     * @param errorContext value to be assigned to property errorContext
     */
    public void setErrorContext(ErrorContext errorContext) {
        this.errorContext = errorContext;
    }

    /**
     * Getter method for property <tt>resultObj</tt>.
     * 
     * @return property value of resultObj
     */
    public T getResultObj() {
        return resultObj;
    }

    /**
     * Setter method for property <tt>resultObj</tt>.
     * 
     * @param resultObj value to be assigned to property resultObj
     */
    public void setResultObj(T resultObj) {
        this.resultObj = resultObj;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值