Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number Format doesn't work #1078

Closed
goskan93 opened this issue Nov 23, 2018 · 5 comments
Closed

Number Format doesn't work #1078

goskan93 opened this issue Nov 23, 2018 · 5 comments
Labels

Comments

@goskan93
Copy link

I create a Table in Excel using Closed XML

            System.Data.DataTable table = AcompanhamentoDePropostaTable();
            XLWorkbook wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("Acompanhamento - Cultura");
            ws.FirstCell().InsertTable(table, false);

Problem is that I have columns as string with values like $20000 or R$10000 (currency is changing) and I don't know how to set the currency type to these columns and not lose information about currency.

My code for now

            for(int j = 2; j<= lastrow; j++)
            {
                ws.Cell(j, 09).Style.NumberFormat.Format = ws.Cell(j, 9).Value.ToString() != "" ? ws.Cell(j, 9).Value.ToString().Contains("R$") ? "[$R$-pt-BR]#,##0.00" : "[$$-en-US]#,##0.00" : "" ;
            }

But it does not change the format of values in my column.
Any ideas how to do this? And maybe also there exist the way to not iterate row by row?
Thank You in advance.

@vbjay
Copy link
Contributor

vbjay commented Nov 23, 2018 via email

@goskan93
Copy link
Author

goskan93 commented Nov 23, 2018

I dont know if I explained well, but what I want is to now my values look like this R$20000 but like this R$20,000.00, and I don't know where I am making the mistake

@vbjay
Copy link
Contributor

vbjay commented Nov 23, 2018 via email

@igitur igitur added the RTFM label Nov 23, 2018
@goskan93
Copy link
Author

I didn't understand your suggestion, but this is what I made:

o the table I created, I add a column with currencyID, and column with only number, without information about currency, this table I added to an Excel, and later in loop I checked row by row what is currency Id and format the cell

ws.Cell(j, i).Style.NumberFormat.Format = ws.Cell(j, i).Value.ToString() != "" ? Convert.ToInt16(ws.Cell(j, (i-1)).Value) == 2 ? " [$R$-pt-BR]#,##0.00" : "[$$-en-US] #,##0.00" : "";}
where ws.Cell(j, i) is cell with value (number) and ws.Cell(j, (i-1)) is cell with currency id

@vbjay
Copy link
Contributor

vbjay commented Nov 28, 2018

You are making this too hard. You don't need to export the whole table. There is no need to have a column with currency info.

Use linq to create an ienumerable based off the data table that only has the right values for the columns. Export them to the sheet. Then use linq against the table again and get formats from table indexed by row. Set correct formats of cells, they will still be in same order of the table. Loop and set format.

Or delete currency column after export.

@igitur igitur closed this as completed Dec 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants