I want to format generated excel file(generated by using dynamic reports) using apache poi (vesrion 3.7). I am facing basically two problems in formatting the excel.
Numbers are considered as text and I am getting warning messages in excel (Number stored as text).
Left indentation is not coming in excel, although it is coming in PDF and HTML.
How can I achieve these two things using apache poi in excel report?
Any help will be appreciated.
Thanks in advance!
Regards
Shikha Singhal
Issue 1: Number stored as text warning:
Set the cell type as Numeric:
Cell cell = sheet.getRow(0).createCell(2);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(12345);
Issue 2: Left indentation for Cell
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_LEFT);
cell.setCellStyle(style);
Related
I am trying to apply Persian date format in text boxes inside some worksheet.
The Text boxes now stores the date in format "dd/mm/yyyy hh:mm" And that's a thing that i applied by myself.
But i'm struggling with applying Persian date in these text boxes. I recorded some macro when i'm changing date format of cell to Persian, and i was trying to use it on text box but this code does not seem to work properly.
This is code that i'm using now and it is working properly
TextBox1 = Format(TextBox1, "dd/mm/yyyy hh:mm")
and those lines I was trying to use to apply Persian date but I don't get any effect
TextBox3 = Format(TextBox3, "[$-fa-IR,16]dd/mm/yyyy;#")
And this is the effect of recording macro and changing date format to Persian manually. From this line i got a formula and try to implement it above.
Range("J30").Select
Selection.numberFormat = "[$-fa-IR,16]dd/mm/yyyy;#"
Does any of you could provide me with some reasonable solution to this?
Thank you in advance for your Help.
This blog post should be of help to you as it covers a number of different date formats, including Persian https://www.jquery-az.com/3-ways-change-date-format-excel-14-date-formulas/
In excel, the following formula will format a standard date in Persian:
=TEXT(A1,"[$-0429]mmmm d,yyyy")
So in VBA, the following should work:
Range("J30").numberFormat = "[$-0429]mmmm d, yyyy"
In terms of formatting within a text box, providing the textbox text is referring to a cell location, formatting this cell location will impact on the format of the textbox - see illustration below.
If you are trying to format text directly within the textbox you might try adding .value to your code snippet above eg.
TextBox3.value = Format(TextBox3.value, "[$-fa-IR,16]dd/mm/yyyy;#")
I only have access to Office for Mac at work and that feature is not supported for me to test.
This blog post discusses how to apply formatting to text boxes via VBA but it may not support language formatting options. If thats the case you may consider amending your text boxes to reference a cell location that you can format.
https://www.extendoffice.com/documents/excel/4899-excel-format-textbox-as-currency.html
Have you installed Persian?
File > Options > Language > [Add additional editing languages] - Select Persian > Add > OK
You will probably need to restart Excel and may need to re-apply the formatting.
I'm having a problem and havnt managed to find a solution online and was hoping I'd get lucky and someone could help.
I have a database application that exports a large dataset to .xlsx
A VBA application then maps this data into another Excel application.
When the data is exported out of the original database application, this process is outside of my control. All the cells have a 'General' cell format and we have some large numbers such as 172627108914 which is the serial number for a piece of equipment. In the exported xlsx file, this serial number is represented as 1.72627E+11.
The next stage of the process copies this data into another worksheet which has all cells formatted as text. The value is copied over but the value stays the same and the format of the cell changes from Text to General.
Does anyone know what I have to do to change to remove the scientific notation?
I'm using Microsoft Excel 2010.
Thanks
Append a single apostrophe to the front of the number. That will force Excel to read the number in 'text' format automatically, and the apostrophe will not show up at the front of the number when it's displayed.
Thanks for the help everyone, in the end some VBA code was written to convert all fields in the original export to text then iterate over each cell and rewriting the value from the formula line. This has resulted in a correctly formatted worksheet to pass to the second application
I'm using NPOI to generate XLS spreadsheets. NPOI is an Excel Spreadsheet generation library/API that is available on codeplex, enables you to create workbooks, formatting, formulae and so on and so forth....I use it to create workbooks with multiple sheets that contain the output of the various calculations.
I've used the following custom data format for each cell that contains a value, which I've plumbed into NPOI using code along the lines of:
var newFormat = MyNPOIWorkBook.CreateDataFormat();
var customFormat = newFormat.GetFormat("[=0]0;0.####");
the customFormat is then applied to a Cell within a method that creates the Cell Styles.
This works rather well - but not so well when the 4th decimal place after the point/period is a 0. When this is the case, I do not get the 0. My requirement is to have the 0 actually show!
So, to illustrate. 0.33445566 displays in the spreadsheet as 0.3343 (fine!) - but 0.3340 displays as 0.334 (not fine) - I want 0.3340. I appreciate that this is somewhat trivial, but I would like to satisfy my objective precisely :)
Can anybody help - either by suggesting a workaround, or altering my custom format in some magical fashion?
Thanks
-SB
How about [=0]0;0.###0?
I generating a excel sheet using jxl, a number of the columns have data that is
preceeded by a hidden apostrophe. The apostrophe can only be seen when you
click on the cell. I have looked and can't find a post that addresses this.
Can anyone out there tell me how to remove this using jxl or any other.
I've a feeling that the apostrophe indicates to Excel that the data in the cell should be treated as text.
Are you writing your cells to the spreadsheet as the correct types? E.g. jxl.write.Number, jxl.write.Label, jxl.write.DateTime, all of which implement jxl.write.WritableCell.
More information about how you're writing these cells, what data they contain and how it appears in Excel would be useful.
Excel usually treats Conditional Formatting formulas as if they are array formulas, except when loading them from an Excel 2002/2003 XML Spreadsheet file.
This is only an issue with the Excel 2002/2003 XML Spreadsheet format... the native Excel format works fine, as does the newer Excel 2007 XML format (xlsx).
After loading the spreadsheet, it is possible to make it work correctly by selecting the formatted range, going to the Conditional Formatting dialog, and clicking OK--but this only fixes the problem for the session.
Test case:
Enter the following into a new sheet:
A B C
1 N N N
2 x x x
3 x x x
Create this conditional format formula on cells A1:C1 (your choice of pretty colors for the format):
=(SUM(($A1:$C1="N")*($A$2:$C$2=A$3))>0)
This is an array formula that activates for A1, B1, and C1 whenever any of them has an "N" and the cell in row 2 below the "N" is equal to the cell in row 3 of the current column.
(This has been simplified from a real-world business spreadsheet. Sorry for the complexity of the test case, I am trying to find an easier test case to present here.)
And it works... you can alter the N's or the x's in any way you want and the formatting works just fine.
Save this as an XML Spreadsheet. Close Excel, and re-open the file. Formatting is now broken. Now, you can only activate conditional formatting if A1 is an "N" and A2 is the same as A3, B3, or C3. The values of B1, B2, C1, and C2 have no effect on the formatting.
Now, select A1:C1 and look at the conditional formatting formula. Exactly the same as before. Hit OK. Conditional formatting starts working again, and will work during the entire session the file is open.
Workarounds considered:
Providing the file in native (BIFF) Excel format. Not an option, these spreadsheets are generated on the fly by a web server and this is only one of dozens of types of workbooks generated dynamically by our system.
Providing the file in the Excel 2007 native XML format (xlsx). Not an option, current user base does not have Office 2007 or the compatibility plug-in.
Asking users to select the range, enter the Conditional Formatting dialog, and hitting ok. Not an option in this case, unsophisticated users.
Asking users to open the XML spreadsheet, save as native XLS, close, and re-open the XLS file. This does not work! Formatting remains broken in the native XLS format if it was loaded broken from an XML file. If (3) above is performed before saving, the XLS file will work properly.
I ended up rewriting the conditional formatting to not use array formulas. So I guess this is "answered" to some degree, but it's still an undocumented, if obscure, bug in Excel 2002/2003's handling of XML files.
I tried to recreate the problem you describe. Here is what I found.
Could consistently recreate the
problem using Excel 2003 on Windows
XP when saving as an XML
spreadsheet.
Could not reproduce the problem
using Excel 2003 on Windows XP when
saving as a standard xls
spreadsheet.
Could not reproduce the problem
using Excel 2007 on Windows Vista
when saving the file in the native
xlsx format.
Could not reproduce the problem
using Excel 2007 on Windows Vista
when saving the file in the Excel
97-2003 xls format.
(Note: All instances of Excel and Windows are current with all Windows updates.)
I also added a simple conditional formatting formula to each test. In every case, it worked as expected after saving the file, closing Excel, and reopening the file.
So the answer seems to be to use the standard Excel 2003 file format when saving the file.
BTW, this is a very odd formatting formula. It is difficult to imagine how you would use it. It must be a very specific & unusual business case. I also have the feeling something is missing in your post. (I'm not accusing you of being dishonest – just wondering if you may have shortened the formula for readability.) If this is not the exact formula you are using, please edit your original post with the complete formula and I will be happy to revisit this issue.
You can find some tutorial videos for self studying the conditional formatting issue over the following pages:
conditional formatting