C#使用npoi 导出excel如何设置单元格为成数字格式

mryouhao 2014-11-22 09:06:56
如题!如何设置导出到EXCEL的格式为数字格式? 我设置了不起作用,请教各位,谢谢!
HSSFCellStyle cellStyle = workbook.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");

代码如下:
HSSFWorkbook workbook = new HSSFWorkbook();
//通过工作本创建一个“页”
HSSFSheet sheet = workbook.CreateSheet("第一页");

HSSFCellStyle cellStyle = workbook.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");

HSSFRow rowhead = sheet.CreateRow(0);
rowhead.CreateCell(0).SetCellValue("流水号");
rowhead.CreateCell(1).SetCellValue("工令单号");
rowhead.CreateCell(2).SetCellValue("产品编码");
rowhead.CreateCell(3).SetCellValue("品名");
rowhead.CreateCell(4).SetCellValue("规格型号");
rowhead.CreateCell(5).SetCellValue("工序序号");
rowhead.CreateCell(6).SetCellValue("工序名称");
rowhead.CreateCell(7).SetCellValue("在制量");
rowhead.CreateCell(8).SetCellValue("生产量");
rowhead.CreateCell(9).SetCellValue("移入量");
rowhead.CreateCell(10).SetCellValue("完工量");
rowhead.CreateCell(12).SetCellValue("反移出量");
rowhead.CreateCell(12).SetCellValue("报废数量");
rowhead.CreateCell(13).SetCellValue("盘盈亏数量");
rowhead.CreateCell(14).SetCellValue("返工在制量");
rowhead.CreateCell(15).SetCellValue("返工完工量");
rowhead.CreateCell(16).SetCellValue("返工反移出量");
rowhead.CreateCell(17).SetCellValue("返工返工数量");
rowhead.CreateCell(18).SetCellValue("返工报废量");
rowhead.CreateCell(19).SetCellValue("返工盘盈亏数量");
rowhead.CreateCell(20).SetCellValue("工序排序号");
rowhead.CreateCell(21).SetCellValue("工序排序名");
rowhead.CreateCell(22).SetCellValue("工序变更版次");
rowhead.CreateCell(23).SetCellValue("是否外发");



//遍历面板,遍历行
for (int rowindex = 0; rowindex < this.dgvMpsOnline.Rows.Count; rowindex++)
{
HSSFRow row = sheet.CreateRow(rowindex + 1);
//遍历面上遍历到的行的列
for (int celIndex = 0; celIndex < 24; celIndex++)
{
row.CreateCell(celIndex).SetCellValue(this.dgvMpsOnline.Rows[rowindex].Cells[celIndex].Value.ToString());
}
}

using (SaveFileDialog sf = new SaveFileDialog())
{
sf.Filter = "表格|*.xls";
sf.AddExtension = true;
if (sf.ShowDialog() == DialogResult.OK)
{
//创建一个文档流对象
using (FileStream fs = new FileStream(sf.FileName, FileMode.Create))
{
//将内存里面的文档对象,写入到一个文档流中
workbook.Write(fs);
}
}
}
...全文
19656 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
kzbpp 2018-10-31
  • 打赏
  • 举报
回复 2
1、设置单元格格式
cell.SetCellType(CellType.Numeric);
2、存入的数据要转成double
ICell cell2 = row.CreateCell(1, CellType.Numeric);
cell2.SetCellValue(Double.Parse(dtNew.Rows[i]["Price"].ToString()));//Price

亲测可以。
qq_36803913 2018-10-22
  • 打赏
  • 举报
回复
你这个问题解决了没?我也遇到这个问题了,求解
欣宇_记住我 2016-12-17
  • 打赏
  • 举报
回复

/// <summary>
///将数据转换为Int类型
/// </summary>
public static int ToInt(object data, int defaultValue = default(int))
{
	int returnValue = defaultValue;

	if (ValidationHelper.IsNullOrEmpty(data) || !int.TryParse(data.ToString(), out returnValue))
		returnValue = defaultValue;
	return returnValue;
}


/// <summary>
/// 判断字符串是否是int/double
/// </summary>
/// <param name="strNumber">需要判断的字符串</param>
/// <returns></returns>
public static bool IsDouble(string strNumber)
{
	Regex objNotNumberPattern = new Regex("[^0-9.-]");
	Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
	Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
	const string strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
	const string strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
	Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
	return !objNotNumberPattern.IsMatch(strNumber) &&
		   !objTwoDotPattern.IsMatch(strNumber) &&
		   !objTwoMinusPattern.IsMatch(strNumber) &&
		   objNumberPattern.IsMatch(strNumber);
}
m0_46979611 2021-07-18
  • 举报
回复
@欣宇_记住我 ValidationHelper.IsNullOrEmpty,为什么说这个不存在?是要引用什么吗?
欣宇_记住我 2016-12-17
  • 打赏
  • 举报
回复

HSSFWorkbook workbookExcel = new HSSFWorkbook();
ISheet sheetExcel = workbookExcel.CreateSheet("Sheet1");
IRow rowExcel = sheetExcel.CreateRow(excelRow);
ICell cellExcel = rowExcel.CreateCell(excelCell);
if (ConvertHelper.ToInt(row[t.DataIndex].ToString(), int.MinValue) != int.MinValue)
{
	if (ConvertHelper.ToInt(row[t.DataIndex].ToString(), int.MinValue) <= 99999999999)
		cellExcel.SetCellValue(int.Parse(row[t.DataIndex].ToString()));
	else //Excel数字大于11个9显示NE+总位数
		cellExcel.SetCellValue(row[t.DataIndex].ToString());
}
else if (ConvertHelper.IsDouble(row[t.DataIndex].ToString()))
{
	cellExcel.SetCellValue(double.Parse(row[t.DataIndex].ToString()));
}
//else if(日期等其他数据类型){...}
else
{
	cellExcel.SetCellValue(row[t.DataIndex].ToString());
}
熙风 2014-11-25
  • 打赏
  • 举报
