PHPExcel Accounting Formats - phpexcel

I'm working with PHPExcel and I'm trying to format a cell using Excel's built-in "Accounting" format. I'm aware of the Format Code:
PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE
But this simply formats to two decimal places and adds a $ in front of the number. The result I'm looking for is the right aligned cell with the $ on the left. $0 values should be listed as "-" and negative values should be $ (1.11)
As far as I can tell there are no other currency formats in the documentation (though I may have missed it, this documentation is horrendous). Am I looking in the wrong place? Can this be achieved with regular cell formatting or is Excel doing something unique for Accounting?

I reverse engineered the format code from an existing spreadsheet using PHPExcel and got this:
_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(#_)
This is the code Excel places on the cell when you select the "Accounting" format... or click that "$" toolbar button in Excel 2007.
If you didn't need currency symbol :
->setFormatCode("_(* #,##0.00_);_(* \(#,##0.00\);_(* \"-\"??_);_(#_)");

$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#,##0.00");
or use
$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#.##0,00");
Set in getStyle CELL.

There are no other pre-defined formats beyond those listed in PHPExcel_Style_NumberFormat, but you should be able to set the format code to any string that you could use when setting an MS Excel custom format...
e.g.
[green]$#,##0.00;[red]$(-#,##0.00)
As regards cell alignment, set this to right yourself, or don't set it at all.

Related

Pasting SQL decimal columns into Excel

I have a issue with data formats of Excel and SQL.
I have a column in SQL which is of datatype DECIMAL(18,0) and when I am trying to paste the result in SQL..the last 3 digits of the sql result gets replaced by 0 in Excel.
Example:
In SQL the result set has a column called session id and has decimal numbers like
119,597,417,242,309,670
329,621,151,415,350,454
134,460,940,261,658,890
but when I paste it in Excel the numbers look like:
I tried changing the format in EXCEL to paste as text however, the whole format of the result set gets distorted (and only the first column gets pasted properly without the 0's)
I can't keep casting all columns in SQL from decimal to int as there are way too many columns.
Can you please guide me as to what I can do?
Numeric fields in Excel are limited to 15 digits precision.
In SQL Assistant under Tools / Options / Data Format you can ask to have large Decimal (and BIGINT) fields displayed as text for just this sort of copy / paste. Or you can tell SQL Assistant to Save As or Export to Excel format.
For other tools you can explicitly FORMAT and CAST the data to VARCHAR in your SELECT so it is retrieved as text.
Several things you can do. I'll list 4.
Pick whatever suits you best.
First paste in a text editor (like notepad), seach/replace there, and paste that.
Set the datarange where you're going to paste to "text", and then paste. After that you can search/replace, and change to the correct format.
Change the regional settings of Windows to match the data that you have.
You can generate formula's from your SQL query, instead of floating point numbers. So generate a text like =5/10 instead of 0.5 or 0,5. Excel will pick it up correctly regardless of your regional settings.

Set decimal separator to dot in PHPExcel

I need to have a dot as decimal seperator for my currency values.
Unfortunately in my excel file, I keep getting a comma instead. I can't figure out how to fix this.
I have tried several ways to format the cells thanks to a couple of other similar posts on here, but the output is the same.
I am using this now:
$excel->getActiveSheet()->getStyle('F1:F'.$i)->getNumberFormat()->setFormatCode('0.00');
I had found a variable that returned the decimal character, but I can't find that anymore, sorry. I have been looking for so long, that I can't find that specific post anymore. It returned a dot. That is why it is so strange that I keep getting a comma in the generated excel file.
Also tried to force the locale to en_us. Even though it should already be that.
Does anyone have any idea what else I could be checking?

PHPExcel converts dot after writing in comma

I generate an Excel file with importing a csv file. In CSV has contents with following numbers
4.0238484
5.3833888
dot seperated
But if I write an Excel file than the column show me the numbers in following format
4,0238484
5,3833888
I want the dot instead of comma.
How can I make it?
PHPExcel Version 1.7.7
In PhpSpreadsheet (next generation of PhpExcel) you can avoid it's by this:
$sheet->setCellValueExplicitByColumnAndRow(__COL_INDEX__, __ROW_INDEX__, __SOME_DATA__, DataType::TYPE_STRING);
setCellValueExplicitByColumnAndRow can force turn your data into string
Check the locale settings for the version of MS Excel that you are using to view the generated file: if it's set to a locale that uses a decimal comma rather than a decimal point, then this is what you will see. Floating point numbers in PHPExcel are managed with a decimal point (PHP doesn't offer any alternative for numbers), but MS Excel has its own formatting rules based on locale.

No Way to Format Cell as Month-Date-Year in PHPExcel?

I've am trying to format cells in an Excel document I create with PHPExcel using the setFormatCode method. I've been going through the list of 'FORMAT_DATE_xxxxxxx' options associated with this method, but I've been unable to find one that lists the month, date, and year in that order. Did I miss something here?
You missed FORMAT_DATE_XLSX14 and FORMAT_DATE_XLSX22
You also missed the fact that these are simply predefined string values, and that you can use any valid Excel numberformat string value that you set yourself.
$objPHPExcel->getActiveSheet()->getStyle('C9')
->getNumberFormat()
->setFormatCode(
'mm-dd-yyyy' // my own personal preferred format that isn't predefined
);

Integer zero, "0' will be ignored when upload to SQL Server

i have a page that allow user to upload an excel file and insert the data in excel file to the SQL Server. Now i have a small issue that, there is a column in excel file with values, such as "001", "029", "236". When it's insert to the SQL Server, the zero in front will be ignored in SQL, so the data would become "1", "29", "239". The data type for the column in SQL is varchar(10). How to solve this?
Excel seems to automatically convert cell values to numbers. Try prefixing the cell contents with a single quote in the Excel sheet prior to processing. Eg '001. If you can't trust the users to do that, use a string formatting routine to left pad the numbers with zeroes.
Something must be converting the data in the excel cell from a string to an integer. How are you performing the insert?
If a user enters 001 into Excel, it will be converted to the number 1.
If the user enters '001 into Excel, it will be saved in the cell as text.
If the cell is pre-formatted with the number format "#", then when the user enters 001 into the cell it will be entered as the text "001". The "#" number format tells Excel that the cell is a text cell and any entry (whether it looks like a number, date, time, fraction, etc...) should simply be placed in the cell as is - as a text cell.
Can you tell your users to pre-format this column with "#"? This is generally the most reliable way to handle this since the user does not have to remember to enter '001.
Maybe setting up the datatype "Text" for an Excel cell will help.
Excel is probably the culprit here. Try converting your file to CSV and see how it comes out. If the leading zeros are gone in the new CSV file, Excel is the problem.
Excel always does this, and its a nuissance. There are three workarounds I know of:
BEFORE entering the data in any cell in Excel format the cell as text (you can do a whole column if needed.) This only works if you control the spreadsheets and users, which is basically never :-).
Assume you'll get a mix of numbers and/or text in the Excel data, and fix it in Excel before import: add a column to the spreadsheet and use the TEXT() function to convert the number to text, as in =TEXT(A2, "000"); fill down. Also assumes you can edit the worksheet.
Assume you have to fix the numbers upon insert in your code. Depending on how you are loading the data, that could happen in T-SQL or in your other code. In TSQL this expression works to pad with zeros to a width of 3 characters: right( '000' + cast ( 2 as varchar(3) ), 3 )

Resources