poi导出excel合并单元格(包括列合并、行合并)

1 工程所需jar包如下:
commons-codec-1.5.jar
commons-logging-1.1.jar
log4j-1.2.13.jar
junit-3.8.1.jar
poi-3.9-20121203.jar

2 Code:

/**
 * 导出设备信息Excel
 * @param form      和 HTTP 请求相关的表格对象
 * @param resources 信息资源对象
 * @param locale    本地化对象
 * @param session   HTTP 会话对象
 * @param request   HTTP 请求对象
 * @param response  HTTP 响应对象
 * @return
 */
public String exportExcel(DynaBean form, MessageResources resources,
        Locale locale, HttpSession session, HttpServletRequest request,
        HttpServletResponse response) throws Exception{
    int iLanguage = (locale.getLanguage().indexOf("en")>=0)?0:1;
    response.reset();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(resources.getMessage(locale, "device.details")+".xls","UTF-8"));
    OutputStream sos = response.getOutputStream();
    List<DeviceVO> deviceList = dao.getAllDevice();
    HSSFWorkbook wb = new HSSFWorkbook();
    Map<String, CellStyle> styles = createStyles(wb);
    // 创建sheet页
    Sheet sheet = wb.createSheet("Sheet");
    PrintSetup printSetup = sheet.getPrintSetup();
    printSetup.setLandscape(true);
    sheet.setFitToPage(true);
    sheet.setHorizontallyCenter(true);
    /**
    *合并单元格的行或者列
    */
    sheet.addMergedRegion(CellRangeAddress.valueOf("$F$1:$H$1"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$M$1:$P$1"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$Q$1:$S$1"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$A$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$B$1:$B$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$C$1:$C$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$D$1:$D$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$E$1:$E$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$I$1:$I$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$J$1:$J$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$K$1:$K$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$L$1:$L$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$T$1:$T$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$U$1:$U$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$V$1:$V$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$W$1:$W$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$X$1:$X$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$Y$1:$Y$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$Z$1:$Z$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$AA$1:$AA$2"));
    sheet.addMergedRegion(CellRangeAddress.valueOf("$AB$1:$AB$2"));  

    // 创建表头
    Row headerRow = sheet.createRow(0);
    headerRow.setHeightInPoints(30);
    Cell headerCell;  

    headerCell = headerRow.createCell(0);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.number")); //设备编号
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(1);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.qrcode")); //设备二维码
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(2);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.customerbase")); //客户群
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(3);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.customertype")); //客户类别
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(4);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.customername")); //客户名称
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(5);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.area")); //设备区域
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(8);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.itemname")); //所属项目名称
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(9);
    headerCell.setCellValue(resources.getMessage(locale, "device.category")); //设备类别
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(10);
    headerCell.setCellValue(resources.getMessage(locale, "device.name")); //设备名称
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(11);
    headerCell.setCellValue(resources.getMessage(locale, "device.no")); //设备信息编号
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(12);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.baseinfomation")); //设备基本信息
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(16);
    headerCell.setCellValue(resources.getMessage(locale, "device.location")); //设备位置
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(19);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.enabledate")); //设备启用日期
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(20);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.backendload")); //后端负载
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(21);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.operationtips")); //操作提示
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(22);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.maintenancepeople")); //维护责任人
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(23);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.usetime")); //巡检时间
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(24);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.intervaltime")); //巡检下台设备时间间隔
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(25);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.patrolpath")); //巡检路径
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(26);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.enable")); //是否启用巡检路径
    headerCell.setCellStyle(styles.get("header"));  

    headerCell = headerRow.createCell(27);
    headerCell.setCellValue(resources.getMessage(locale, "device.export.excel.other")); //其他信息
    headerCell.setCellStyle(styles.get("header"));  

    Row headerRowRegion = sheet.createRow(1);
    headerRowRegion.setHeightInPoints(15);
    Cell headerCellRegion;
    headerCellRegion = headerRowRegion.createCell(5);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.province")); //省份
    headerCellRegion.setCellStyle(styles.get("header"));  

    headerCellRegion = headerRowRegion.createCell(6);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.region")); //市
    headerCellRegion.setCellStyle(styles.get("header"));  

    headerCellRegion = headerRowRegion.createCell(7);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.county")); //县/区
    headerCellRegion.setCellStyle(styles.get("header"));  

    headerCellRegion = headerRowRegion.createCell(12);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.brand")); //品牌
    headerCellRegion.setCellStyle(styles.get("header"));  

    headerCellRegion = headerRowRegion.createCell(13);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.model")); //型号
    headerCellRegion.setCellStyle(styles.get("header"));  

    headerCellRegion = headerRowRegion.createCell(14);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.capacity")); //容量
    headerCellRegion.setCellStyle(styles.get("header"));  

    headerCellRegion = headerRowRegion.createCell(15);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.export.excel.systemtype")); //系统类型
    headerCellRegion.setCellStyle(styles.get("header"));  

    headerCellRegion = headerRowRegion.createCell(16);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.build")); //楼号
    headerCellRegion.setCellStyle(styles.get("header"));  

    headerCellRegion = headerRowRegion.createCell(17);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.floor")); //楼层
    headerCellRegion.setCellStyle(styles.get("header"));  

    headerCellRegion = headerRowRegion.createCell(18);
    headerCellRegion.setCellValue(resources.getMessage(locale, "device.room")); //房间号
    headerCellRegion.setCellStyle(styles.get("header"));  

    for(int i=0;i<deviceList.size();i++){
        DeviceVO device = deviceList.get(i);
        Cell rowCell;
        Row cellRow = sheet.createRow(i + 2);  

        rowCell = cellRow.createCell(0);
        rowCell.setCellValue(device.getDeviceId());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(1);
        rowCell.setCellValue(device.getQrcode());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(2);
        //判断是否是中文
        if(iLanguage==1){
            rowCell.setCellValue(device.getItemId().getCustomerBase().substring(device.getItemId().getCustomerBase().indexOf(":")+1,device.getItemId().getCustomerBase().length()));
        }else if(iLanguage==0){ //判断是否是英文
            rowCell.setCellValue(device.getItemId().getCustomerBase().substring(0,device.getItemId().getCustomerBase().indexOf(":")));
        }
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(3);
        //判断是否是中文
        if(iLanguage==1){
            rowCell.setCellValue(device.getItemId().getCustomerType().substring(device.getItemId().getCustomerType().indexOf(":")+1,device.getItemId().getCustomerType().length()));
        }else if(iLanguage==0){ //判断是否是英文
            rowCell.setCellValue(device.getItemId().getCustomerType().substring(0,device.getItemId().getCustomerType().indexOf(":")));
        }
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(4);
        rowCell.setCellValue(device.getItemId().getCustomerName());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(5);
        rowCell.setCellValue(device.getItemId().getProvince());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(6);
        rowCell.setCellValue(device.getItemId().getCity());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(7);
        rowCell.setCellValue(device.getItemId().getCounty());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(8);
        //判断是否是中文
        if(iLanguage==1){
            rowCell.setCellValue(device.getItemId().getItemName());
        }else if(iLanguage==0){ //判断是否是英文
            rowCell.setCellValue(device.getItemId().getForShort());
        }
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(9);
        //判断是否是中文
        if(iLanguage==1){
            rowCell.setCellValue(device.getZequipId().getZequipGroup().getNameLoc());
        }else if(iLanguage==0){ //判断是否是英文
            rowCell.setCellValue(device.getZequipId().getZequipGroup().getNameEn());
        }
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(10);
        rowCell.setCellValue(device.getDeviceName());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(11);
        rowCell.setCellValue(device.getDeviceNo());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(12);
        rowCell.setCellValue(device.getDeviceBrand());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(13);
        rowCell.setCellValue(device.getEquipDriveId().getModel());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(14);
        rowCell.setCellValue(device.getDeviceCapacity());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(15);
        rowCell.setCellValue(device.getSystemType());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(16);
        rowCell.setCellValue(device.getStairsNo());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(17);
        rowCell.setCellValue(device.getFloor());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(18);
        rowCell.setCellValue(device.getRoomNo());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(19);
        rowCell.setCellValue(device.getEnableDate()!=null ? new SimpleDateFormat("yyyy-MM-dd").format(device.getEnableDate()) : "");
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(20);
        rowCell.setCellValue(device.getBackendLoad());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(21);
        rowCell.setCellValue(device.getOperationTips());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(22);
        rowCell.setCellValue(device.getPersonId().getUserName());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(23);
        rowCell.setCellValue(device.getInspecTime()+resources.getMessage(locale, "device.export.excel.minute"));
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(24);
        rowCell.setCellValue(device.getIntervalTime()+resources.getMessage(locale, "device.export.excel.minute"));
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(25);
        rowCell.setCellValue(device.getDevicePath());
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(26);
        rowCell.setCellValue(device.getValidityNr() == 0 ? resources.getMessage(locale, "mmc.soft.person.disabled") : resources.getMessage(locale, "mmc.soft.person.enable"));
        rowCell.setCellStyle(styles.get("cell"));  

        rowCell = cellRow.createCell(27);
        rowCell.setCellValue(device.getOtherInfo() != null ? device.getOtherInfo() : "");
        rowCell.setCellStyle(styles.get("cell"));
    }
    wb.write(sos);
    sos.flush();
    sos.close();
    return null;
}  

//excel样式
private Map<String, CellStyle> createStyles(Workbook wb)
{
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    CellStyle style;
    Font titleFont = wb.createFont();
    titleFont.setFontHeightInPoints((short) 18);
    titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(titleFont);
    styles.put("title", style);  

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setWrapText(true);
    styles.put("header", style);  

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setWrapText(true);
    styles.put("cell", style);  

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
    styles.put("formula", style);  

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
    styles.put("formula_2", style);  

    return styles;
}

  

时间: 2024-11-07 05:26:49

poi导出excel合并单元格(包括列合并、行合并)的相关文章

C#.Net 导出Excel 之单元格 相关设置

range.NumberFormatLocal = "@";     //设置单元格格式为文本range = (Range)worksheet.get_Range("A1", "E1");     //获取Excel多个单元格区域:本例做为Excel表头range.Merge(0);     //单元格合并动作worksheet.Cells[1, 1] = "Excel单元格赋值";     //Excel单元格赋值range

POI 实现合并单元格以及列自适应宽度

POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 2)); 自适应列宽度: sheet.autoSizeColumn(1); sheet.autoSizeColumn(1, true); 这两种方式都是自适应列宽度,但是注意这个方法在后边的版本才提供,poi的版本不要太老. 注意:第一个方法在合并单元格的的单元格并不好使,必须用

