一:前言
最近幫一個朋友做excel的導出功能,對於我來說還是挺頭疼,我看了下表格樣式,對於我來說還是挺頭疼的,想當年耗子剛剛出社會的時候做的第一份工作,第一份任務就是把把word轉換為html,在這個過程中,嘗試了太多方式,但是並不能保證所有的都能轉換成功,復雜的word轉換,依然會造成錯亂。excel的導出我也還沒去做過,設計到單元格的合並,我去看了看官方文檔,其實也沒看出什麼名堂,然後就在網上查找了,下面記錄下自己踩的坑吧,其實沒用多久時間,從晚上八點開始做,三個小時完成,但是這之間做了很多實驗。
二:內容
先說網上搜索的結果,我搜了"hssfrow 合並單元格",找到了兩篇文章結合
(1):http://dacoolbaby.iteye.com/blog/1630957
(2):http://blog.csdn.net/hehexiaoyou/article/details/37873131
先說我的測試,我把(1)中的代碼弄到本地跑,但是一直報錯,主要是
//表示合並B2,B3
sheet.addMergedRegion(new Region(
1, //first row (0-based)
(short)1, //first column (0-based)
2, //last row (0-based)
(short)1 //last column (0-based)
));
主要是紅色這個類,我查閱了下文檔,並結合了(2)做了修改,貼上代碼
package com.mouse.moon.frepoi;
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.HSSFFont;
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.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
//測試復雜的單元格合並
public class TestFreMergedCells {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
//HSSFRow row = sheet.createRow(0);
//HSSFCell cell = row.createCell(1); cell.setCellValue("value"); 這個代表設置值為(0,1)第1行第二列
//設置值是(0,0)代表第1行第1列,如果你想到的是我既然合並了第一列的第一行和第二行,我是否可以設置(1,0,
//你可以測試下,這個顯然是不行的
cell.setCellValue("第一行第二行合並,第一列");
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
//合並第二列的第一行第二行
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
//1.生成字體對象
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 10);
font.setFontName("新宋體");
font.setColor(HSSFColor.BLUE.index);
font.setBoldweight((short) 0.8);
//2.生成樣式對象
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setFont(font); //調用字體樣式對象
style.setWrapText(true);
//增加表格邊框的樣式 例子
style.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
style.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);
style.setTopBorderColor(HSSFColor.GOLD.index);
style.setLeftBorderColor(HSSFColor.PLUM.index);
//3.單元格應用樣式
cell.setCellStyle(style);
//行:合並第一行,第二行,第三行
//列說明:列也是從0開始的,0代表第一列;
//列:合並第五列,第六列,第七列,第八列,第九列
HSSFRow rowOther = sheet.createRow(0);
HSSFCell cellOther = rowOther.createCell(4);
cellOther.setCellValue("多行多列合並");
sheet.addMergedRegion(new CellRangeAddress(0, 2, 4, 9));
//一下代碼表示在D4 cell 插入一段字符串
HSSFRow row2 = sheet.createRow(3);
HSSFCell cell2 = row2.createCell(3);
cell2.setCellValue("只是一個數據而已");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("/Mouse/poi/test.xls");
wb.write(fileOut);
fileOut.close();
}
}
做下簡單的說明,其實自己測試也能發現,那麼就是對於合並單元格設置數據的問題。
HSSFRow rowOther = sheet.createRow(0);
HSSFCell cellOther = rowOther.createCell(4);
cellOther.setCellValue("多行多列合並");
sheet.addMergedRegion(new CellRangeAddress(0, 2, 4, 9));
比如這個創建第1行(0代表第1行),第5(4代表第5列)列,下面合並了第一到第三行,第5列到第19列,那麼我設置值的時候只能設置坐標為(0,4)的,如果你改為如下:
HSSFRow rowOther = sheet.createRow(1);
HSSFCell cellOther = rowOther.createCell(4);
cellOther.setCellValue("多行多列合並");
sheet.addMergedRegion(new CellRangeAddress(0, 2, 4, 9));
此時的賦值是第2行,第五列,其實也是在合並的單元格內,但是最後生存的表格確是沒數據的。
我做的表頭是動態生成的,行也是動態的,最後導出的example如下圖

三:總結
其實解決一個問題,只要找到了門路,然後,自己冷靜的去嘗試,去找文檔,很多問題都不是自己想象的那麼困難。好久沒寫博客了,其實有些,只是一直放在草稿箱的,最近應該會堅持寫完發出來了。努力,這一年努力,多看書,計劃照常進行。GO。