POI导出数据到Excel的多个Sheet

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

一、PSGInfoSearchAction.java中设置response返回参数

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

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

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward psgInfoView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
        	
        	response.reset();// 清空response   
        	response.addHeader("Content-Disposition", "attachment;filename="+fileName);// 设置response的Header 
        	response.setContentType("application/octet-stream");
        	PoiExportExcel.PoiWriteExcel("工作薄",psgList,request,response);
        	return null;
			
        }
		return mapping.findForward("psgInfo");//跳转到统计页面
	}	 	
   
}

二、PoiExportExcel.java组装数据与Excel导出格式

package com.sasis.util;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.HSSFColor;

import com.sasis.model.PSGInfoSearchForReport;
public class PoiExportExcel {
 
 /**
  * 导出数据到多个sheet
  * @param title
  * @param list
  * @param os
  * add by bcao 2015-8-13
  */
 @SuppressWarnings({ "deprecation", "rawtypes", "unused", "unchecked" })
public static void PoiWriteExcel(String title,List list,HttpServletRequest request,HttpServletResponse response){
 	 OutputStream os=null;  
  try{
	 int sheetNum=1;//工作薄sheet编号
	 int bodyRowCount=1;//正文内容行号
	 int currentRowCount=1;//当前的行号
	 int perPageNum = 50000;//每个工作薄显示50000条数据
	 os = new BufferedOutputStream(response.getOutputStream());//输出流
 	 HSSFWorkbook workbook = new HSSFWorkbook();//创建excel
	 HSSFSheet sheet = workbook.createSheet(title+sheetNum);//创建一个工作薄
	 setSheetColumn(sheet);//设置工作薄列宽
	 HSSFRow row = null;//创建一行
	 HSSFCell cell = null;//每个单元格
		  
	 HSSFCellStyle titleCellStyle = createTitleCellStyle(workbook);
	 writeTitleContent(sheet,titleCellStyle);//写入标题
	 
	//第二行开始写入数据
	ArrayPageList pageList = (ArrayPageList) list;
	ArrayList<PSGInfoSearchForReport> psgInfos = new ArrayList<PSGInfoSearchForReport>();
	psgInfos = (ArrayList<PSGInfoSearchForReport>) pageList.getList();	 
	HSSFCellStyle bodyCellStyle = createBodyCellStyle(workbook); 
    HSSFCellStyle dateBobyCellStyle = createDateBodyCellStyle(workbook);
	for(int i=0;i<psgInfos.size();i++){
		//正文内容
		row = sheet.createRow(bodyRowCount);        
		//第二行写开始写入正文内容
		cell = row.createCell((short)0);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(currentRowCount);//序号
		cell = row.createCell((short)1);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(psgInfos.get(i).getPsg_name_cn());//姓名
		cell = row.createCell((short)2);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(psgInfos.get(i).getTel_num());//手机号
		cell = row.createCell((short)3);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(psgInfos.get(i).getFlt_num());//航班号
		cell = row.createCell((short)4);
		cell.setCellStyle(dateBobyCellStyle);
		cell.setCellValue(psgInfos.get(i).getFlt_date());//航班日期
		cell = row.createCell((short)5);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(psgInfos.get(i).getOrig());//始发
		cell = row.createCell((short)6);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(psgInfos.get(i).getDest());//到达
		cell = row.createCell((short)7);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(psgInfos.get(i).getEt_num());//电子票号
		cell = row.createCell((short)8);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(psgInfos.get(i).getCertNo());//证件号
		cell = row.createCell((short)9);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(psgInfos.get(i).getMain_classXls());//主舱
		cell = row.createCell((short)10);
		cell.setCellStyle(bodyCellStyle);
		cell.setCellValue(psgInfos.get(i).getSell_class());//子舱
		cell = row.createCell((short)11);
		cell.setCellStyle(bodyCellStyle);	
		cell.setCellValue(psgInfos.get(i).getSeat_no());//座位
		cell = row.createCell((short)12);
		cell.setCellStyle(bodyCellStyle);	
		cell.setCellValue(psgInfos.get(i).getStatus());//状态
		cell = row.createCell((short)13);
		cell.setCellStyle(bodyCellStyle);	
		cell.setCellValue(psgInfos.get(i).getBags());//行李数
		cell = row.createCell((short)14);
		cell.setCellStyle(bodyCellStyle);	
		cell.setCellValue(psgInfos.get(i).getBagwht());//行李重
		cell = row.createCell((short)15);
		cell.setCellStyle(bodyCellStyle);	
		cell.setCellValue(psgInfos.get(i).getAllSpecialStr());//特殊	
		if(currentRowCount % perPageNum == 0){//每个工作薄显示50000条数据
			sheet=null;
			sheetNum++;//工作薄编号递增1
			sheet = workbook.createSheet(title+sheetNum);//创建一个新的工作薄
			setSheetColumn(sheet);//设置工作薄列宽
			bodyRowCount = 0;//正文内容行号置位为0
			writeTitleContent(sheet,titleCellStyle);//写入标题			
		}
		bodyRowCount++;//正文内容行号递增1
		currentRowCount++;//当前行号递增1
	}
	   CookieUtil.remove(request,response,"exporting");//删除exporting cookie
	   workbook.write(os);
	   os.flush();
	 }catch(Exception e){
		   e.printStackTrace();
	 } finally {
		try {
			 os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		 
	 }
		 
	}
 	/**
 	 * 设置正文单元样式
 	 * @param workbook
 	 * @return
 	 */
 	public static HSSFCellStyle createBodyCellStyle(HSSFWorkbook workbook){ 
 		HSSFCellStyle cellStyle = workbook.createCellStyle();
 		HSSFFont font = workbook.createFont();
 		font.setFontHeightInPoints((short) 8);  
 		font.setFontName(HSSFFont.FONT_ARIAL);//设置标题字体
 		cellStyle.setFont(font);
 		cellStyle = workbook.createCellStyle();
 		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 		
 		return cellStyle;
 	}
 	/**
 	 * 设置正文单元时间样式
 	 * @param workbook
 	 * @return
 	 */
 	public static HSSFCellStyle createDateBodyCellStyle(HSSFWorkbook workbook){ 
 		HSSFCellStyle cellStyle = workbook.createCellStyle();
 		HSSFFont font = workbook.createFont();
 		font.setFontHeightInPoints((short) 8);  
 		font.setFontName(HSSFFont.FONT_ARIAL);//设置标题字体
 		cellStyle.setFont(font);
 		cellStyle = workbook.createCellStyle();
 		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 	
 	    HSSFDataFormat format= workbook.createDataFormat();
 	    cellStyle.setDataFormat(format.getFormat("yyyy-mm-dd"));  		
 		return cellStyle;
 	} 	

 	/**
 	 * 设置标题单元样式
 	 * @param workbook
 	 * @return
 	 */
 	public static HSSFCellStyle createTitleCellStyle(HSSFWorkbook workbook){ 
 		HSSFCellStyle cellStyle = workbook.createCellStyle();
 		HSSFFont font = workbook.createFont();
 		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 		font.setFontHeightInPoints((short) 8);  
 		font.setFontName(HSSFFont.FONT_ARIAL);//设置标题字体
 		cellStyle.setFont(font);
 		cellStyle = workbook.createCellStyle();
 		cellStyle.setFont(font);//设置列标题样式
 		cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);// 设置背景色
 		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
 		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 		
 		return cellStyle;
 	} 	
 	/**
 	 * 写入标题行
 	 * @param workbook
 	 * @return
 	 */
 	public static void writeTitleContent (HSSFSheet sheet,HSSFCellStyle cellStyle){ 
 		HSSFRow row = null;
 		HSSFCell cell = null;		
 		//标题
 		row = sheet.createRow(0);        
 		//第一行写入标题行
 		cell = row.createCell((short)0);//序号
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("序号");
 		cell = row.createCell((short)1);//姓名
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("姓名");
 		cell = row.createCell((short)2);//手机号
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("手机号");
 		cell = row.createCell((short)3);//航班号
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("航班号");
 		cell = row.createCell((short)4);//航班日期
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("航班日期");
 		cell = row.createCell((short)5);//始发
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("始发");
 		cell = row.createCell((short)6);//到达
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("到达");
 		cell = row.createCell((short)7);//电子票号
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("电子票号");
 		cell = row.createCell((short)8);//证件号
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("证件号");
 		cell = row.createCell((short)9);//主舱
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("主舱");
 		cell = row.createCell((short)10);//子舱
 		cell.setCellStyle(cellStyle);
 		cell.setCellValue("子舱");
 		cell = row.createCell((short)11);//座位
 		cell.setCellStyle(cellStyle);	
 		cell.setCellValue("座位");
 		cell = row.createCell((short)12);//状态
 		cell.setCellStyle(cellStyle);	
 		cell.setCellValue("状态");
 		cell = row.createCell((short)13);//行李数
 		cell.setCellStyle(cellStyle);	
 		cell.setCellValue("行李数");
 		cell = row.createCell((short)14);//行李重
 		cell.setCellStyle(cellStyle);	
 		cell.setCellValue("行李重");
 		cell = row.createCell((short)15);//特殊
 		cell.setCellStyle(cellStyle);	
 		cell.setCellValue("特殊");
 	}
 	public static void setSheetColumn(HSSFSheet sheet){
		 sheet.setColumnWidth((short) 2, (short) 3200);//设置手机号列宽
 		 sheet.setColumnWidth((short) 4, (short) 3200);//设置航班日期列宽
 		 sheet.setColumnWidth((short) 7, (short) 5250);//设置电子票号列宽
 		 sheet.setColumnWidth((short) 8, (short) 6250);//设置证件号列宽
 	}
 		
}
三、导出的Excel文件




  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: POI是Apache下的一个开源项目,可以用来操作Excel文件。通过POI,可以很方便地在Excel中创建多个sheet页。可以使用Workbook.createSheet()方法创建新的sheet页,并可以使用Sheet.setSheetName()方法设置sheet页的名称。 ### 回答2: 随着poi的不断完善和发展,现在已经能够方便地实现导出excel文件的多个sheet页功能。 首先,我们需要导入poi的jar包,然后创建一个工作簿对象,即Workbook对象,这个对象负责创建我们要输出的Excel文件和其中的sheet页。 接着,我们可以创建多个sheet页,每个sheet页都是一个Sheet对象,我们可以使用Workbook的createSheet方法来创建一个sheet页,可以通过一个字符串来命名这个sheet页。 对于每个sheet页,我们可以使用同样的流程来设定表头、添加数据等操作。在所有数据都被填入sheet页之后,我们需要关闭输出流并保存文件,即调用workbook对象的write方法将数据写入文件中。 当然,要注意一些细节问题。比如,我们可以使用Workbook的setSheetName方法来给每个sheet页重命名,同时需要注意一些特殊字符的转义问题,如果表头和数据中包含了这些特殊字符,需要进行转义才能正常显示。 以上就是poi导出excel多个sheet页的基本流程,需要注意的事项比较多,但使用起来还是比较方便的,特别适合一些需要导出大量数据的业务场景。 ### 回答3: poi是一种非常实用的Java API,可以帮助我们快速开发Excel导出功能。对于需要导出多个sheet页的需求,我们可以利用poi的工具类来实现。 首先,我们需要创建一个Workbook对象,表示整个Excel文件。然后,创建多个Sheet对象,表示每个sheet页。可以使用Workbook.createSheet()方法创建多个Sheet对象,并指定sheet页的名称。 接下来,我们需要创建行和单元格,向每个sheet页中写入数据。可以使用Sheet.createRow()方法创建行对象,然后使用Row.createCell()方法创建单元格对象,并设定单元格的值。 在写入数据之前,我们需要设置每个sheet页的列宽和样式。可以使用Sheet.setDefaultColumnWidth()方法设置列宽,使用CellStyle对象设置单元格的样式。 最后,我们需要将Workbook对象写入到文件中。可以使用FileOutputStream对象和Workbook.write()方法将Workbook对象写入到文件中。 总的来说,实现poi导出Excel多个sheet页需要以下步骤: 1.创建Workbook对象表示整个Excel文件; 2.创建多个Sheet对象表示每个sheet页; 3.设置每个sheet页的列宽和样式; 4.创建行和单元格,向每个sheet页中写入数据; 5.将Workbook对象写入到文件中。 使用poi导出Excel多个sheet页可以帮助我们提高开发效率,并满足不同的业务需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值