POI實現EXCEL單元格合並及邊框樣式
下面例子為創建產生一個excel,合並單元格,然后為合並后的單元格添加邊框
- package test;
- 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.ss.usermodel.BorderStyle;
- import org.apache.poi.ss.usermodel.Font;
- import org.apache.poi.ss.usermodel.HorizontalAlignment;
- import org.apache.poi.ss.util.CellRangeAddress;
- import org.apache.poi.ss.util.RegionUtil;
- public class ExcelPoiTest {
- public static void main(String[] args) {
- HSSFWorkbook workbook = new HSSFWorkbook(); // 創建一個excel
- // excel生成過程: excel-->sheet-->row-->cell
- HSSFSheet sheet = workbook.createSheet("test"); // 為excel創建一個名為test的sheet頁
- HSSFRow row = sheet.createRow(1); // 創建一行,參數2表示第一行
- HSSFCell cellB2 = row.createCell(1); // 在B2位置創建一個單元格
- HSSFCell cellB3 = row.createCell(2); // 在B3位置創建一個單元格
- cellB2.setCellValue("單元格B2"); // B2單元格填充內容
- cellB3.setCellValue("單元格B3"); // B3單元格填充內容
- HSSFCellStyle cellStyle = workbook.createCellStyle(); // 單元格樣式
- Font fontStyle = workbook.createFont(); // 字體樣式
- fontStyle.setBold(true); // 加粗
- fontStyle.setFontName("黑體"); // 字體
- fontStyle.setFontHeightInPoints((short) 11); // 大小
- // 將字體樣式添加到單元格樣式中
- cellStyle.setFont(fontStyle);
- // 邊框,居中
- cellStyle.setAlignment(HorizontalAlignment.CENTER);
- cellStyle.setBorderBottom(BorderStyle.THIN);
- cellStyle.setBorderLeft(BorderStyle.THIN);
- cellStyle.setBorderRight(BorderStyle.THIN);
- cellStyle.setBorderTop(BorderStyle.THIN);
- cellB2.setCellStyle(cellStyle); // 為B2單元格添加樣式
- // 合並單元格
- CellRangeAddress cra =new CellRangeAddress(1, 3, 1, 3); // 起始行, 終止行, 起始列, 終止列
- sheet.addMergedRegion(cra);
- // 使用RegionUtil類為合並后的單元格添加邊框
- RegionUtil.setBorderBottom(1, cra, sheet); // 下邊框
- RegionUtil.setBorderLeft(1, cra, sheet); // 左邊框
- RegionUtil.setBorderRight(1, cra, sheet); // 有邊框
- RegionUtil.setBorderTop(1, cra, sheet); // 上邊框
- // 輸出到本地
- String excelName = "/myExcel.xls";
- FileOutputStream out = null;
- try {
- out = new FileOutputStream(excelName);
- workbook.write(out);
- out.flush();
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (out != null)
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- out = null;
- }
- }
- }
生成的excel樣式為

簡單說明:
1.excel生成過程: excel-->sheet-->row-->cell
2.索引從0開始
3.合並單元格后保留最左上角的單元格(B3單元格被B2單元格覆蓋)
4.合並后單元格邊框通過RegionUtil設置,如果刪除以下代碼
- <span style="font-size:18px;"><strong> // 使用RegionUtil類為合並后的單元格添加邊框
- RegionUtil.setBorderBottom(1, cra, sheet); // 下邊框
- RegionUtil.setBorderLeft(1, cra, sheet); // 左邊框
- RegionUtil.setBorderRight(1, cra, sheet); // 有邊框
- RegionUtil.setBorderTop(1, cra, sheet); // 上邊框</strong></span>
可以看到只有B2單元格有邊框。
java poi 合並單元格
- XSSFWorkbook wb = new XSSFWorkbook();
- XSSFSheet sheet = wb.createSheet();
- //這個就是合並單元格
- //參數說明:1:開始行 2:結束行 3:開始列 4:結束列
- //比如我要合並 第二行到第四行的 第六列到第八列 sheet.addMergedRegion(new CellRangeAddress(1,3,5,7));
- sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
- XSSFRow row = sheet.createRow(number);
需要注意的地方(不對的地方請指教)
感覺唯一要注意的地方就是:需要先設置 合並單元格,然后再 生成 行。
比如我們要生成的單元格為:
1工作站 | 左位置 | 1序號 | 2000訂單號 | 1成品號/型號 |
左位置 | 2序號 | |||
左位置 | 3序號 | |||
右位置 | 4序號 |
代碼可以這樣寫:
- sheet.addMergedRegion(new CellRangeAddress(0,3,0,0));
- sheet.addMergedRegion(new CellRangeAddress(0,3,3,3));
- sheet.addMergedRegion(new CellRangeAddress(0,3,4,4));
- //第一行數據
- XSSFRow row = sheet.createRow(0);
- row.createCell(0).setCellValue("工作站");
- row.createCell(1).setCellValue("位置");
- row.createCell(2).setCellValue("序號");
- row.createCell(3).setCellValue("訂單號");
- row.createCell(4).setCellValue("成品號/型號");
- //第二行數據
- XSSFRow row = sheet.createRow(number);
- //row.createCell(0).setCellValue("工作站");//因為和上面的行合並了,所以不用再次 賦值了
- row.createCell(1).setCellValue("位置");
- row.createCell(2).setCellValue("序號");
- //row.createCell(3).setCellValue("訂單號");//因為和上面的行合並了,所以不用再次 賦值了
- //row.createCell(4).setCellValue("成品號/型號");//因為和上面的行合並了,所以不用再次 賦值了
第三行數據和 第二行是一樣的