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);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM