easypoi导出excel怎样设置相同内容不合并

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了easypoi导出excel怎样设置相同内容不合并相关的知识,希望对你有一定的参考价值。

使用easypoi怎样设置下面的图片中相同数据的列不自动合并。

参考技术A 如何合并单元格

Java代码
import java.io.FileOutputStream;
import java.io.IOException;

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;

public class ExcelTest

/**
* @param args
*/
public static void main(String[] args) throws IOException

try
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // 样式对象

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);

sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));
HSSFCell ce = row.createCell((short) 0);
ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 中文处理
ce.setCellValue("项目\\日期"); // 表格的第一行第一列显示的数据
ce.setCellStyle(style); // 样式,居中
int num = 0;
for (int i = 0; i < 9; i++) // 循环9次,每一次都要跨单元格显示
// 计算从那个单元格跨到那一格
int celln = 0;
int celle = 0;
if (i == 0)
celln = 0;
celle = 1;
else
celln = (i * 2);
celle = (i * 2 + 1);

// 单元格合并
// 四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 跨单元格显示的数据
cell.setCellStyle(style); // 样式
// 不跨单元格显示的数据,如:分两行,上一行分别两格为一格,下一行就为两格,“数量”,“金额”
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金额");
cell2.setCellStyle(style);
num++;


// 在后面加上合计百分比

// 合计 在最后加上,还要跨一个单元格
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)));
HSSFCell cell = row.createCell((short) (2 * num + 1));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("合计");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金额");
cell2.setCellStyle(style);

// 百分比 同上
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
HSSFCell cellb = row.createCell((short) (2 * num + 3));
cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb.setCellValue("百分比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb1.setCellValue("数量");
cellb1.setCellStyle(style);
cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb2.setCellValue("金额");
cellb2.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
catch (Exception ex)
ex.printStackTrace();




easypoi导出隔行样式设置

导出excel需要设置隔行背景样式,如下图:

1、ExcelUtil 导出工具类

/**
  * 导出设置隔行背景色
  * @param params
  * @param list
  * @param pojoClass
  * @param fileName
  * @param response
  */
 public static void exportExcel( ExportParams params,List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException 
     Workbook workbook = ExcelExportUtil.exportExcel(params,pojoClass,list);
     if (workbook != null);
     setRowBackground(workbook);
     downLoadExcel(fileName, response, workbook);
 

 /**
  * 偶数行设置背景色
  */
 private static void setRowBackground(Workbook workbook)
     Sheet sheet = workbook.getSheetAt(0);
     CellStyle styles = ExcelStyleUtil.getStyles(workbook,false,(short) 12);
     for(int i = 0; i <= sheet.getLastRowNum(); i ++) 
         Row row = sheet.getRow(i);
         if (i%2==0 && i>0)//标题用全局的标题样式,就不单独设置样式了,所以排除标题
             for(int j = 0; j < row.getPhysicalNumberOfCells(); j ++) 
                 Cell cell = row.getCell(j);
                 cell.setCellStyle(styles);
             
         
     
 

/**
  * 下载
  * @param fileName 文件名称
  * @param response
  * @param workbook excel数据
  */
 public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException 
     try 
         response.setCharacterEncoding("UTF-8");
         response.setHeader("content-Type", "application/vnd.ms-excel");
         response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLS.getValue(), "UTF-8"));
         workbook.write(response.getOutputStream());
      catch (Exception e) 
         throw new IOException(e.getMessage());
     
 

2、ExcelStyleUtil 样式工具类

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.ss.usermodel.*;

