NPOI 复制Sheet(转载)

以下内容由chocolateboy兄所写,在此仅作为转帖

原文载于:http://blog.csdn.net/chocolateboy/article/details/9705425

 

NPOI已经发布到了2.0的版本,可喜的是该版本支持到了Excel2007,但缺憾的是没有复制SHEET表的功能,只有一个CloneSheet方法,确实没明白怎么使用。所以在网上搜索了一下,只找一个JAVA的版本,好像也是自己写的代码实现的。于是拿过来翻译了一下,复制SHEET后的格式基本没变,但单元格的宽度没有实现,ICell接口没有类似于Width的属性,只好作罢。

哪位如果知道CloneSheet方法的使用,恳请留言,免去不必要的操作和代码。

  1. public class SheetClone
  2. {
  3. public staticvoid CopyCellStyle(IWorkbook wb, ICellStyle fromStyle, ICellStyle toStyle)
  4. {
  5. toStyle.Alignment = fromStyle.Alignment;
  6. //边框和边框颜色
  7. toStyle.BorderBottom = fromStyle.BorderBottom;
  8. toStyle.BorderLeft = fromStyle.BorderLeft;
  9. toStyle.BorderRight = fromStyle.BorderRight;
  10. toStyle.BorderTop = fromStyle.BorderTop;
  11. toStyle.TopBorderColor = fromStyle.TopBorderColor;
  12. toStyle.BottomBorderColor = fromStyle.BottomBorderColor;
  13. toStyle.RightBorderColor = fromStyle.RightBorderColor;
  14. toStyle.LeftBorderColor = fromStyle.LeftBorderColor;
  15. //背景和前景
  16. toStyle.FillBackgroundColor = fromStyle.FillBackgroundColor;
  17. toStyle.FillForegroundColor = fromStyle.FillForegroundColor;
  18. toStyle.DataFormat = fromStyle.DataFormat;
  19. toStyle.FillPattern = fromStyle.FillPattern;
  20. //toStyle.Hidden=fromStyle.Hidden;
  21. toStyle.IsHidden = fromStyle.IsHidden;
  22. toStyle.Indention = fromStyle.Indention;//首行缩进
  23. toStyle.IsLocked = fromStyle.IsLocked;
  24. toStyle.Rotation = fromStyle.Rotation;//旋转
  25. toStyle.VerticalAlignment = fromStyle.VerticalAlignment;
  26. toStyle.WrapText = fromStyle.WrapText;
  27. toStyle.SetFont(fromStyle.GetFont(wb));
  28. }
  29. /// <summary>
  30. /// 复制表
  31. /// </summary>
  32. /// <param name="wb"></param>
  33. /// <param name="fromSheet"></param>
  34. /// <param name="toSheet"></param>
  35. /// <param name="copyValueFlag"></param>
  36. public staticvoid CopySheet(IWorkbook wb, ISheet fromSheet, ISheet toSheet,bool copyValueFlag)
  37. {
  38. //合并区域处理
  39. MergerRegion(fromSheet, toSheet);
  40. System.Collections.IEnumerator rows = fromSheet.GetRowEnumerator();
  41. while (rows.MoveNext())
  42. {
  43. IRow row = null;
  44. if (wb is HSSFWorkbook)
  45. row = rows.Current as HSSFRow;
  46. else
  47. row = rows.Current as NPOI.XSSF.UserModel.XSSFRow;
  48. IRow newRow = toSheet.CreateRow(row.RowNum);
  49. CopyRow(wb, row, newRow, copyValueFlag);
  50. }
  51. }
  52. /// <summary>
  53. /// 复制行
  54. /// </summary>
  55. /// <param name="wb"></param>
  56. /// <param name="fromRow"></param>
  57. /// <param name="toRow"></param>
  58. /// <param name="copyValueFlag"></param>
  59. public staticvoid CopyRow(IWorkbook wb, IRow fromRow, IRow toRow,bool copyValueFlag)
  60. {
  61. System.Collections.IEnumerator cells = fromRow.GetEnumerator();//.GetRowEnumerator();
  62. toRow.Height = fromRow.Height;
  63. while (cells.MoveNext())
  64. {
  65. ICell cell = null;
  66. //ICell cell = (wb is HSSFWorkbook) ? cells.Current as HSSFCell : cells.Current as NPOI.XSSF.UserModel.XSSFCell;
  67. if (wb is HSSFWorkbook)
  68. cell = cells.Current as HSSFCell;
  69. else
  70. cell = cells.Current as NPOI.XSSF.UserModel.XSSFCell;
  71. ICell newCell = toRow.CreateCell(cell.ColumnIndex);
  72. CopyCell(wb, cell, newCell, copyValueFlag);
  73. }
  74. }
  75. /// <summary>
  76. /// 复制原有sheet的合并单元格到新创建的sheet
  77. /// </summary>
  78. /// <param name="fromSheet"></param>
  79. /// <param name="toSheet"></param>
  80. public staticvoid MergerRegion(ISheet fromSheet, ISheet toSheet)
  81. {
  82. int sheetMergerCount = fromSheet.NumMergedRegions;
  83. for (int i = 0; i < sheetMergerCount; i++)
  84. {
  85. //Region mergedRegionAt = fromSheet.GetMergedRegion(i); //.MergedRegionAt(i);
  86. //CellRangeAddress[] cra = new CellRangeAddress[1];
  87. //cra[0] = fromSheet.GetMergedRegion(i);
  88. //Region[] rg = Region.ConvertCellRangesToRegions(cra);
  89. toSheet.AddMergedRegion(fromSheet.GetMergedRegion(i));
  90. }
  91. }
  92. /// <summary>
  93. /// 复制单元格
  94. /// </summary>
  95. /// <param name="wb"></param>
  96. /// <param name="srcCell"></param>
  97. /// <param name="distCell"></param>
  98. /// <param name="copyValueFlag"></param>
  99. public staticvoid CopyCell(IWorkbook wb, ICell srcCell, ICell distCell,bool copyValueFlag)
  100. {
  101. ICellStyle newstyle = wb.CreateCellStyle();
  102. CopyCellStyle(wb, srcCell.CellStyle, newstyle);
  103. //样式
  104. distCell.CellStyle = newstyle;
  105. //评论
  106. if (srcCell.CellComment != null)
  107. {
  108. distCell.CellComment = srcCell.CellComment;
  109. }
  110. // 不同数据类型处理
  111. CellType srcCellType = srcCell.CellType;
  112. distCell.SetCellType(srcCellType);
  113. if (copyValueFlag)
  114. {
  115. if (srcCellType == CellType.NUMERIC)
  116. {
  117. if (HSSFDateUtil.IsCellDateFormatted(srcCell))
  118. {
  119. distCell.SetCellValue(srcCell.DateCellValue);
  120. }
  121. else
  122. {
  123. distCell.SetCellValue(srcCell.NumericCellValue);
  124. }
  125. }
  126. else if (srcCellType == CellType.STRING)
  127. {
  128. distCell.SetCellValue(srcCell.RichStringCellValue);
  129. }
  130. else if (srcCellType == CellType.BLANK)
  131. {
  132. // nothing21
  133. }
  134. else if (srcCellType == CellType.BOOLEAN)
  135. {
  136. distCell.SetCellValue(srcCell.BooleanCellValue);
  137. }
  138. else if (srcCellType == CellType.ERROR)
  139. {
  140. distCell.SetCellErrorValue(srcCell.ErrorCellValue);
  141. }
  142. else if (srcCellType == CellType.FORMULA)
  143. {
  144. distCell.SetCellFormula(srcCell.CellFormula);
  145. }
  146. else
  147. { // nothing29
  148. }
  149. }
  150. }
  151. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值