泛微api返回附件


package com.api.pxwsemi.down;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;

import com.oneWorld.ecology.doc.utils.DocFileUtil;
import com.oneWorld.ecology.doc.utils.DocIo;
import com.oneWorld.ecology.doc.utils.ImagefileVo;
import com.oneWorld.ecology.workflow.utils.FormUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.file.ExcelFile;
import weaver.file.ExcelParseForPOI;
import weaver.file.ExcelRow;
import weaver.file.ExcelSheet;
import weaver.file.FileUploadToPath;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import javax.ws.rs.core.Response;

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;

@Path("/pxwsemi/workflow/file/download")
public class DownloadFile {

    @GET
    @Path("/file")
    @Produces({"application/octet-stream"})
    public Response downfile(@Context HttpServletRequest request, @Context HttpServletResponse response) throws ServletException, IOException {
        String docid = request.getParameter("docid");
        DocFileUtil docFileUtil=new DocFileUtil();
        ImagefileVo imagefileVo=docFileUtil.getDoc(docid);
        String filename=imagefileVo.getImagefilename();
        DocIo docIo=docFileUtil.getDocIo(imagefileVo);
        InputStream image=docIo.getFileIo();
        String excelName = filename;
        excelName = new String(excelName.replaceAll("<", "").replaceAll(">", "").replaceAll("&lt;", "").replaceAll("&gt;", "").getBytes("UTF-8"), "ISO-8859-1");

        return Response.ok(image)
                .header("Content-disposition", "attachment;filename=" + excelName)
                .header("Cache-Control", "no-cache")
                .build();
    }

    @GET
    @Path("/table")
    @Produces({"application/octet-stream"})
    public Response exportDt(@Context HttpServletRequest request, @Context HttpServletResponse response) throws ServletException, IOException {

        String requestid = request.getParameter("requestid");
        String dtindex = request.getParameter("dtindex");        //第几个明细

        XSSFWorkbook workbook = new XSSFWorkbook();

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//上下居中
        cellStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());//设置背景色
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//必须设置 否则无效
        //设置边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);

        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);

        //表名
        String tablename= FormUtils.getFormTableByRequestId(requestid);

        XSSFSheet sheet = workbook.createSheet("表一");

        RecordSet rs=new RecordSet();
        String sql = "select d.* from "+tablename+" m, "+tablename+"_dt"+dtindex+" d " +
                " where m.id = d.mainid " +
                " and m.requestId = " + requestid;
        rs.executeQuery(sql);
        List<String> ColumnName= Arrays.asList(rs.getColumnName());

        //excle表头
        Row row = sheet.createRow(0);
        for (int i = 0; i < ColumnName.size(); i++) {
            if(ColumnName.get(i).equals("id") || ColumnName.get(i).equals("mainid")){
                continue;
            }
            Cell cello = row.createCell(i-2);
            cello.setCellValue(getColumname(tablename,ColumnName.get(i)));
            cello.setCellStyle(cellStyle);
        }
        //数据
        int index=1;
        while (rs.next()){
            Row row2 = sheet.createRow(index);
            for (int i = 0; i < ColumnName.size(); i++) {
                if(ColumnName.get(i).equals("id") || ColumnName.get(i).equals("mainid")){
                    continue;
                }
                Cell cello = row2.createCell(i-2);
                cello.setCellValue(rs.getString(ColumnName.get(i)));
                cello.setCellStyle(cellStyle);
            }
            index++;
        }

        //读取文件流
        //将workbook转换为流的形式
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        workbook.write(os);
        InputStream input = new ByteArrayInputStream(os.toByteArray());

        String excelName = "测试一个excel.xlsx";
        //文件名称 中文乱码,就打开下面的的注释
        excelName = new String(excelName.replaceAll("<", "").replaceAll(">", "").replaceAll("&lt;", "").replaceAll("&gt;", "").getBytes("UTF-8"), "ISO-8859-1");

        return Response.ok(input)
                .header("Content-disposition", "attachment;filename=" + excelName)
                .header("Cache-Control", "no-cache")
                .build();
    }
    public String getColumname(String tablename,String name){
        String billidsql="select id from workflow_bill where tablename='"+tablename+"'";
        RecordSet rs=new RecordSet();
        rs.executeQuery(billidsql);
        String billid="";
        if(rs.next()){
            billid=rs.getString("id");
        }
        String namesql="select indexdesc from workflow_billfield wb,htmllabelindex h where wb.fieldlabel=h.id and billid='"+billid+"' " +
                "and wb.detailtable is not null and wb.detailtable !='' and fieldname='"+name+"'";
        rs.executeQuery(namesql);
        if (rs.next()){
            return rs.getString("indexdesc");
        }else {
            return "";
        }
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值