Excel c# convert cell to percentage

岁酱吖の 提交于 2020-07-03 06:50:20

问题


I need to convert a cell with a double to a precentage. I used a macro in excel and it says:

Range("B5").Select
Selection.Style = "Percent"

When I do this in c#, it doesn't work:

Excel.Range procentRange = xlWorksheet.get_Range("A1","A1");
procentRange.Style = "Percent";

Anybody knows how to do this?


回答1:


I've found the anwser with help of JN Web

Excel.Range procentRange = xlWorksheet.get_Range("A1","A1");    
procentRange.NumberFormat = "###,##%";

So first you need a range, then set the decimals and add "%" -> automatically a 100 time multiplication




回答2:


Try using Excel.Range.NumberFormat instead of Excel.Range.Style




回答3:


Try using Excel.Range.Style.NumberFormat.Format="#,###.00%"




回答4:


Try this procentRange.NumberFormatLocal = "0.00%"



来源:https://stackoverflow.com/questions/3005015/excel-c-sharp-convert-cell-to-percentage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!