运用%数字格式为使用单元格的值的OpenXML [英] Applying % number format to a cell value using OpenXMl
问题描述
我想用应用%数字格式开放的XML C#
我有十进制值3.6我要显示在Excel中号3.6%
我如何实现这一目标。
请让我知道。
WorkbookStylesPart SP = workbookPart.AddNewPart< WorkbookStylesPart>();
创建一个样式表,
sp.Stylesheet =新样式表();
创建numberingformat,
sp.Stylesheet.NumberingFormats =新NumberingFormats();
//#,##%也是Excel的风格指数1
结果
NumberingFormat nf2decimal =新NumberingFormat();
nf2decimal.NumberFormatId = UInt32Value.FromUInt32(3453);
nf2decimal.Format code = StringValue.FromString(0.0%);
sp.Stylesheet.NumberingFormat.Append(nf2decimal);
创建一个单元格的格式和应用编号格式ID
cellFormat =新CellFormat();
cellFormat.FontId = 0;
cellFormat.FillId = 0;
cellFormat.BorderId = 0;
cellFormat.FormatId = 0;
cellFormat.NumberFormatId = nf2decimal.NumberFormatId;
cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(真);
cellFormat.ApplyFont = TRUE;//添加单元格的格式为标题行的单元格
sp.Stylesheet.CellFormats.AppendChild< CellFormat>(cellFormat);
//更新字体计数
sp.Stylesheet.CellFormats.Count = UInt32Value.FromUInt32((UINT)sp.Stylesheet.CellFormats.ChildElements.Count);
//将更改保存到样式表的一部分
sp.Stylesheet.Save();
当你的价值附加到单元格有以下中心code hereonversion并应用风格指数
在我的情况我有三个风格指数因此3个是我个风格指数即2,因为索引从0开始。
字符串VAL = Convert.ToString(Convert.ToDecimal(值)/ 100);
电池单元=新Cell();
cell.DataType =新EnumValue< CellValues>(CellValues.Number);
cell.CellValue =新CellValue(VAL);
cell.StyleIndex = 2;
row.Append(细胞);
I want to apply the % number format using open XML C#
I have decimal value 3.6 i want to display that number in excel as 3.6% how do i achieve this. Please let me know.
WorkbookStylesPart sp = workbookPart.AddNewPart<WorkbookStylesPart>();
Create a stylesheet,
sp.Stylesheet = new Stylesheet();
Create a numberingformat,
sp.Stylesheet.NumberingFormats = new NumberingFormats();
// #.##% is also Excel style index 1
NumberingFormat nf2decimal = new NumberingFormat();
nf2decimal.NumberFormatId = UInt32Value.FromUInt32(3453);
nf2decimal.FormatCode = StringValue.FromString("0.0%");
sp.Stylesheet.NumberingFormat.Append(nf2decimal);
Create a cell format and apply the numbering format id
cellFormat = new CellFormat();
cellFormat.FontId = 0;
cellFormat.FillId = 0;
cellFormat.BorderId = 0;
cellFormat.FormatId = 0;
cellFormat.NumberFormatId = nf2decimal.NumberFormatId;
cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true);
cellFormat.ApplyFont = true;
//append cell format for cells of header row
sp.Stylesheet.CellFormats.AppendChild<CellFormat>(cellFormat);
//update font count
sp.Stylesheet.CellFormats.Count = UInt32Value.FromUInt32((uint)sp.Stylesheet.CellFormats.ChildElements.Count);
//save the changes to the style sheet part
sp.Stylesheet.Save();
and when you append the value to the cell have the following center code hereonversion and apply the style index in my case i had three style index hence the 3 one was my percentage style index i.e 2 since the indexes start from 0
string val = Convert.ToString(Convert.ToDecimal(value)/100);
Cell cell = new Cell();
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
cell.CellValue = new CellValue(val);
cell.StyleIndex = 2;
row.Append(cell);
这篇关于运用%数字格式为使用单元格的值的OpenXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!