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