下载模板和信息写入文件并下载

下载模板

ccc68fe7bb20c02e6a0716cbaea8860a3eb.jpg

17d60b9761bab34891acbbb6466225466b9.jpg

信息写入文件并下载

7ca68e9320a663fc63582af515aa9e10b85.jpg

a02a10d5d76e6a96f4f3c3aae44aae97f28.jpg

e02ec101b3052d9bd617a54ed294f8f7be8.jpg

 

59eec6198483e27ed0769a9a56e7ced779c.jpg

package com.njwb.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javassist.expr.NewArray;

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

import org.apache.logging.log4j.core.config.plugins.ResolverUtil.NameEndsWith;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.njwb.entity.eopapp.EopApp;
import com.njwb.service.DeptService;

@Controller
public class DownController {
    @Autowired
    private DeptService deptService;
    /*
     * 下载模板
     */
    @ResponseBody
    @RequestMapping("/downModel")
    public void downModel(HttpServletRequest req,HttpServletResponse resp){
        try {
             //获取要下载的模板名称(中文乱码需要再次解码)
            String fileNames =URLEncoder.encode("测试模板.xls", "utf-8");
            String fileName=java.net.URLDecoder.decode( fileNames, "ISO-8859-1");
             //设置要下载的文件的名称
            resp.setHeader("Content-disposition", "attachment;fileName=" + fileName);
             //通知客服文件的MIME类型
            resp.setContentType("application/vnd.ms-excel;charset=UTF-8");
            String f = DownController.class.getResource("importOrderTemplate.xls").getPath(); 
           // String f = DownController.class.getClassLoader().getResource(“4.txt”).getPath();
            File file=new File(f);
            FileInputStream fileinput=new FileInputStream(file);
           /* BufferedReader input = new BufferedReader(new InputStreamReader(fileinput));
            OutputStream out = resp.getOutputStream();
           // byte[] b = new byte[2048];
           // int len;
            String line;
            while ( (line=input.readLine()) != null) {
                out.write(line.getBytes("utf-8"));
                
            }*/
            FileInputStream input = new FileInputStream(file);
            OutputStream out = resp.getOutputStream();
            byte[] b = new byte[2048];
            int len;
            while ((len = input.read(b)) != -1) {
                out.write(b, 0, len);
            }
            //修正 Excel在xxx.xlsx中发现不可读取的内容。是否恢复此工作薄的内容?如果信任此工作簿的来源,请点击"是"
            resp.setHeader("Content-Length", String.valueOf(fileinput.getChannel().size()));
            input.close();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /*
     * 输入信息到文件
     */
    @ResponseBody
    @RequestMapping("writeFile")
    public String writeFile(HttpServletRequest req,HttpServletResponse resp) throws UnsupportedEncodingException{
    /*     //获取要下载的模板名称
        String fileName =URLEncoder.encode(("测试.xls"), "utf-8");
         //设置要下载的文件的名称
        resp.setHeader("Content-disposition", "attachment;fileName=" + fileName);
         //通知客服文件的MIME类型
        resp.setContentType("application/vnd.ms-excel;charset=UTF-8");*/
        //1、创建工作簿
        Workbook wb = new XSSFWorkbook();
        //1.1、设置表格的格式----居中
        CellStyle cs =wb.createCellStyle();
        cs.setAlignment(CellStyle.ALIGN_CENTER);
        //2.1、创建工作表
        Sheet sheet = wb.createSheet("订单信息列表1");
        sheet.setColumnWidth(0, 100 * 40);
        sheet.setColumnWidth(1, 100 * 40);
        sheet.setColumnWidth(2, 100 * 40);
        sheet.setColumnWidth(3, 100 * 40);
        sheet.setColumnWidth(4, 100 * 40);
        //3.1、创建行----表头行
        Row row = sheet.createRow(0);

        //4、创建格
        Cell cell = row.createCell(0);
                cell.setCellValue("供应商名称");
                cell.setCellStyle(cs);
            cell = row.createCell(1);
                cell.setCellValue("订单编号");
                cell.setCellStyle(cs);
            cell = row.createCell(2);
                cell.setCellValue("下单时间");
                cell.setCellStyle(cs);
            cell = row.createCell(3);
                cell.setCellValue("订单状态");
                cell.setCellStyle(cs);
            cell = row.createCell(4);
                cell.setCellValue("订单来源");
                cell.setCellStyle(cs);
                
        List<EopApp> dopApps=deptService.queryAll();
        for(int i=0;i<dopApps.size();i++){
            //3.2、创建行----内容行
            row=sheet.createRow(i+1);
            //第几行第几格  第一行第一格为“code”
            row.createCell(0).setCellValue(dopApps.get(i).getApp_Name());
            row.createCell(1).setCellValue(dopApps.get(i).getAppId());
            row.createCell(2).setCellValue(dopApps.get(i).getPath());
            row.createCell(3).setCellValue(dopApps.get(i).getAuthor());
            row.createCell(4).setCellValue(dopApps.get(i).getDescript());        
        }
        
        //指定存储位置
        try {
            String f = DownController.class.getResource("Orders.xlsx").getPath(); 
            System.out.println(f);
            File file=new File(f);
            FileOutputStream fout = new FileOutputStream(file);
            resp.setHeader("Content-Length", String.valueOf(fout.getChannel().size()));
            
            wb.write(fout);
            fout.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        return "success";
         
    }
    
    /**
     * 
     */
    @ResponseBody
    @RequestMapping("/downFile")
    public void downFile(HttpServletRequest req,HttpServletResponse resp){
        try {
             //获取要下载的模板名称
            String fileName =URLEncoder.encode(("orders.xls"), "utf-8");
             //设置要下载的文件的名称
            resp.setHeader("Content-disposition", "attachment;fileName=" + fileName);
             //通知客服文件的MIME类型
            resp.setContentType("application/vnd.ms-excel;charset=UTF-8");
            String f = DownController.class.getResource("Orders.xlsx").getPath(); 
           // String f = DownController.class.getClassLoader().getResource(“4.txt”).getPath();
            File file=new File(f);
            FileInputStream fileinput=new FileInputStream(file);
           /* BufferedReader input = new BufferedReader(new InputStreamReader(fileinput));
            OutputStream out = resp.getOutputStream();
           // byte[] b = new byte[2048];
           // int len;
            String line;
            while ( (line=input.readLine()) != null) {
                out.write(line.getBytes("utf-8"));
            }*/
            FileInputStream input = new FileInputStream(file);
            OutputStream out = resp.getOutputStream();
            byte[] b = new byte[2048];
            int len;
            while ((len = input.read(b)) != -1) {
                out.write(b, 0, len);
            }
            //修正 Excel在xxx.xlsx中发现不可读取的内容。是否恢复此工作薄的内容?如果信任此工作簿的来源,请点击"是"
            resp.setHeader("Content-Length", String.valueOf(fileinput.getChannel().size()));
            input.close();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}
 

 

转载于:https://my.oschina.net/u/3114501/blog/2993458

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值