62,072
社区成员
发帖
与我相关
我的任务
分享
protected void Button2_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~\\Public\\test1.xls");
if (System.IO.File.Exists(path)) //判断文件是否存在
{
FileStream fs = File.OpenRead(path);
IWorkbook hssfworkbook;
try
{
hssfworkbook = new HSSFWorkbook(fs);
ISheet sheet = hssfworkbook.GetSheetAt(0);//读取第一个sheet
IEnumerator rows = sheet.GetRowEnumerator();
//得到Excel工作表的行
IRow headerRow = sheet.GetRow(0);
ICell cell = headerRow.GetCell(0);
String val = cell.ToString();
}
catch
{
}
fs.Close();
fs.Dispose();
}
}
string formatString = cell.CellStyle.GetDataFormatString();
string value = cell.NumericCellValue.ToString(formatString);
参考地址:
https://stackoverflow.com/questions/28714740/using-npoi-how-do-i-return-a-cell-value-as-it-has-been-formatted-by-excelprotected void Button2_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~\\Public\\test1.xls");
if (System.IO.File.Exists(path)) //判断文件是否存在
{
FileStream fs = File.OpenRead(path);
IWorkbook hssfworkbook;
try
{
hssfworkbook = new HSSFWorkbook(fs);
ISheet sheet = hssfworkbook.GetSheetAt(0);//读取第一个sheet
IEnumerator rows = sheet.GetRowEnumerator();
//得到Excel工作表的行
IRow headerRow = sheet.GetRow(0);
ICell cell = headerRow.GetCell(0);
HSSFCellStyle cellStyle = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
HSSFDataFormat format = (HSSFDataFormat)hssfworkbook.CreateDataFormat();
cellStyle.DataFormat = format.GetFormat("###º##.###'"); //###"º"##"."###' Hºmm.ss'
cell.CellStyle = cellStyle;
String val = cell.ToString();
}
catch
{
}
fs.Close();
fs.Dispose();
}
}
哥们,试了一下,不行哦,你有尝试过吗???