0
点赞
收藏
分享

微信扫一扫

特殊excel表格导出模板

ZSACH 2022-01-20 阅读 85

1、代码

package com.li.zzh.fileUtils;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Component;

/**
 * excel导出
 * @author Administrator
 *
 */
@Component
public class ExcelUtils {

	/**
	 * 导出excel
	 * @param title
	 * @param request
	 * @param response
	 */
	public void downExcel(String title, HttpServletRequest request,HttpServletResponse response){
		//两百条刷新磁盘一次
        Workbook workbook = new SXSSFWorkbook(200);
        //生成一个表格
        Sheet sheet = workbook.createSheet("托书");
        //设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth((short) 18);
        //样式
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        Font font = workbook.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        //背景色
        short colors = IndexedColors.GREY_25_PERCENT.getIndex();
        cellStyle.setFillForegroundColor(colors);
        cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        //标题边界线加粗
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        
        //创建excel表格
        for(int i = 0; i < 3; i++){
        	//创建行
        	Row row = sheet.createRow((int)i);
        	//创建行对应的列
            Cell cell = row.createCell(0);
            if(i == 0){
            	cell.setCellValue("FBA仓库代码");
            }else if(i == 1){
            	cell.setCellValue("收件人邮编");
            }else if(i == 2){
            	cell.setCellValue("物流方式");
            }
            //创建五个单元格(此五个单元格需合并成一个)
            for(int j = 1; j < 6; j++){
                cell = row.createCell(j);
                if(i==0 && j == 1){
                	cell.setCellValue("01");
                }else if(i==1 && j == 1){
                	cell.setCellValue("11");
                }else if(i==2 && j == 1){
                	cell.setCellValue("21");
                }
            }
            cell = row.createCell(6);
            if(i == 0){
            	cell.setCellValue("收件人地址");
            }else if(i == 1){
            	cell.setCellValue("收件人城市");
            }else if(i == 2){
            	cell.setCellValue("是否单独报关");
            }
            //创建五个单元格(此五个单元格需合并成一个)
            for(int j = 7; j < 12; j++){
            	cell = row.createCell(j);
                if(i==0 && j == 7){
                	cell.setCellValue("07");
                }else if(i==1 && j == 7){
                	cell.setCellValue("17");
                }else if(i==2 && j == 7){
                	cell.setCellValue("27");
                }
            }
            cell = row.createCell(12);
            if(i == 0){
            	cell.setCellValue("收件人电话");
            }else if(i == 1){
            	cell.setCellValue("收件人州");
            }else if(i == 2){
            	cell.setCellValue("托书生成时间");
            }
            //创建五个单元格(此五个单元格需合并成一个)
            for(int j = 13; j < 18; j++){
            	cell = row.createCell(j);
                if(i==0 && j == 13){
                	cell.setCellValue("013");
                }else if(i==1 && j == 13){
                	cell.setCellValue("113");
                }else if(i==2 && j == 13){
                	cell.setCellValue("213");
                }
            }
            // 合并i行的列(下标为1到5的列、7到11的列、13到17的列合并合并)
            CellRangeAddress region1 = new CellRangeAddress(i, i, 1, 5);
            sheet.addMergedRegion(region1);
            CellRangeAddress region2 = new CellRangeAddress(i, i, 7, 11);
            sheet.addMergedRegion(region2);
            CellRangeAddress region3 = new CellRangeAddress(i, i, 13, 17);
            sheet.addMergedRegion(region3);
        }
        //创建第4行表头
        String[] heads = {"SDP单号","FBA编号","货件跟踪编号","货号","中文品名",
						  "英文品名","海关编号HSCODE","数量(PCS)","单价(USD)","申报总金额(USD)",
						  "箱数","总净重(KG)","总毛重(KG)","总体积(CBM)","品牌",
						  "材质","用途","产品图片"};
    	Row row = sheet.createRow((int)3);
    	for(int k = 0; k < heads.length; k++){
    		Cell cell = row.createCell(k);
    		cell.setCellValue(heads[k]);
    		cell.setCellStyle(cellStyle);
    	}
    	//列表数据
    	List<Map<String, String>> list = this.getList(heads);
        int index = 4;
        for(Map<String, String> map : list){
        	Row rowX = sheet.createRow((int)index);
        	for(int k = 0; k < heads.length; k++){
        		//创建rowX行单元格
        		Cell cell = rowX.createCell(k);
        		cell.setCellValue(map.get(String.valueOf(k)));
        	}
        	index ++;
        	Row rowY = sheet.createRow((int)index);
        	for(int k = 0; k < heads.length; k++){
        		//创建rowY行单元格
        		Cell cell = rowY.createCell(k);
        	}
        	//合并rowX与rowY行
        	for(int k = 0; k < heads.length; k++){
        		CellRangeAddress region = new CellRangeAddress(index - 1, index, k, k);
                sheet.addMergedRegion(region);
        	}
            
            index ++;
        }
        //下载excel表头
        this.downLoad(title,workbook,request,response);
	}
	
	/**
	 * 列表数据
	 * @return
	 */
	private List<Map<String, String>> getList(String[] heads){
		List<Map<String, String>> retList = new ArrayList<>();
		for(int i = 0; i< 10; i++){
			Map<String, String> retMap = new HashMap<>();
			for(int j = 0; j< heads.length; j++){
				retMap.put(String.valueOf(j), String.valueOf(j));
			}
			retList.add(retMap);
		}
		return retList;
	}

	
	/**
	 * 下载
	 */
	public void downLoad(String title, Workbook workbook, HttpServletRequest request,HttpServletResponse response){	    
	    BufferedOutputStream out=null;	
        try{
        	// 创建文件输出流,准备输出电子表格
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream"); 
            String agent=request.getHeader("User-Agent").toLowerCase();
        	if(agent.indexOf("firefox")>0){ 
    	    	response.addHeader("Content-Disposition", "attachment;filename="+ new String(title.getBytes("GB2312"),"ISO-8859-1")+".xls");
    	    }else{
    	    	response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode(title,"UTF-8")+".xls"); 
    	    }
        	
        	out = new BufferedOutputStream(response.getOutputStream());
        	workbook.write(out);
            out.flush();
        }catch (IOException e){  
            e.printStackTrace();
        }finally{
        	try {
				out.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
	}
}
package com.li.zzh.business.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.li.zzh.fileUtils.ExcelUtils;

@RestController
@RequestMapping("/test")
public class TestController {
	
	@Autowired
	private ExcelUtils excelUtils;
	
	@RequestMapping(value = "/f02",method = RequestMethod.GET)
	public void downLoad(HttpServletRequest request,HttpServletResponse response){
		excelUtils.downExcel("托书", request, response);
	}
}

2、效果

在这里插入图片描述
在这里插入图片描述

举报

相关推荐

0 条评论