在读取excel单元格的数据之前,有时候需要判断单元格的数据类型。在spire.xls中有string、number、formula、boolean、error和blank六种数据类型,分别对应字符串型、数值型、公式型、布尔型、错误和空值。这篇文章将介绍如何使用spire.xls获取单元格的数据类型。
c#
using system.drawing;
using spire.xls;
using spire.xls.core.spreadsheet;
namespace getcelltype
{
class program
{
static void main(string[] args)
{
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile("input.xlsx");
//获取第二张工作表
worksheet sheet = workbook.worksheets[1];
//获取指定范围内的单元格的数据类型
foreach (cellrange range in sheet.range["a2:a7"])
{
xlsworksheet.trangevaluetype celltype = sheet.getcelltype(range.row, range.column, false);
sheet[range.row, range.column 1].text = celltype.tostring();
sheet[range.row, range.column 1].style.font.color = color.red;
sheet[range.row, range.column 1].style.font.isbold = true;
}
//保存文档
workbook.savetofile("getcelltype.xlsx", excelversion.version2013);
}
}
}
vb.net
imports system.drawing
imports spire.xls
imports spire.xls.core.spreadsheet
namespace getcelltype
class program
private shared sub main(args as string())
'创建workbook实例
dim workbook as new workbook()
'加载excel文档
workbook.loadfromfile("input.xlsx")
'获取第二张工作表
dim sheet as worksheet = workbook.worksheets(1)
'获取指定范围内的单元格的数据类型
for each range as cellrange in sheet.range("a2:a7")
dim celltype as xlsworksheet.trangevaluetype = sheet.getcelltype(range.row, range.column, false)
sheet(range.row, range.column 1).text = celltype.tostring()
sheet(range.row, range.column 1).style.font.color = color.red
sheet(range.row, range.column 1).style.font.isbold = true
next
'保存文档
workbook.savetofile("getcelltype.xlsx", excelversion.version2013)
end sub
end class
end namespace
结果文档: