欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poi导出cvs文件

程序员文章站 2022-04-03 19:16:06
...

    cvs文件其实就是一文本文件的形式存储信息,该文件可转化为excel 文件及其他文件类型。

   

    可以实现cvs文件下载的java代码

package com.jifeng.report.service.impl;

import java.io.File;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
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;
import org.apache.poi.hssf.util.Region;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.jifeng.common.common.PortalConfig;
import com.jifeng.common.util.DateFormatHelper;
import com.jifeng.common.util.IdWorkerUtil;
import com.jifeng.common.util.StringUtil;
import com.jifeng.module.campaigns.entity.Campaigns;
import com.jifeng.report.service.CampaignsCsvService;


/**
 * 营销活动cvs下载service实现
 */
@SuppressWarnings("deprecation")
@Service
public class CampaignsCsvServiceImpl implements CampaignsCsvService {
    private Logger log = LoggerFactory.getLogger(CampaignsCsvServiceImpl.class);
    
    /**
	 * 营销活动列表下载csv
	 * @param campaignsList  活动详情
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	public boolean campaignsListCsvDown(List<Campaigns> campaignsList, HttpServletResponse response){
		try {
    		//CSV文件生成
            HSSFWorkbook workbook = new HSSFWorkbook();
            
            HSSFSheet sheet = workbook.createSheet("campaignsListCsvDown");
            
            HSSFCellStyle style = workbook.createCellStyle(); 			// 样式对象   
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);	// 垂直   
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);				// 水平  
            
            HSSFRow row = sheet.createRow((short) 0);   
            HSSFRow row1 = sheet.createRow((short) 1);   
            
            // 写入头部信息
            addMergeCell(sheet,row,style,0,0,1,0,"活动名称");
            addMergeCell(sheet,row,style,0,1,1,1,"开始日期");
            addMergeCell(sheet,row,style,0,2,1,2,"持续时间");
            addMergeCell(sheet,row,style,0,3,1,3,"店铺");
            addMergeCell(sheet,row,style,0,4,1,4,"开销");
            addMergeCell(sheet,row,style,0,5,0,6,"客流量变化");
            addMergeCell(sheet,row1,style,1,5,1,5,"店铺");
            addMergeCell(sheet,row1,style,1,6,1,6,"对比位置");
            addMergeCell(sheet,row,style,0,7,1,7,"平均成本每增加访客");
            
            //从第3行开始循环写数据
            int rowNum = 2;
            for (Campaigns campaigns : campaignsList) {
            	 HSSFRow row2 = sheet.createRow(rowNum);
            	 row2.createCell(0).setCellValue(campaigns.getCampaignsName());
            	 row2.createCell(1).setCellValue(DateFormatHelper.formatToMdy(campaigns.getStartTime()));
            	 row2.createCell(2).setCellValue(campaigns.getDayNum() + "天");
            	 row2.createCell(3).setCellValue(campaigns.getStoreNum() + "家");
            	 
            	 row2.createCell(4).setCellValue("$" + campaigns.getSpend());
            	 row2.createCell(5).setCellValue(campaigns.getRateChange() + "%");
            	 row2.createCell(6).setCellValue("不涉及");
            	 
            	 if(campaigns.getCostAvg() > 0){
            		 row2.createCell(7).setCellValue(campaigns.getCostAvg());
            	 }else{
            		 row2.createCell(7).setCellValue("不涉及");
            	 }
            	 
            	 rowNum++;
    		}
            
            
            File file = new File(PortalConfig.getPdfAddress()+ IdWorkerUtil.nextId() +".csv");
            String fileName = file.getPath();
            
            if(null != response) {
                // 设置下载时客户端Excel的名称
                fileName = MessageFormat.format(fileName, DateFormatHelper.getDate());
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=" + StringUtil.toUTF8String("campaignsListCsvDown.csv"));
                OutputStream ouputStream = response.getOutputStream();
                workbook.write(ouputStream);
                ouputStream.flush();
                ouputStream.close();
            }
            
            return true;
		} catch (Exception e) {
			log.error("",e);
			return false;
		}
	}

	
    /**
     * 营销活动详情根据日期下载csv
     * @param campaignsList  活动详情
     * @param response
     * @return
     * @throws Exception 
     */
    public boolean campaignsDetailsDayCsvDown(List<Campaigns> campaignsList, HttpServletResponse response) {
    	try {
    		//CSV文件生成
            HSSFWorkbook workbook = new HSSFWorkbook();
            
            HSSFSheet sheet = workbook.createSheet("campaignsDetailsDayAnalysis");
            
            //第一行
            HSSFRow header = sheet.createRow(0);
            header.createCell(0).setCellValue("日期");
            header.createCell(1).setCellValue("总客流量");
            header.createCell(2).setCellValue("平均客流量每店");
            header.createCell(3).setCellValue("比较时期同比");
            
            //从第2行开始循环写数据
            int rowNum = 1;
            for (Campaigns campaigns : campaignsList) {
            	 HSSFRow row = sheet.createRow(rowNum);
            	 row.createCell(0).setCellValue(campaigns.getCurrentDate());
            	 row.createCell(1).setCellValue(campaigns.getEnters());
            	 row.createCell(2).setCellValue(campaigns.getEntersAvg());
            	 row.createCell(3).setCellValue(campaigns.getRateTb() + "%");
            	 
            	 rowNum++;
    		}
            
            File file = new File(PortalConfig.getPdfAddress()+ IdWorkerUtil.nextId() +".csv");
            String fileName = file.getPath();
            
            if(null != response) {
                // 设置下载时客户端Excel的名称
                fileName = MessageFormat.format(fileName, DateFormatHelper.getDate());
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=" + StringUtil.toUTF8String("campaignsDetailsDayAnalysis.csv"));
                OutputStream ouputStream = response.getOutputStream();
                workbook.write(ouputStream);
                ouputStream.flush();
                ouputStream.close();
            }
            
            return true;
		} catch (Exception e) {
			log.error("",e);
			return false;
		}
    }

    
    /**
     * 营销活动详情根据场地下载csv
     * @param campaignsList  活动详情
     * @param response
     * @return
     * @throws Exception 
     */
	public boolean campaignsDetailsStoreCsvDown(List<Campaigns> campaignsList, HttpServletResponse response) {
		try {
    		//CSV文件生成
            HSSFWorkbook workbook = new HSSFWorkbook();
            
            HSSFSheet sheet = workbook.createSheet("campaignsDetailsStoreCsvDown");
            
            //第一行
            HSSFRow header = sheet.createRow(0);
            header.createCell(0).setCellValue("店铺");
            header.createCell(1).setCellValue("店铺客流量变化");
            header.createCell(2).setCellValue("销售额");
            header.createCell(3).setCellValue("成交率");
            header.createCell(4).setCellValue("客流量");
            
            //从第2行开始循环写数据
            int rowNum = 1;
            for (Campaigns campaigns : campaignsList) {
            	 HSSFRow row = sheet.createRow(rowNum);
            	 row.createCell(0).setCellValue(campaigns.getStoreName());
            	 row.createCell(1).setCellValue(campaigns.getRateChange() + "%");
            	 row.createCell(2).setCellValue("$" + campaigns.getSales());
            	 row.createCell(3).setCellValue(campaigns.getConversion() +"%");
            	 row.createCell(4).setCellValue(campaigns.getEnters());
            	 
            	 rowNum++;
    		}
            
            File file = new File(PortalConfig.getPdfAddress()+ IdWorkerUtil.nextId() +".csv");
            String fileName = file.getPath();
            
            if(null != response) {
                // 设置下载时客户端Excel的名称
                fileName = MessageFormat.format(fileName, DateFormatHelper.getDate());
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=" + StringUtil.toUTF8String("campaignsDetailsStoreCsvDown.csv"));
                OutputStream ouputStream = response.getOutputStream();
                workbook.write(ouputStream);
                ouputStream.flush();
                ouputStream.close();
            }
            
            return true;
		} catch (Exception e) {
			log.error("",e);
			return false;
		}
	}
	
	
	/**
	 * 合并单元格写法
	 * @param sheet
	 * @param row 
	 * @param style 		样式
	 * @param beginRow		开始行
	 * @param beginColumn	开始列
	 * @param endRow		结束行
	 * @param endColumn		结束列
	 * @param value			值
	 */
	public void addMergeCell(HSSFSheet sheet,HSSFRow row,HSSFCellStyle style,int beginRow,int beginColumn,int endRow,int endColumn,String value){
		// 单元格合并   
        // 四个参数分别是:起始行,起始列,结束行,结束列
        sheet.addMergedRegion(new Region(beginRow, (short) beginColumn, endRow, (short) endColumn));  
        //希尔数据的列
        HSSFCell cell = row.createCell((short) beginColumn);  
        // 跨单元格显示的数据 
        cell.setCellValue(value);   
        cell.setCellStyle(style);   
	}
}

 

   

相关标签: poi cvs