public class ExcelStyleUtil implements IExcelExportStyler 
    private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
    private static final short FONT_SIZE_TEN = 12;
    private static final short FONT_SIZE_ELEVEN = 14;
    private static final short FONT_SIZE_TWELVE = 18;
    /**
     * 大标题样式
     */
    private CellStyle headerStyle;
    /**
     * 每列标题样式
     */
    private CellStyle titleStyle;
    /**
     * 数据行样式
     */
    private CellStyle styles;

    public ExcelStyleUtil(Workbook workbook) 
        this.init(workbook);
    

    /**
     * 初始化样式
     *
     * @param workbook
     */
    private void init(Workbook workbook) 
        this.headerStyle = initHeaderStyle(workbook);
        this.titleStyle = initTitleStyle(workbook);
        this.styles = initStyles(workbook);
    

    /**
     * 大标题样式
     *
     * @param color
     * @return
     */
    @Override
    public CellStyle getHeaderStyle(short color) 
        return headerStyle;
    

    /**
     * 每列标题样式
     *
     * @param color
     * @return
     */
    @Override
    public CellStyle getTitleStyle(short color) 
        return titleStyle;
    

    /**
     * 数据行样式
     *
     * @param parity 可以用来表示奇偶行
     * @param entity 数据内容
     * @return 样式
     */
    @Override
    public CellStyle getStyles(boolean parity, ExcelExportEntity entity) 
        return styles;
    

    /**
     * 获取样式方法
     *
     * @param dataRow 数据行
     * @param obj     对象
     * @param data    数据
     */
    @Override
    public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) 
        return getStyles(true, entity);
    

    /**
     * 模板使用的样式设置
     */
    @Override
    public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) 
        return null;
    

    /**
     * 设置隔行背景色
     */
    public static CellStyle getStyles(Workbook workbook,boolean isBold,short size) 
        CellStyle style = getBaseCellStyle(workbook);
        style.setFont(getFont(workbook, size,isBold));
        //背景色
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setDataFormat(STRING_FORMAT);
        return style;
    

    /**
     * 初始化--大标题样式
     *
     * @param workbook
     * @return
     */
    private CellStyle initHeaderStyle(Workbook workbook) 
        CellStyle style = getBaseCellStyle(workbook);
        style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
        return style;
    

    /**
     * 初始化--每列标题样式
     *
     * @param workbook
     * @return
     */
    private CellStyle initTitleStyle(Workbook workbook) 
        CellStyle style = getBaseCellStyle(workbook);
        style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, true));
        //背景色
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return style;
    

    /**
     * 初始化--数据行样式
     *
     * @param workbook
     * @return
     */
    private CellStyle initStyles(Workbook workbook) 
        CellStyle style = getBaseCellStyle(workbook);
        style.setFont(getFont(workbook, FONT_SIZE_TEN, false));
        style.setDataFormat(STRING_FORMAT);
        return style;
    

    /**
     * 基础样式
     *
     * @return
     */
    private static CellStyle getBaseCellStyle(Workbook workbook) 
        CellStyle style = workbook.createCellStyle();
        //下边框
        style.setBorderBottom(BorderStyle.THIN);
        //左边框
        style.setBorderLeft(BorderStyle.THIN);
        //上边框
        style.setBorderTop(BorderStyle.THIN);
        //右边框
        style.setBorderRight(BorderStyle.THIN);
        //水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //上下居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置自动换行
        style.setWrapText(true);
        return style;
    

    /**
     * 字体样式
     *
     * @param size   字体大小
     * @param isBold 是否加粗
     * @return
     */
    private static Font getFont(Workbook workbook, short size, boolean isBold) 
        Font font = workbook.createFont();
        //字体样式
        font.setFontName("宋体");
        //是否加粗
        font.setBold(isBold);
        //字体大小
        font.setFontHeightInPoints(size);
        return font;
    

3、导出

/**
  * 导出
  */
 @RequestMapping("/export")
 public void export(SysUser query, HttpServletResponse response) throws IOException 
     ExportParams params = new ExportParams();
     params.setTitleHeight((short) 15);
     params.setHeight((short) 12);
     params.setStyle(ExcelStyleUtil.class);
     List<SysUser> list = sysUserService.list(query);
     ExcelUtils.exportExcel(params,list, SysUser.class,"用户列表",response);
 

以上是关于easypoi导出excel怎样设置相同内容不合并的主要内容,如果未能解决你的问题,请参考以下文章

使用easypoi时,动态设置excel列宽

使用 easypoi 导出 excel 实现动态列,完美解决!

easypoiword导出图片发现无法读取的内容

C#2008报表控件导出excel格式,能实现类似excel中的合并单元格

poi导出excel问题

poi导出excel有没有设置排序功能