Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Excel cells (currency)

I developed an Add-In for Excel so you can insert some numbers from a MySQL database into specific cells. Now I tried to format these cells to currency and I have two problems with that. 1. When using a formula on formatted cells, the sum for example is displayed like that: "353,2574€". What do I have to do to display it in an appropriate way? 2. Some cells are empty but have to be formatted in currency as well. When using the same format I used for the sum formula and type something in, there's only the number displayed. No "€", nothing. What is that? I specified a Excel.Range and used this to format the range

sum.NumberFormat = "#.## €";

But I also tried

sum.NumberFormat = "0,00 €";
sum.NumberFormat = "#.##0,00 €";

Any idea someone?

like image 802
soundslogic Avatar asked Jan 15 '13 12:01

soundslogic


People also ask

How do you Format all cells with dollar amounts as Currency?

Step 1: Select the range of cells that you want to Format. Step 2: After you select the required data, Go to Home tab >> Number group >> Click on the down arrow in the Number Format box. Step 3: Now, Click on the Currency option from the given list.

How do I Format text to Currency in Excel?

Generally, you should use the Format Cells dialog (Ctrl+1) or Home > Number > Accounting Number Format option to apply a currency formatting to a cell.

How do I stop Excel from formatting my Currency?

To get rid of the dollar sign, first select the cells with dollar signs you want to remove. Then, in the Toolbar, click on Format > Number > Number. As a result, the dollar sign is deleted from the selected cells.


1 Answers

This one works for me. I have excel test app that formats the currency into 2 decimal places with comma as thousand separator. Below is the Console Application that writes data on Excel File.

Make sure you have referenced Microsoft.Office.Interop.Excel dll

using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var bankAccounts = new List<Account> {
                new Account { ID = 345678, Balance = 541.27},
                new Account {ID = 1230221,Balance = -1237.44},
                new Account {ID = 346777,Balance = 3532574},
                new Account {ID = 235788,Balance = 1500.033333}
};
            DisplayInExcel(bankAccounts);
        }
        static void DisplayInExcel(IEnumerable<Account> accounts)
        {
            var excelApp = new Excel.Application { Visible = true };
            excelApp.Workbooks.Add();
            Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
            workSheet.Cells[1, "A"] = "ID Number";
            workSheet.Cells[1, "B"] = "Current Balance";
            var row = 1;
            foreach (var acct in accounts)
            {
                row++;
                workSheet.Cells[row, "A"] = acct.ID;
                workSheet.Cells[row, "B"] = acct.Balance;

            }
            workSheet.Range["B2", "B" + row].NumberFormat = "#,###.00 €";
            workSheet.Columns[1].AutoFit();
            workSheet.Columns[2].AutoFit();
        }
    }
    public class Account
    {
        public int ID { get; set; }
        public double Balance { get; set; }
    }
}

The Output

enter image description here

like image 164
Jobert Enamno Avatar answered Oct 17 '22 20:10

Jobert Enamno