2,C#,NPOI2.2.1,.NET 4.0 获取单元格公式值,设置单元格的格式

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.SS.Util;

string tpath = “d:\\123.xlsx”;

FileStream fs = new FileStream(tpath, FileMode.Open, FileAccess.Read);

if (tpath.IndexOf(".xlsx") > 0) // 2007版本

   workbook = new XSSFWorkbook(fs);

else if (tpath.IndexOf(".xls") > 0) // 2003版本

   workbook = new HSSFWorkbook(fs);

sheet = workbook.GetSheet(sheetName);

IRow row = sheet.GetRow(0);

//获取单元格的公式值

String temp;

if (row.GetCell(0).CellType == CellType.Formula)

temp = row.GetCell(0).NumericCellValue.ToString();

else

temp = row.GetCell(0).ToString();

 

//设置单元格格式为0.00,

IDataFormat dataformat = workbook.CreateDataFormat();

 ICellStyle style0 = workbook.CreateCellStyle();

style0.DataFormat = dataformat.GetFormat("0.00");

row.CreateCell(0).CellStyle = style0;

row.GetCell(0).SetCellValue(1);

posted on 2019-01-15 11:58  helloe  阅读(1919)  评论(0编辑  收藏  举报

导航