NPOI 复制Sheet(转载)
2014-02-20 16:04
435 查看
以下内容由chocolateboy兄所写,在此仅作为转帖
原文载于:/article/11667901.html
NPOI已经发布到了2.0的版本,可喜的是该版本支持到了Excel2007,但缺憾的是没有复制SHEET表的功能,只有一个CloneSheet方法,确实没明白怎么使用。所以在网上搜索了一下,只找一个JAVA的版本,好像也是自己写的代码实现的。于是拿过来翻译了一下,复制SHEET后的格式基本没变,但单元格的宽度没有实现,ICell接口没有类似于Width的属性,只好作罢。
哪位如果知道CloneSheet方法的使用,恳请留言,免去不必要的操作和代码。
[csharp]
view plaincopyprint?
public class SheetClone
{
public staticvoid CopyCellStyle(IWorkbook wb, ICellStyle fromStyle, ICellStyle toStyle)
{
toStyle.Alignment = fromStyle.Alignment;
//边框和边框颜色
toStyle.BorderBottom = fromStyle.BorderBottom;
toStyle.BorderLeft = fromStyle.BorderLeft;
toStyle.BorderRight = fromStyle.BorderRight;
toStyle.BorderTop = fromStyle.BorderTop;
toStyle.TopBorderColor = fromStyle.TopBorderColor;
toStyle.BottomBorderColor = fromStyle.BottomBorderColor;
toStyle.RightBorderColor = fromStyle.RightBorderColor;
toStyle.LeftBorderColor = fromStyle.LeftBorderColor;
//背景和前景
toStyle.FillBackgroundColor = fromStyle.FillBackgroundColor;
toStyle.FillForegroundColor = fromStyle.FillForegroundColor;
toStyle.DataFormat = fromStyle.DataFormat;
toStyle.FillPattern = fromStyle.FillPattern;
//toStyle.Hidden=fromStyle.Hidden;
toStyle.IsHidden = fromStyle.IsHidden;
toStyle.Indention = fromStyle.Indention;//首行缩进
toStyle.IsLocked = fromStyle.IsLocked;
toStyle.Rotation = fromStyle.Rotation;//旋转
toStyle.VerticalAlignment = fromStyle.VerticalAlignment;
toStyle.WrapText = fromStyle.WrapText;
toStyle.SetFont(fromStyle.GetFont(wb));
}
/// <summary>
/// 复制表
/// </summary>
/// <param name="wb"></param>
/// <param name="fromSheet"></param>
/// <param name="toSheet"></param>
/// <param name="copyValueFlag"></param>
public staticvoid CopySheet(IWorkbook wb, ISheet fromSheet, ISheet toSheet,bool
copyValueFlag)
{
//合并区域处理
MergerRegion(fromSheet, toSheet);
System.Collections.IEnumerator rows = fromSheet.GetRowEnumerator();
while (rows.MoveNext())
{
IRow row = null;
if (wb is HSSFWorkbook)
row = rows.Current as HSSFRow;
else
row = rows.Current as NPOI.XSSF.UserModel.XSSFRow;
IRow newRow = toSheet.CreateRow(row.RowNum);
CopyRow(wb, row, newRow, copyValueFlag);
}
}
/// <summary>
/// 复制行
/// </summary>
/// <param name="wb"></param>
/// <param name="fromRow"></param>
/// <param name="toRow"></param>
/// <param name="copyValueFlag"></param>
public staticvoid CopyRow(IWorkbook wb, IRow fromRow, IRow toRow,bool
copyValueFlag)
{
System.Collections.IEnumerator cells = fromRow.GetEnumerator();//.GetRowEnumerator();
toRow.Height = fromRow.Height;
while (cells.MoveNext())
{
ICell cell = null;
//ICell cell = (wb is HSSFWorkbook) ? cells.Current as HSSFCell : cells.Current as NPOI.XSSF.UserModel.XSSFCell;
if (wb is HSSFWorkbook)
cell = cells.Current as HSSFCell;
else
cell = cells.Current as NPOI.XSSF.UserModel.XSSFCell;
ICell newCell = toRow.CreateCell(cell.ColumnIndex);
CopyCell(wb, cell, newCell, copyValueFlag);
}
}
/// <summary>
/// 复制原有sheet的合并单元格到新创建的sheet
/// </summary>
/// <param name="fromSheet"></param>
/// <param name="toSheet"></param>
public staticvoid MergerRegion(ISheet fromSheet, ISheet toSheet)
{
int sheetMergerCount = fromSheet.NumMergedRegions;
for (int i = 0; i < sheetMergerCount; i++)
{
//Region mergedRegionAt = fromSheet.GetMergedRegion(i); //.MergedRegionAt(i);
//CellRangeAddress[] cra = new CellRangeAddress[1];
//cra[0] = fromSheet.GetMergedRegion(i);
//Region[] rg = Region.ConvertCellRangesToRegions(cra);
toSheet.AddMergedRegion(fromSheet.GetMergedRegion(i));
}
}
/// <summary>
/// 复制单元格
/// </summary>
/// <param name="wb"></param>
/// <param name="srcCell"></param>
/// <param name="distCell"></param>
/// <param name="copyValueFlag"></param>
public staticvoid CopyCell(IWorkbook wb, ICell srcCell, ICell distCell,bool copyValueFlag)
{
ICellStyle newstyle = wb.CreateCellStyle();
CopyCellStyle(wb, srcCell.CellStyle, newstyle);
//样式
distCell.CellStyle = newstyle;
//评论
if (srcCell.CellComment !=
null)
{
distCell.CellComment = srcCell.CellComment;
}
// 不同数据类型处理
CellType srcCellType = srcCell.CellType;
distCell.SetCellType(srcCellType);
if (copyValueFlag)
{
if (srcCellType == CellType.NUMERIC)
{
if (HSSFDateUtil.IsCellDateFormatted(srcCell))
{
distCell.SetCellValue(srcCell.DateCellValue);
}
else
{
distCell.SetCellValue(srcCell.NumericCellValue);
}
}
else if (srcCellType == CellType.STRING)
{
distCell.SetCellValue(srcCell.RichStringCellValue);
}
else if (srcCellType == CellType.BLANK)
{
// nothing21
}
else if (srcCellType == CellType.BOOLEAN)
{
distCell.SetCellValue(srcCell.BooleanCellValue);
}
else if (srcCellType == CellType.ERROR)
{
distCell.SetCellErrorValue(srcCell.ErrorCellValue);
}
else if (srcCellType == CellType.FORMULA)
{
distCell.SetCellFormula(srcCell.CellFormula);
}
else
{ // nothing29
}
}
}
}
原文载于:/article/11667901.html
NPOI已经发布到了2.0的版本,可喜的是该版本支持到了Excel2007,但缺憾的是没有复制SHEET表的功能,只有一个CloneSheet方法,确实没明白怎么使用。所以在网上搜索了一下,只找一个JAVA的版本,好像也是自己写的代码实现的。于是拿过来翻译了一下,复制SHEET后的格式基本没变,但单元格的宽度没有实现,ICell接口没有类似于Width的属性,只好作罢。
哪位如果知道CloneSheet方法的使用,恳请留言,免去不必要的操作和代码。
[csharp]
view plaincopyprint?
public class SheetClone
{
public staticvoid CopyCellStyle(IWorkbook wb, ICellStyle fromStyle, ICellStyle toStyle)
{
toStyle.Alignment = fromStyle.Alignment;
//边框和边框颜色
toStyle.BorderBottom = fromStyle.BorderBottom;
toStyle.BorderLeft = fromStyle.BorderLeft;
toStyle.BorderRight = fromStyle.BorderRight;
toStyle.BorderTop = fromStyle.BorderTop;
toStyle.TopBorderColor = fromStyle.TopBorderColor;
toStyle.BottomBorderColor = fromStyle.BottomBorderColor;
toStyle.RightBorderColor = fromStyle.RightBorderColor;
toStyle.LeftBorderColor = fromStyle.LeftBorderColor;
//背景和前景
toStyle.FillBackgroundColor = fromStyle.FillBackgroundColor;
toStyle.FillForegroundColor = fromStyle.FillForegroundColor;
toStyle.DataFormat = fromStyle.DataFormat;
toStyle.FillPattern = fromStyle.FillPattern;
//toStyle.Hidden=fromStyle.Hidden;
toStyle.IsHidden = fromStyle.IsHidden;
toStyle.Indention = fromStyle.Indention;//首行缩进
toStyle.IsLocked = fromStyle.IsLocked;
toStyle.Rotation = fromStyle.Rotation;//旋转
toStyle.VerticalAlignment = fromStyle.VerticalAlignment;
toStyle.WrapText = fromStyle.WrapText;
toStyle.SetFont(fromStyle.GetFont(wb));
}
/// <summary>
/// 复制表
/// </summary>
/// <param name="wb"></param>
/// <param name="fromSheet"></param>
/// <param name="toSheet"></param>
/// <param name="copyValueFlag"></param>
public staticvoid CopySheet(IWorkbook wb, ISheet fromSheet, ISheet toSheet,bool
copyValueFlag)
{
//合并区域处理
MergerRegion(fromSheet, toSheet);
System.Collections.IEnumerator rows = fromSheet.GetRowEnumerator();
while (rows.MoveNext())
{
IRow row = null;
if (wb is HSSFWorkbook)
row = rows.Current as HSSFRow;
else
row = rows.Current as NPOI.XSSF.UserModel.XSSFRow;
IRow newRow = toSheet.CreateRow(row.RowNum);
CopyRow(wb, row, newRow, copyValueFlag);
}
}
/// <summary>
/// 复制行
/// </summary>
/// <param name="wb"></param>
/// <param name="fromRow"></param>
/// <param name="toRow"></param>
/// <param name="copyValueFlag"></param>
public staticvoid CopyRow(IWorkbook wb, IRow fromRow, IRow toRow,bool
copyValueFlag)
{
System.Collections.IEnumerator cells = fromRow.GetEnumerator();//.GetRowEnumerator();
toRow.Height = fromRow.Height;
while (cells.MoveNext())
{
ICell cell = null;
//ICell cell = (wb is HSSFWorkbook) ? cells.Current as HSSFCell : cells.Current as NPOI.XSSF.UserModel.XSSFCell;
if (wb is HSSFWorkbook)
cell = cells.Current as HSSFCell;
else
cell = cells.Current as NPOI.XSSF.UserModel.XSSFCell;
ICell newCell = toRow.CreateCell(cell.ColumnIndex);
CopyCell(wb, cell, newCell, copyValueFlag);
}
}
/// <summary>
/// 复制原有sheet的合并单元格到新创建的sheet
/// </summary>
/// <param name="fromSheet"></param>
/// <param name="toSheet"></param>
public staticvoid MergerRegion(ISheet fromSheet, ISheet toSheet)
{
int sheetMergerCount = fromSheet.NumMergedRegions;
for (int i = 0; i < sheetMergerCount; i++)
{
//Region mergedRegionAt = fromSheet.GetMergedRegion(i); //.MergedRegionAt(i);
//CellRangeAddress[] cra = new CellRangeAddress[1];
//cra[0] = fromSheet.GetMergedRegion(i);
//Region[] rg = Region.ConvertCellRangesToRegions(cra);
toSheet.AddMergedRegion(fromSheet.GetMergedRegion(i));
}
}
/// <summary>
/// 复制单元格
/// </summary>
/// <param name="wb"></param>
/// <param name="srcCell"></param>
/// <param name="distCell"></param>
/// <param name="copyValueFlag"></param>
public staticvoid CopyCell(IWorkbook wb, ICell srcCell, ICell distCell,bool copyValueFlag)
{
ICellStyle newstyle = wb.CreateCellStyle();
CopyCellStyle(wb, srcCell.CellStyle, newstyle);
//样式
distCell.CellStyle = newstyle;
//评论
if (srcCell.CellComment !=
null)
{
distCell.CellComment = srcCell.CellComment;
}
// 不同数据类型处理
CellType srcCellType = srcCell.CellType;
distCell.SetCellType(srcCellType);
if (copyValueFlag)
{
if (srcCellType == CellType.NUMERIC)
{
if (HSSFDateUtil.IsCellDateFormatted(srcCell))
{
distCell.SetCellValue(srcCell.DateCellValue);
}
else
{
distCell.SetCellValue(srcCell.NumericCellValue);
}
}
else if (srcCellType == CellType.STRING)
{
distCell.SetCellValue(srcCell.RichStringCellValue);
}
else if (srcCellType == CellType.BLANK)
{
// nothing21
}
else if (srcCellType == CellType.BOOLEAN)
{
distCell.SetCellValue(srcCell.BooleanCellValue);
}
else if (srcCellType == CellType.ERROR)
{
distCell.SetCellErrorValue(srcCell.ErrorCellValue);
}
else if (srcCellType == CellType.FORMULA)
{
distCell.SetCellFormula(srcCell.CellFormula);
}
else
{ // nothing29
}
}
}
}
相关文章推荐
- NPOI中如何复制Sheet
- 工作总结 1 sql写法 insert into select from 2 vs中 obj文件和bin文件 3 npoi 模板copy CopySheet 最好先全部Copy完后 再根据生成sheet写数据 4 sheet.CopyRow(rowsindex, rowsindex + x); 5 npoi 复制模板如果出现单元格显示问题
- NPOI复制Excel工作簿Sheet以及删除Excel工作簿
- NPOI 将一个sheet指定区域复制到另一个sheet,复制数据,样式和备注
- NPOI跨Sheet复制(含格式、字体、合并区域)
- android 复制信息内容到剪切板 (2011-12-05 09:31:11)转载▼
- C#使用Log4Net记录日志(想直接转载的,但是不支持转到这上面来,复制来的,作者勿怪,学习用)
- ios 中 实现对象复制(转载)
- 点滴积累【other】---win2003 service pack2 IIS 无法复制CONVLOG.EXE CONVLOG.EX_(转载)
- 表的复制[转载]
- 转载:MFC文件/文件夹操作查找、复制、删除
- 如何在Oracle中复制表结构和表数据 【转载】
- 解决使用NPOI在已有Sheet中插入多张图片问题
- (开发笔记)转载:maven如何将项目引用的所有jar包复制到lib文件夹内
- ios 中 实现对象复制(转载)
- redis主从复制(转载)
- 转载:NPOI导出到Excel表格
- 转载:vim的复制,删除,粘贴,块操作以及快速替换功能
- python实现跨excel的工作表sheet之间的复制方法
- [转载]Word——Visio虚线框复制到Word中变实线框问题