简介:包含:获取单元格合并信息GetMergedCellAddress、获取引用单元格字符串ConvertAddressToString、获取单元格字符串格式内容CellValueToString

public class ExcelHelper
{
/// <summary>
/// Return cell's CellRangeAddress if that is a merged cell, otherwise return null
/// </summary>
/// <param name="cell">the cell will be checked</param>
/// <returns>the cell's CellRangeAddress</returns>
public static CellRangeAddress GetMergedCellAddress(ICell cell)
{
//not merged cell
if (cell.IsMergedCell == false)
return null; int MergedRegionsNum = cell.Sheet.NumMergedRegions;
for(int index = ; index < MergedRegionsNum; index++)
{
CellRangeAddress address = cell.Sheet.GetMergedRegion(index);
if (address.FirstRow == cell.RowIndex && address.FirstColumn == cell.ColumnIndex)
return address;
} //no match
return null;
} /// <summary>
/// 将Excel行列索引号(从0开始)转换为引用地址,分非R1C1格式(默认)和R1C1格式
/// </summary>
/// <param name="rowIndex">行索引号(从0开始)</param>
/// <param name="columnIndex">列索引号(从0开始)</param>
/// <param name="isR1C1Format">是否R1C1格式(默认为否)</param>
/// <returns>Excel单元格引用地址字符串</returns>
public static string ConvertAddressToString(int rowIndex, int columnIndex, bool isR1C1Format = false)
{
if (rowIndex < || columnIndex < )
throw new IndexOutOfRangeException("行/列索引号不可为负数");
if (isR1C1Format)
//R1C1格式行号在前列号在后 eg:R2C3 第2行第3列
return string.Format("R{0}C{1}", rowIndex + , columnIndex + );
else
{
//非R1C1格式列号在前行号在后 eg:C5 第5行第3列
//convert column num to letter
int num = columnIndex;
string colAddress = string.Empty;
while(num > -)
{
int remainder = num >= ? num % : num;
colAddress = (char)(remainder + ) + colAddress;
num = (num / ) - ;
} return string.Format("{1}{0}", rowIndex + , colAddress);
}
} /// <summary>
/// 获取Excel单元格引用地址,分非R1C1格式(默认)和R1C1格式
/// </summary>
/// <param name="cell">引用单元格</param>
/// <param name="isR1C1Format">是否R1C1格式(默认为否)</param>
/// <returns>Excel单元格引用地址字符串</returns>
public static string ConvertAddressToString(ICell cell, bool isR1C1Format = false)
{
return ConvertAddressToString(cell.RowIndex, cell.ColumnIndex, isR1C1Format);
} /// <summary>
/// 获取Excel单元格值(字符串格式)
/// </summary>
/// <param name="cell">单元格</param>
/// <returns>单元格字符串格式值内容</returns>
public static string CellValueToString(ICell cell)
{
if (cell == null)
return null;
switch (cell.CellType)
{
case CellType.Blank:
return string.Empty;
case CellType.String:
return cell.StringCellValue;
case CellType.Boolean:
return cell.BooleanCellValue.ToString();
case CellType.Error:
return cell.ErrorCellValue.ToString();
case CellType.Formula:
return cell.CellFormula;
case CellType.Numeric:
case CellType.Unknown:
default:
return cell.ToString();
}
}
}

最新文章

  1. hive建表与数据的导入导出
  2. .NET软件开发与常用工具清单(转)
  3. HDOJ 3709 Balanced Number
  4. &lt;转&gt;ORA-12154: TNS: 无法解析指定的连接标识符
  5. 加速你的py.test, pytest-dist
  6. 30.赋值运算符重载函数[Assign copy constructor]
  7. &quot;is not on any development teams &quot; Xcode的账号错误问题
  8. SQL关键字转换大写核心算法实现
  9. AngularJS进阶(三十九)基于项目实战解析ng启动加载过程
  10. PA模块常用表
  11. 从壹开始微服务 [ DDD ] 之五 ║聚合:实体与值对象 (上)
  12. eclipse导入maven项目,但无法编译的问题
  13. java笔记 -- 输入输出
  14. 作业---修改haproxy配置文件
  15. JMeter学习non-gui模式运行
  16. PHP: Browser, Operating System (OS), Device, and Language Detect
  17. PHP文本操作
  18. js实现点击评论进行显示回复框
  19. bzr登陆加密
  20. js实现密码强度

热门文章

  1. Atitit.软件开发的终于的设计&amp;#160;dsl化,ast化(建立ast,&amp;#160;解析运行ast)
  2. ios svn repository
  3. 【Android实战】Socket消息通信
  4. vue --- 脚手架初始化项目中配置文件webpack.base.conf.js代码含义
  5. ACM的算法分类 2015-04-16 14:25 22人阅读 评论(0) 收藏
  6. Python(十) 函数式编程: 匿名函数、高阶函数、装饰器
  7. Oracle Database Sample Schemas
  8. c#+windows api SetWindowsHookEx 全局钩子 demo 下载
  9. Python标准库:内置函数ascii(object)
  10. Android自己定义效果——随机抽奖