需要在添加一个单元格合并 /// <summary>
/// 合并单元格
/// </summary>
/// <param name="sheet">要合并单元格所在的sheet</param>
/// <param name="rowstart">开始行的索引</param>
/// <param name="rowend">结束行的索引</param>
/// <param name="colstart">开始列的索引</param>
/// <param name="colend">结束列的索引</param>
public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
{
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
sheet.AddMergedRegion(cellRangeAddress);
}
/// 合并单元格
/// </summary>
/// <param name="sheet">要合并单元格所在的sheet</param>
/// <param name="rowstart">开始行的索引</param>
/// <param name="rowend">结束行的索引</param>
/// <param name="colstart">开始列的索引</param>
/// <param name="colend">结束列的索引</param>
public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
{
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
sheet.AddMergedRegion(cellRangeAddress);
}
你这是把所有的cell都当做是文本类型了?
要是有其他类型呢?
模板中已经合并好了格式
list中存放的数据 是一个二维记录
ContentRowCount 表示 n行为一条记录 我这里为6
FirstRowIndex表示正文启示位置 我这里是3 for (int i = 1; i <= list.Count; i++)
{
for (int j = 0; j < this.ContentRowCount; j++)
{
Sheet.CopyRow(FirstRowIndex, FirstRowIndex + i*this.ContentRowCount);
}
}
以上代码有误...
正确代码
for (int i = 1; i <= list.Count; i++)
{
for (int j = 0; j < this.ContentRowCount; j++)
{
Sheet.CopyRow(FirstRowIndex+j, FirstRowIndex + i*this.ContentRowCount+j);
}
}
int tLastIndex = FirstRowIndex + list.Count*ContentRowCount;
for (int i = 0; i < ContentRowCount; i++)
{
IRow row = Sheet.GetRow(i + tLastIndex);
if (row==null)
{
continue;
}
Sheet.RemoveRow(row);
}第一个循环建立样式,但是最后一排,总是会出现样式合并的错误,所以我干脆多复制了一大行(i <= list.Count) 第二个循环再删除这多出的一行
然后后面填充数据.