首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NPOI数字格式

NPOI数字格式
EN

Stack Overflow用户
提问于 2021-08-23 20:30:07
回答 1查看 702关注 0票数 2

我正在使用NPOI库创建excel文件,我在格式化价格方面有问题。

代码语言:javascript
复制
                ISheet excelSheet = workbook.CreateSheet(sheetName);

                ICellStyle codeCellStyle = workbook.CreateCellStyle();
                ICellStyle priceCellStyle = workbook.CreateCellStyle();
                ICellStyle availabilityStyle = workbook.CreateCellStyle();

                excelSheet.SetColumnWidth(0, 10 * 256);
                excelSheet.SetColumnWidth(1, 12 * 256);
                excelSheet.SetColumnWidth(2, 15 * 256);

                List<string> columns = new List<string>() { "Code", "Price", "Availability" };
                IRow row = excelSheet.CreateRow(0);

                foreach (var columnData in columns.Select((v, i) => new { Column = v, Index = i }).ToList())
                {
                    row.CreateCell(columnData.Index).SetCellValue(columnData.Column);
                }

                codeCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("@");
                priceCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("#,##0.00");
                availabilityStyle.DataFormat = workbook.CreateDataFormat().GetFormat("@");

                int rowIndex = 1;

                foreach (var item in items)
                {
                    row = excelSheet.CreateRow(rowIndex);
                    row.CreateCell(0).SetCellValue(item.Code);
                    row.Cells[0].CellStyle = codeCellStyle;

                    if (item.HasData)
                    {
                        if (item.Price == "ON DEMAND")
                        {
                            row.CreateCell(1).SetCellValue(item.Price);
                        }
                        else
                        {
                            ICell priceCell = row.CreateCell(1);

                            row.Cells[1].SetCellType(CellType.Numeric);
                            row.Cells[1].CellStyle = priceCellStyle;

                            if (percentage != 0)
                                priceCell.SetCellValue(double.Parse(item.Price) + ((double)percentage / (double)100) * double.Parse(item.Price));
                            else
                                priceCell.SetCellValue(double.Parse(item.Price));
                        }
                        row.CreateCell(2).SetCellValue(item.Availability ? "True" : "False");
                    }
                    else
                    {
                        row.CreateCell(1).SetCellValue("");
                        row.CreateCell(2).SetCellValue("");
                    }
                    rowIndex++;
                }

当我有标价为"422,26“的项目时,它会产生价格值为42226,00的excel文件。当我有价格"380,00","4730,00“时,它可以正常工作,但是当我在字符串数中有小数部分时,就会出现问题。

我尝试了来自堆栈溢出的其他建议,但我没有运气,没有什么对我的例子有效。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-17 17:01:15

经过努力,这种格式解决了我的问题

"# #0.00"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68898693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档