npoi完整复制行?
npoi完整复制行,包括合并行,样式等等。完整的复制一行数据?以下方法,不能复制合并行。private void CopyRange(HSSFWorkbook myHSSFWorkBook, int fromRowIndex, int fromColIndex, int toRowIndex, int toColIndex, bool onlyData, bool copyComment)
{
HSSFRow sourceRow = myHSSFWorkBook.GetSheetAt(myHSSFWorkBook.ActiveSheetIndex).GetRow(fromRowIndex);
HSSFCell sourceCell = sourceRow.GetCell(fromColIndex);
if (sourceRow != null && sourceCell != null)
{
HSSFRow changingRow = null;
HSSFCell changingCell = null;
changingRow = myHSSFWorkBook.GetSheetAt(myHSSFWorkBook.ActiveSheetIndex).GetRow(toRowIndex);
if (changingRow == null)
changingRow = myHSSFWorkBook.GetSheetAt(myHSSFWorkBook.ActiveSheetIndex).CreateRow(toRowIndex);
changingCell = changingRow.GetCell(toColIndex);
if (changingCell == null)
changingCell = changingRow.CreateCell(toColIndex);
if (onlyData)//仅数据
{
//对单元格的值赋值
changingCell.SetCellValue(sourceCell.StringCellValue);
}
else //非仅数据
{
//单元格的编码
changingCell.Encoding = sourceCell.Encoding;
//单元格的格式
changingCell.CellStyle = sourceCell.CellStyle;
//单元格的公式
if (sourceCell.CellFormula == "")
changingCell.SetCellValue(sourceCell.StringCellValue);
else
changingCell.SetCellFormula(sourceCell.CellFormula);
//对单元格的批注赋值
if (copyComment)
{
if (sourceCell.CellComment != null)
{
HSSFPatriarch patr = myHSSFWorkBook.GetSheetAt(myHSSFWorkBook.ActiveSheetIndex).CreateDrawingPatriarch();
HSSFComment comment = patr.CreateComment(new HSSFClientAnchor(0, 0, 0, 0, toColIndex, toRowIndex, toColIndex + 1, toRowIndex + 1));
comment.String = new HSSFRichTextString(sourceCell.CellComment.String.ToString());
comment.Author = sourceCell.CellComment.Author;
changingCell.CellComment = comment;
}
}
}
}
}
--------------------编程问答-------------------- 萬能的主啊 。賜給我一個答案把。 --------------------编程问答-------------------- 需要在添加一个单元格合并
/// <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);
}
--------------------编程问答-------------------- changingCell.SetCellValue(sourceCell.StringCellValue);
你这是把所有的cell都当做是文本类型了?
要是有其他类型呢? --------------------编程问答-------------------- 楼主 ,我今天完成了合并单元格的复制,但说实话 我自己是怎么完成的 我自己也不知道哦.
我是读取的已有excel模板
模板中已经合并好了格式
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) 第二个循环再删除这多出的一行
然后后面填充数据.
补充:.NET技术 , ASP.NET