POI进行跨行需要用到对象HSSFSheet对象,现在就当我们程序已经定义了一个HSSFSheet对象sheet。?
跨第1行第1个到第2个单元格的操作为?
sheet.addMergedRegion(new Region(0,(short)0,0,(short)1));?
跨第1行第1个到第2行第1个单元格的操作为?
sheet.addMergedRegion(new Region(0,(short)0,1,(short)0));?
重点注意事项:?
1.单元格CELL和ROW对象下标都是从0开始的。?
2.单元格合并时Region(1,2,3,4)第1个值的行号必须要比3位置的行号小,如果大于3就不能正常合并单元格?
3.合并单元格的时候要合并的单单元格必须先创建,这样方便后面再次获取这个单元格来填充数据,主要就是因为合并时不能由后向前进行合并引起的。?
Java代码??
class="star" alt="收藏代码" style="border: 0px;" src="/Upload/Images/2014110700/40B102E0EF997EA6.png">
- 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?{??????
- ?????
- ?????
- ?
- ?????
- ????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++)?{???
- ??????????????????
- ????????????????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);??????
- ?????
- ?????????????
- ?
- ?
- ?
- ??
- ??????????????
- ??????????????
- ?????????????
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????????????System.out.print("OK");??????
- ????????}?catch?(Exception?ex)?{??????
- ????????????ex.printStackTrace();??????
- ????????}??????
- ?????
- ????}??????
- ?????
- } ? ?