生活随笔
收集整理的這篇文章主要介紹了
java合并单元格同时导出excel
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
POI進(jìn)行跨行需要用到對象HSSFSheet對象,現(xiàn)在就當(dāng)我們程序已經(jīng)定義了一個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對象下標(biāo)都是從0開始的。?
2.單元格合并時Region(1,2,3,4)第1個值的行號必須要比3位置的行號小,如果大于3就不能正常合并單元格?
3.合并單元格的時候要合并的單單元格必須先創(chuàng)建,這樣方便后面再次獲取這個單元格來填充數(shù)據(jù),主要就是因為合并時不能由后向前進(jìn)行合并引起的。?
Java代碼??
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("數(shù)量");??????????????????????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("數(shù)量");??????????????????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("數(shù)量");??????????????????cellb1.setCellStyle(style);??????????????????cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);??????????????????cellb2.setCellValue("金額");??????????????????cellb2.setCellStyle(style);???????????????????????????????????????????????????????????????????????????System.out.print("OK");??????????????}?catch?(Exception?ex)?{??????????????????ex.printStackTrace();??????????????}???????????????}???????????} ? ?
轉(zhuǎn)載于:https://www.cnblogs.com/langtianya/p/6345012.html
總結(jié)
以上是生活随笔為你收集整理的java合并单元格同时导出excel的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。