poi获取合并单元格内的第一行第一列的值

当读取如图所示的excel时,显示为第1行 第1列 的内容是:合并单元格 其它在合并单元格区域内的单元格不显示 示例代码如下: 1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.IOException; 4 5 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 6 import org.apache.poi.ss.

DataGridView合并单元格(一列或一行)之一

#region"合并单元格的测试(一列或一行)" // int?是搜索一种类型(可空类型),普通的int不能为null,而用int?,其值可以为null //private int? nextrow = null; //private int? nextcol = null; //在CellPainting方法后调用 private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGri

POI向Excel的单元格中写入图片

HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现 XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现 在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片. 针对.xls文件导出图片 public class ExcelImageTest { public static void main(Strin

EasyUi 合并单元格占列显示

$("#TableContainer").datagrid({                url: '',                method: "get",                title: '工资明细列表',                loadMsg: '数据加载中,请稍候...',                nowrap: false,                pageSize: 50,                pag

apache poi 合并单元格 设置边框

HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle styleBorderThin= wb.createCellStyle(); setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左

【记录】解析具有合并单元格的Excel

最近公司让做各种数据表格的导入导出,就涉及到电子表格的解析,做了这么多天总结一下心得. 工具:NOPI 语言:C# 目的:因为涉及到导入到数据库,具有合并单元格的多行必然要拆分,而NPOI自动解析的时候拆分单元格除第一个单元格外其余值都是空,对于列头有合并项目的,数据库设计一般才有合并单元格下面的最小列单元作为数据库字段.于是希望达到这样一个效果.于是有了一个思路就是把读入的Excel复制到新建的Excel,然后再去读新的Excel.总体思路就是把合并单元格所包含的所有最小单元格的值都设置成合并

NPOI操作EXCEL(五)——含合并单元格复杂表头的EXCEL解析

我们在第三篇文章中谈到了那些非常反人类的excel模板,博主为了养家糊口,也玩命做出了相应的解析方法... 我们先来看看第一类复杂表头: ...... 博主称这类excel模板为略复杂表头模板(蓝色部分为表头部分,蓝色前面几行是博主项目的基础样式,称为元数据),这类excel的表头多为2-3行,甚至于5/6行 ,具有合并层级关系,看似复杂,但只需要在我们以前的基础上稍微做一下重构就可以完美实现解析. 我们以各地区户籍人口城乡构成表头为例: 其实,只要我们能准确解析这类表头所表达的意思,就能复用以