首页 新闻 赞助 找找看

npoi 合并单元格 之后设置单元格居中

0
悬赏园豆:30 [已解决问题] 解决于 2018-11-20 13:10

我采用npoi 合并了单元格,但是现在有一个需求,我需要将合并后的单元格垂直居中显示,没有找到npoi对应的操作,请熟悉的朋友指教一下,非常感谢!

baidixing的主页 baidixing | 初学一级 | 园豆:107
提问于:2014-04-03 15:25
< >
分享
最佳答案
1

//设置style
ICellStyle cellstyle = workbook.CreateCellStyle();
cellstyle.VerticalAlignment = VerticalAlignment.Center;
cellstyle.Alignment = HorizontalAlignment.Center;

//合并操作

sheet.AddMergedRegion(new CellRangeAddress(index["firstRow"], index["lastRow"], index["firstCol"], index["lastCol"]));//起始行,结束行,起始列,结束列

//设置合并后style
var cell = sheet.GetRow(index["firstRow"]).GetCell(index["firstCol"]);
cell.CellStyle = cellstyle;

收获园豆:30
Leap李 | 菜鸟二级 |园豆:246 | 2018-02-08 10:23
其他回答(3)
-1

為右上角的那個单元格设个样式为居中对齐试试

清风君 | 园豆:372 (菜鸟二级) | 2014-04-03 15:44

没有效果,我取的是第一个单元格,学经历还是在底部 没有垂直居中

支持(0) 反对(0) baidixing | 园豆:107 (初学一级) | 2014-04-03 16:08
0

合并0列的n--n+2行:

CellRangeAddress m_region = new CellRangeAddress(n,n+2, 0, 0);  //合并0列的n--n+2行

sheet.AddMergedRegion(m_region);

IRow row = sheet.CreateRow(n);

ICell cell = row.CreateCell(0);

ICellStyle cellstyle = hssfworkbook.CreateCellStyle();//设置垂直居中格式

cellstyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中

cell.CellStyle = cellstyle;

gltide | 园豆:403 (菜鸟二级) | 2014-10-10 22:52
0

 HSSFWorkbook book = new HSSFWorkbook();

ICellStyle cellStyle = book.CreateCellStyle();

//设置单元格的样式:水平对齐居中
cellStyle.VerticalAlignment = VerticalAlignment.Justify;//垂直对齐(默认应该为center,如果center无效则用justify)
cellStyle.Alignment = HorizontalAlignment.Center;//水平对齐

cell.CellStyle=cellstyle;

iV5 | 园豆:202 (菜鸟二级) | 2016-04-12 10:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册