Excel導出

package com.foxconn.system.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import com.foxconn.system.util.InitInformation.InitMessage;

public class UploadAndDownloadFile {
    
    /**
     * @創建目的:文件上傳的公共方法
     * @param file SpringMvc上傳的文件類型
     * @param req 請求
     * @return 
     * @創建人 H2408236
     * @創建時間 2017年6月20日 下午2:25:43
     * @修改人 
     * @修改時間
     */
    public static String uploadFile(MultipartFile file){
        
        //獲取項目絕對物理路徑
        String path = InitMessage.getFilePath(InitMessage.FILE_TYPE_FILE);
        //String path = "E:\\lc\\166";
        //獲取文件名稱
        String fileName=file.getOriginalFilename();
        //拼勁文件路徑
        String filePath = path+"\\"+fileName;
        //生成相應文件
        File targetFile = new File(path, fileName);
        //保存文件
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filePath;
    }
    
    /**
     * @創建目的:文件下載公共方法
     * @param response 響應
     * @param filePath 服務器文件保存絕對路徑
     * @throws IOException
     * @創建人 H2408236
     * @創建時間 2017年6月20日 下午2:21:58
     * @修改人 
     * @修改時間
     */
    public static void downloadFile(HttpServletResponse response, String filePath)
            throws IOException {
        String fileName = filePath.substring(filePath.lastIndexOf("\\")+1, filePath.length());
        //1.設置響應頭
        response.setHeader("Content-Disposition", "attachment;filename=" +fileName);
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        
        InputStream in = null;
        OutputStream output = null;
        
        try {  
            //2.讀取服務器文件
            in = new FileInputStream(filePath);  
            //3.通过response获取ServletOutputStream对象
            output = response.getOutputStream();
            output.flush(); 
            int b = 0;  
            byte[] buffer = new byte[512];  
            
            while ((b = in.read(buffer)) != -1){  
                //4.写到输出流(output)中  
                output.write(buffer,0,b);  
            }
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {
            //关掉输入、输出流
            in.close();  
            output.close(); 
        }
        
    }
    /**
     * @創建目的:導出Excel數據
     * @param response 響應
     * @param wb Excel檔
     * @param fileName 導出文件名
     * @throws Exception
     * @創建人 Hunter ht
     * @創建時間 2018-9-17
     * @修改人 
     * @修改時間
     */
    public static void export(HttpServletResponse response,Workbook wb,String fileName)throws Exception{
        response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso8859-1"));
        response.setContentType("application/ynd.ms-excel;charset=UTF-8");
        OutputStream out=response.getOutputStream();
        wb.write(out);
        out.flush();
        out.close();
    }
}


    package com.foxconn.system.util.file;
    
    import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import com.foxconn.system.util.InitInformation.InitMessage;
    
    public class UploadAndDownloadFile {
        
        /**
         * @創建目的:文件上傳的公共方法
         * @param file SpringMvc上傳的文件類型
         * @param req 請求
         * @return 
         * @創建人 H2408236
         * @創建時間 2017年6月20日 下午2:25:43
         * @修改人 
         * @修改時間
         */
        public static String uploadFile(MultipartFile file){
            
            //獲取項目絕對物理路徑
            String path = InitMessage.getFilePath(InitMessage.FILE_TYPE_FILE);
            //String path = "E:\\lc\\166";
            //獲取文件名稱
            String fileName=file.getOriginalFilename();
            //拼勁文件路徑
            String filePath = path+"\\"+fileName;
            //生成相應文件
            File targetFile = new File(path, fileName);
            //保存文件
            try {
                file.transferTo(targetFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return filePath;
        }
        
        /**
         * @創建目的:文件下載公共方法
         * @param response 響應
         * @param filePath 服務器文件保存絕對路徑
         * @throws IOException
         * @創建人 H2408236
         * @創建時間 2017年6月20日 下午2:21:58
         * @修改人 
         * @修改時間
         */
        public static void downloadFile(HttpServletResponse response, String filePath)
                throws IOException {
            String fileName = filePath.substring(filePath.lastIndexOf("\\")+1, filePath.length());
            //1.設置響應頭
            response.setHeader("Content-Disposition", "attachment;filename=" +fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            
            InputStream in = null;
            OutputStream output = null;
            
            try {  
                //2.讀取服務器文件
                in = new FileInputStream(filePath);  
                //3.通过response获取ServletOutputStream对象
                output = response.getOutputStream();
                output.flush(); 
                int b = 0;  
                byte[] buffer = new byte[512];  
                
                while ((b = in.read(buffer)) != -1){  
                    //4.写到输出流(output)中  
                    output.write(buffer,0,b);  
                }
            } catch (IOException e) {  
                e.printStackTrace();  
            } finally {
                //关掉输入、输出流
                in.close();  
                output.close(); 
            }
            
        }
        /**
         * @創建目的:導出Excel數據
         * @param response 響應
         * @param wb Excel檔
         * @param fileName 導出文件名
         * @throws Exception
         * @創建人 Hunter ht
         * @創建時間 2018-9-17
         * @修改人 
         * @修改時間
         */
        public static void export(HttpServletResponse response,Workbook wb,String fileName)throws Exception{
            response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso8859-1"));
            response.setContentType("application/ynd.ms-excel;charset=UTF-8");
            OutputStream out=response.getOutputStream();
            wb.write(out);
            out.flush();
            out.close();
        }
    }
    原文作者:蚁丶点
    原文地址: https://www.jianshu.com/p/798b77d4e247
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