`
TableMiao
  • 浏览: 75045 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Excel多Sheet导出

阅读更多

Excel 多个Sheet 导出

       上次贴了几个关于上传下载的 发现以接口的方式贴过来,读起来很混乱。直接索性贴上工具类,附上测试,自己根据需要再去封装比较好。我直接从项目里抠过来了,同事兼哥们写的……

       1.1多sheetExcel工具类

package com.tm.util;

import java.util.List;
import java.util.Map;

public class ManySheetExcelUtils {

	private String sheetName;// sheet名称
	
	private List<PropSetter> props;// 属性设置
	
	private List<Map<String, Object>> datas;// 数据信息 

	/*******  getter、setter 省略  ******/
	public ManySheetExcelUtils() {
		super();
	}

	public ManySheetExcelUtils(String sheetName, List<PropSetter> props,
			List<Map<String, Object>> datas) {
		super();
		this.sheetName = sheetName;
		this.props = props;
		this.datas = datas;
	}
}

         1.2组成Excel数据工具类   

      

package com.tm.util;

/***
 * 组成list数据的实体
 * 
 * @author tablemiao
 *
 */
public class PropSetter {

	private String rOne; //第一行标题
	
	private String rTwo; //第二行标题
	
	private String prop;//对应的导出数据的字段名
	
	private String type;//数据类型
	
	private int width;//表格宽度
	
	private boolean color;//颜色

	/*****  Getter 、Setter 省略********/
	public PropSetter(String rOne, String prop, int width) {
		super();
		this.rOne = rOne;
		this.prop = prop;
		this.width = width;
	}
	
	public PropSetter(String rOne, String rTwo, String prop, int width) {
		super();
		this.rOne = rOne;
		this.rTwo = rTwo;
		this.prop = prop;
		this.width = width;
	}

	public PropSetter(String rOne, String rTwo, String prop, int width,
			boolean color) {
		super();
		this.rOne = rOne;
		this.rTwo = rTwo;
		this.prop = prop;
		this.width = width;
		this.color = color;
	}

	public PropSetter(String rOne, String rTwo, String prop, String type,
			int width) {
		super();
		this.rOne = rOne;
		this.rTwo = rTwo;
		this.prop = prop;
		this.type = type;
		this.width = width;
	}

	public PropSetter() {
		super();
	}
	
}

 

       1.3 组装加测试

package com.tm.util;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 多sheet导出模板
 * */
public class ManySheetExportUtils {
	
	private static Logger logger = LoggerFactory.getLogger(CreateMapExcel.class);

	private static SXSSFWorkbook workbook = new SXSSFWorkbook();

	private static Sheet sheet;

	private static CellStyle titleStyle;

	private static CellStyle stringStyle;

	private static CellStyle longStyle;

	private static CellStyle doubleStyle;

	static {
		DataFormat format = workbook.createDataFormat();

		Font titleFont = workbook.createFont();
		titleFont.setFontName("微软雅黑");
		titleFont.setFontHeightInPoints((short) 10); // 字体大小
		titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);// 加粗

		Font contentFont = workbook.createFont();
		contentFont.setFontName("微软雅黑");
		contentFont.setFontHeightInPoints((short) 9);

		titleStyle = workbook.createCellStyle();
		titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
		titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
		titleStyle.setBorderBottom(CellStyle.BORDER_THIN);
		titleStyle.setBorderLeft(CellStyle.BORDER_THIN);
		titleStyle.setBorderRight(CellStyle.BORDER_THIN);
		titleStyle.setBorderTop(CellStyle.BORDER_THIN);
		titleStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);// 填暗红色
		titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		titleStyle.setFont(titleFont);
		titleStyle.setWrapText(true);

		stringStyle = workbook.createCellStyle();
		stringStyle.setAlignment(CellStyle.ALIGN_LEFT);
		stringStyle.setBorderBottom(CellStyle.BORDER_THIN);
		stringStyle.setBorderLeft(CellStyle.BORDER_THIN);
		stringStyle.setBorderRight(CellStyle.BORDER_THIN);
		stringStyle.setBorderTop(CellStyle.BORDER_THIN);
		stringStyle.setFont(contentFont);
		stringStyle.setWrapText(true);

		longStyle = workbook.createCellStyle();
		longStyle.setAlignment(CellStyle.ALIGN_LEFT);
		longStyle.setBorderBottom(CellStyle.BORDER_THIN);
		longStyle.setBorderLeft(CellStyle.BORDER_THIN);
		longStyle.setBorderRight(CellStyle.BORDER_THIN);
		longStyle.setBorderTop(CellStyle.BORDER_THIN);
		longStyle.setFont(contentFont);
		longStyle.setDataFormat(format.getFormat("0"));
		longStyle.setWrapText(true);

		doubleStyle = workbook.createCellStyle();
		doubleStyle.setAlignment(CellStyle.ALIGN_LEFT);
		doubleStyle.setBorderBottom(CellStyle.BORDER_THIN);
		doubleStyle.setBorderLeft(CellStyle.BORDER_THIN);
		doubleStyle.setBorderRight(CellStyle.BORDER_THIN);
		doubleStyle.setBorderTop(CellStyle.BORDER_THIN);
		doubleStyle.setFont(contentFont);
		doubleStyle.setDataFormat(format.getFormat("0.00"));
		doubleStyle.setWrapText(true);
	}

	/**
	 * @param
	 * 		utils  全部属性信息
	 * 
	 * */
	public static SXSSFWorkbook createExcel(List<ManySheetExcelUtils> utils) {
		long startTime = System.currentTimeMillis();
		
		Cell cell;
		Row row;
		for (int i = 0; i < utils.size(); i++) {
			sheet = workbook.createSheet();
			workbook.setSheetName(i, utils.get(i).getSheetName());// 根据属性创建sheet页
			logger.info("第" + i + "次sheet创建完成");
			Row rowOne = sheet.createRow(0);
			rowOne.setHeight((short) 350);
			List<PropSetter> props = utils.get(i).getProps();
			for (int j = 0; j < props.size(); j++) {// 标题的设置
				cell = rowOne.createCell(j);
				sheet.setColumnWidth(j, props.get(j).getWidth()); // 宽度
				cell.setCellStyle(titleStyle);
				cell.setCellValue(props.get(j).getrOne()); // 标题
			}
			logger.info("第" + i + "次表头信息创建完成");
			// 内容设置
			List<Map<String, Object>> datas = utils.get(i).getDatas();
			logger.info("准备执行第" + i + "次数据填充操作");
			if (datas.size() != 0) {
				for (int m = 0; m < datas.size(); m++) {
					row = sheet.createRow(m + 1);
					row.setHeight((short) 310);
					for (int n = 0; n < props.size(); n++) {
						cell = row.createCell(n);
						Object value = datas.get(m).get(props.get(n).getProp());
						if (value == null) {
							cell.setCellValue("");
							cell.setCellStyle(stringStyle);
						} else {
							try {
								cell.setCellType(HSSFCell.CELL_TYPE_STRING);
								cell.setCellValue(Long.valueOf(String.valueOf(value)));
								cell.setCellStyle(longStyle);
							} catch (Exception e) {
								try {
									cell.setCellType(HSSFCell.CELL_TYPE_STRING);
									cell.setCellValue((Double.valueOf(String.valueOf(value))));
									cell.setCellStyle(doubleStyle);
								} catch (Exception e1) {
									cell.setCellType(HSSFCell.CELL_TYPE_STRING);
									cell.setCellValue(String.valueOf(value));
									cell.setCellStyle(stringStyle);
								}
							}
						}
					}
				}
			}
		}
		logger.info("导出总计耗时: {}", System.currentTimeMillis() - startTime + "毫秒!");
		return workbook;
	}

	@Test
	public void test() throws Exception {
		List<PropSetter> props = new ArrayList<PropSetter>();
		props.add(new PropSetter("客户ID", "id", 3000));
		props.add(new PropSetter("客户名称", "name", 4000));
		props.add(new PropSetter("客户地市", "city", 4000));
		List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		map = new HashMap<String, Object>();
		map.put("id", "10000001");
		map.put("name", "上海移动");
		map.put("city", "上海市");
		datas.add(map);// 第一条数据
		map = new HashMap<String, Object>();
		map.put("id", "10000002");
		map.put("name", "北京移动");
		map.put("city", "北京市");
		datas.add(map);// 第二条数据
		map = new HashMap<String, Object>();
		map.put("id", "10000003");
		map.put("name", "重庆移动");
		map.put("city", "重庆市");
		datas.add(map);// 第三条数据
		
		List<PropSetter> props2 = new ArrayList<PropSetter>();
		props2.add(new PropSetter("集团编号", "id", 4000));
		props2.add(new PropSetter("服务等级", "level", 3500));
		props2.add(new PropSetter("属地", "dependency", 3500));
		List<Map<String, Object>> datas2 = new ArrayList<Map<String, Object>>();
		Map<String, Object> map2 = null;
		map2 = new HashMap<String, Object>();
		map2.put("id", "20000001");
		map2.put("level", "标准");
		map2.put("dependency", "浦东");
		datas2.add(map2);// 第一条数据
		map2 = new HashMap<String, Object>();
		map2.put("id", "20000002");
		map2.put("level", "金牌");
		map2.put("dependency", "北区");
		datas2.add(map2);// 第二条数据
		map2 = new HashMap<String, Object>();
		map2.put("id", "20000003");
		map2.put("level", "银牌");
		map2.put("dependency", "南区");
		datas2.add(map2);// 第三条数据

		List<ManySheetExcelUtils> excelUtils = new ArrayList<ManySheetExcelUtils>();
		excelUtils.add(new ManySheetExcelUtils("sheet0", props, datas));
		excelUtils.add(new ManySheetExcelUtils("集团信息", props2, datas2));
		excelUtils.add(new ManySheetExcelUtils("sheet2", props2, datas2));

		OutputStream outputStream = new FileOutputStream("Z:/多sheet.xlsx");
		SXSSFWorkbook workbook = ManySheetExportUtils.createExcel(excelUtils);
		workbook.write(outputStream);
		outputStream.flush();
		outputStream.close();
	}

}

    1.4附上结果

 

 

  • 大小: 42.7 KB
0
2
分享到:
评论

相关推荐

    easyPoi多sheet导出excel

    easyPoi多sheet页导出实例代码,附有数据库sql,支持多表头、合并单元格,通过注解形式导出excel,代码简介、轻松实现导出excel功能。框架使用struts2+spring+hibernate。

    JavaScript 实现 Excel数据导出 支持多个Sheet页导出

    本文将深入探讨如何使用纯JavaScript实现JSON格式数据到Excel文件的导出,同时支持多个Sheet页的导出。这个功能对于前端开发者来说,能够极大地提升用户体验,特别是在数据管理、分析和分享场景下。 首先,我们要...

    导出excel多个sheet.zip

    在Delphi编程环境中,实现从应用程序导出数据到Excel多分页Sheet是一项常见的任务。这个"导出excel多个sheet.zip"压缩包文件可能包含了一个使用Delphi编写的示例或库,用于帮助开发者实现这一功能。下面将详细解释这...

    使用poi方式导出excel(分多个sheet导出)

    本教程将详细介绍如何利用Apache POI库来实现使用多个Sheet(工作簿)导出一个Excel文件的功能。 一、Apache POI简介 Apache POI 是一个开源项目,提供API来读取、写入和修改Microsoft Office文件格式,如Word(DOC...

    poi分多个sheet导出excel

    标题“poi分多个sheet导出excel”所指的就是如何使用POI库来动态地生成多个sheet,并根据设定的阈值(如50000条记录)进行切换。 首先,要使用Apache POI,你需要在项目中引入对应的依赖。如果你使用的是Maven,...

    基于POI的Excel多Sheet页导出导入工具类

    "基于POI的Excel多Sheet页导出导入工具类"是一个实用的Java类库,专为处理Excel文件中的多个工作表(Sheet)而设计,提供了一种高效且灵活的方式来操作Excel数据。 该工具类的核心功能包括: 1. **多Sheet页操作**...

    c#导出excel支持多sheet导出,可自定义sheetName

    c#导出excel支持多sheet导出,可自定义sheetName,如有疑问请留言,或qq1574697828.c#导出excel支持多sheet导出,可自定义sheetName,如有疑问请留言。

    ASP.NET 分Sheet导出EXCEL 2003 导出引用dll

    本文将深入探讨如何在ASP.NET环境中实现Excel 2003的分Sheet导出,并涉及到关键的DLL引用。 首先,我们要明白,Excel 2003使用的是.BIFF8文件格式,这是微软早期版本的Excel所采用的。在ASP.NET中,我们可以使用第...

    vue实现多sheet页导出所需文件Export2Excel.js

    vue实现多sheet页导出所需文件Export2Excel.js

    poi多sheet页导出工具类和实例 包含多个excel导出

    在处理大量数据时,由于单个 Excel 工作表(sheet)的限制,我们可能需要将数据分拆到多个 sheet 或多个 Excel 文件中进行导出。本资源提供的 poi 多 sheet 导出工具类和实例,旨在解决这个问题,特别是当数据量超过...

    C# NPOI导出多个Sheet页的Excel

    C# 用NPOI导出多个sheet页的Excel,sheet页名称可以自定义

    导出Excel 一个Excel多个sheet

    - 数据备份:定期将Sheet导出为独立文件,作为数据备份的一种策略。 通过以上方法,我们可以灵活地管理和导出Excel中的多个Sheet,满足各种工作需求。熟练掌握这些技巧,能极大地提高Excel的使用效率。

    分多个sheet导出excel

    本篇文章将详细介绍如何利用Apache POI来分多个sheet导出Excel文件。 首先,我们需要引入Apache POI的依赖。在Maven项目中,可以在pom.xml文件中添加以下依赖: ```xml &lt;groupId&gt;org.apache.poi &lt;artifactId&gt;...

    C# Excel导入导出多sheet页

    本篇文章将深入探讨如何在C#中实现Excel的多sheet页导入与导出。 首先,我们来了解两个关键概念:`Microsoft.Office.Interop.Excel` 和 `Aspose.Cells`。`Microsoft.Office.Interop.Excel` 是.NET框架中的一个库,...

    html导出excel,多sheet,支持不规则table。

    大神提供的,经测试没问题。

    C# winform 导出datatable到excel的多个sheet

    使用C#实现了将多个datatable中的内容导出到 一个excel文件的不同的sheet页,每个sheet页对应一个datatable。代码中的datatable是程序中添加的内容,从数据库中导出数据到excel的话只需要将数据表内容写入datatable...

    jqgrid导出excel多sheet控件

    自定义多个jqgrid导出一个或者多个sheet,可以自动控制。非常强大!

    多Sheet导出excel

    ### 多Sheet导出Excel知识点解析 #### 一、知识点概览 本篇文章将深入解析一个C#程序片段,该程序的功能是实现基于`DataSet`的多Sheet Excel文件导出功能。具体而言,我们将探讨以下几个核心知识点: 1. **如何...

    DEV GridControl GridView导出到Excel 支持多个Sheet 源码

    在描述中提到的功能,即导出多个GridView到Excel的多个Sheet,对于处理复杂的数据结构尤其有用。这个功能允许用户将不同来源或者不同类型的数据分开存储,保持Excel文件的整洁,同时方便用户按需查看和操作。 实现...

    asp.net实现数据导入导出Excel 多Sheet表

    ### ASP.NET 实现数据导入导出到 Excel 中的多 Sheet 表 在 ASP.NET 开发过程中,经常需要处理数据的导入导出功能,特别是在需要将数据分门别类地存储到不同的工作表(Sheet)中时,这便涉及到 Excel 的多 Sheet ...

Global site tag (gtag.js) - Google Analytics