回复
        double doubV = 0;
                            double.TryParse(drValue, out doubV);
                            dataRow.CreateCell(j).SetCellValue(doubV);
Anymore 2014-11-25
  • 打赏
  • 举报
回复
http://www.cnblogs.com/xwgli/archive/2013/07/08/3178153.html 单元格数字格式的问题 NPOI向Excel文件中插入数值时,可能会出现数字当作文本的情况(即左上角有个绿色三角),这样单元格的值就无法参与运算。这是因为在SetCellValue设置单元格值的时候使用了字符串进行赋值,默认被转换成了字符型。如果需要纯数字型的,请向SetCellValue中设置数字型变量。 sheet.GetRow(2).CreateCell(2).SetCellValue(123);
mryouhao 2014-11-25
  • 打赏
  • 举报
回复
网上查了一下,也有人遇到这种情况,不过好像都没有准确的解决方案。
luhr 2014-11-25
  • 打赏
  • 举报
回复
我觉得判断一下能不能转数字格式的,能就转,不能就字符 这样比较好
周美文 2014-11-25
  • 打赏
  • 举报
回复

cell.SetCellType(CellType.Numeric);
这两种都行的
周美文 2014-11-25
  • 打赏
  • 举报
回复

  ICell cell = row.CreateCell(0, CellType.Numeric);
异常异长 2014-11-25
  • 打赏
  • 举报
回复
这种如果数据有规律的话 建议还是 读取一个带格式化的模板excel 比较方便
_小黑_ 2014-11-25
  • 打赏
  • 举报
回复

switch (dtSource.Columns[oldColumnNames[i]].DataType.ToString())
                    {
                        case "System.String"://字符串类型   
                            newCell.SetCellValue(drValue);
                            break;
                        case "System.DateTime"://日期类型   
                            DateTime dateV;
                            DateTime.TryParse(drValue, out dateV);
                            newCell.SetCellValue(dateV);

                            newCell.CellStyle = dateStyle;//格式化显示   
                            break;
                        case "System.Boolean"://布尔型   
                            bool boolV = false;
                            bool.TryParse(drValue, out boolV);
                            newCell.SetCellValue(boolV);
                            break;
                        case "System.Int16"://整型   
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Byte":
                            int intV = 0;
                            int.TryParse(drValue, out intV);
                            newCell.SetCellValue(intV);
                            break;
                        case "System.Decimal"://浮点型   
                        case "System.Double":
                            double doubV = 0;
                            double.TryParse(drValue, out doubV);
                            newCell.SetCellValue(doubV);
                            break;
                        case "System.DBNull"://空值处理   
                            newCell.SetCellValue("");
                            break;
                        default:
                            newCell.SetCellValue("");
                            break;
                    }
mryouhao 2014-11-22
  • 打赏
  • 举报
回复
引用 1 楼 wind_cloud2011 的回复:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00"); 再加句cell.CellStyle=cellStyle; 试试 http://lennon.wang.blog.163.com/blog/static/213560620091116634569/
试过也不行!
wind_cloud2011 2014-11-22
  • 打赏
  • 举报
回复
HSSFWorkbook workbook = new HSSFWorkbook(); //通过工作本创建一个“页” ISheet sheet = workbook.CreateSheet("第一页"); ICellStyle style11 = wk.CreateCellStyle();              style11.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00"); 
wind_cloud2011 2014-11-22
  • 打赏
  • 举报
回复
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00"); 再加句cell.CellStyle=cellStyle; 试试 http://lennon.wang.blog.163.com/blog/static/213560620091116634569/
mryouhao 2014-11-22
  • 打赏
  • 举报
回复
引用 3 楼 wind_cloud2011 的回复:

看这个例子
//创建工作薄
HSSFWorkbook wk = new HSSFWorkbook();
//创建一个名称为mySheet的表
ISheet tb = wk.CreateSheet("mySheet");
//创建一行,此行为第二行
IRow row = tb.CreateRow(1);
ICellStyle style11 = wk.CreateCellStyle();
style11.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
for (int i = 0; i < 20; i++)
{
ICell cell = row.CreateCell(i); //在第二行中创建单元格
cell.SetCellValue(i);//循环往第二行的单元格中添加数据
cell.CellStyle = style11; //要加上这行,

}
using (FileStream fs = File.OpenWrite(@"c:/myxls.xls")) //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
{
wk.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
MessageBox.Show("提示:创建成功!");
}



非常感谢你的回复,但是导出的EXCEL还是文本格式的。
感觉这句代码没起作用: style11.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
wind_cloud2011 2014-11-22
  • 打赏
  • 举报
回复

看这个例子
 //创建工作薄
              HSSFWorkbook wk = new HSSFWorkbook();
              //创建一个名称为mySheet的表
              ISheet tb = wk.CreateSheet("mySheet");
              //创建一行,此行为第二行
              IRow row = tb.CreateRow(1);
              ICellStyle style11 = wk.CreateCellStyle();
              style11.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00"); 
             for (int i = 0; i < 20; i++)   
             {
                 ICell cell = row.CreateCell(i);  //在第二行中创建单元格
                 cell.SetCellValue(i);//循环往第二行的单元格中添加数据
                 cell.CellStyle = style11;  //要加上这行,
                
             }
             using (FileStream fs = File.OpenWrite(@"c:/myxls.xls")) //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
             {
                 wk.Write(fs);   //向打开的这个xls文件中写入mySheet表并保存。
                 MessageBox.Show("提示:创建成功!");
             }

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