使用NPOI时写的几个辅助方法
简介:包含:获取单元格合并信息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();
}
}
}
使用NPOI时写的几个辅助方法的更多相关文章
- 分享一个异步任务在遇到IO异常时支持递归回调的辅助方法
public void TryAsyncActionRecursively<TAsyncResult>( string asyncActionName, Func<Task<T ...
- 004.MVC视图、辅助方法
一.视图基础- 视图定义: 用户界面,是显示应用程序用户界面(UI)组件 Web应用程序:页面 作用: 1.输出/显示模型数据 2.出入提交 视图建议在View文件夹位置存储视图 视图引擎(了解):本 ...
- 32-HTML辅助方法
顾名思义,HTML辅助方法(HTML Helper)就是用来辅助产生HTML之用,在开发View的时候一定会面对许多HTML标签,处理这些HTML的工作非常繁琐,为了降低View的复杂度,可以使用HT ...
- HTML辅助方法
顾名思义,HTML辅助方法(HTML Helper)就是用来辅助产生HTML之用,在开发View的时候一定会面对许多HTML标签,处理这些HTML的工作非常繁琐,为了降低View的复杂度,可以使用HT ...
- MVC 之HTML辅助方法
顾名思义,HTML辅助方法(HTML Helper)就是用来辅助产生HTML之用,在开发View的时候一定会面对许多HTML标签,处理这些HTML的工作非常繁琐,为了降低View的复杂度,可以使用HT ...
- MVC HTML辅助方法
HTML辅助方法(HTML Helper)用来辅助产生HTML,在开发View的时候会面对许多HTML标签,处理这些HTML标签非常繁琐,为了降低View的复杂度,可以使用HTML辅助方法帮助你产生一 ...
- 【未完待续】MVC 之HTML辅助方法
顾名思义,HTML辅助方法(HTML Helper)就是用来辅助产生HTML之用, 在开发View的时候一定会面对许多HTML标签,处理这些HTML的工作非常繁琐,为了降低View的复杂度,可以使用H ...
- Livid : 在 26 岁时写给 18 岁的自己
转载自: https://livid.v2ex.com/essays/2012/01/24/a-letter-from-26-to-18.html 在 26 岁时写给 18 岁的自己 Jan 24, ...
- MVC学习系列4--@helper辅助方法和用户自定义HTML方法
在HTML Helper,帮助类的帮助下,我们可以动态的创建HTML控件.HTML帮助类是在视图中,用来呈现HTML内容的.HTML帮助类是一个方法,它返回的是string类型的值. HTML帮助类, ...
随机推荐
- MySQL auttoReconnect
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from ...
- android动画-拖动
先上图看效果 实质上说是动画有点不妥,确切的说应该是手势的处理,废话不多说看代码 SimpleDragSample.java public class SimpleDragSample extends ...
- 在Fedora18上配置个人的Hadoop开发环境
在Fedora18上配置个人的Hadoop开发环境 1. 背景 文章中讲述了类似于"personalcondor"的一种"personal hadoop" ...
- vue2.0-elementUI
main.js import Vue from 'vue' import App from './App.vue' import ElementUI from 'element-ui' import ...
- mysql中group by和order by混用 结果不是理想结果
在使用mysql排序的时候会想到按照降序分组来获得一组数据,而使用order by往往得到的不是理想中的结果,那么怎么才能使用group by 和order by得到理想中的数据结果呢? 例如 有一个 ...
- php将数组或字符串写入文件
//将数组保存在文件里 function export_to_file($file, $variable) { $fopen = fopen($file, 'wb'); if (!$fopen) { ...
- Boolean operations between triangle meshes
Boolean operations between triangle meshes eryar@163.com Abstract. Boolean operations is one of basi ...
- codeforce 571 B Minimization
题意:给出一个序列,经过合适的排序后.使得最小. 做法:将a升序排序后,dp[i][j]:选择i个数量为n/k的集合,选择j个数量为n/k+1的集合的最小值. 举个样例, a={1,2,3,4,5,6 ...
- 第二天,导出文件sql,查询,视图view,聚合函数,反模式,字符串处理函数
//把数据库导出到脚本文件mysqldump -uroot -p1234 --databases abc > d:/a/abc.sql CREATE TABLE stud( id INT PRI ...
- EC2 开启 IPV6 访问 和 禁止重启后自动分配IP地址
EC2 开启 IPV6 访问 和 禁止重启后自动分配IP地址进入 VPC 控制台,对当前 VPC 添加 IPV6 CIDR 块对该 VPC 的路由表进行修改,添加其它路由,第一个空填::/0,第二个空 ...