XSSF worksheet cells having random styles - excel

Is there any way to set style for all next cells in excel worksheet? Like for the rest of the document after certain cell?
I am using Apache POI 3.9 for exporting data to excel worksheet. Client wants colored font for invalid data. Everything is running fine, but sometimes, when I put text in empty cells in "INFORMATION" column, it makes font colored red, even after reseting cell style or explicitly asking for condition and then setting default cell style. Youre more likely to get my idea from pic of code.
Ive uploaded some pic of excel too. Hope it helps. It starts on row 37, text somehow changes color even after not running through setting conditions. I havent recognized any pattern in there.
I tried to change conditions, change cell style setting location in code... Had no effect.
Edit: It seems like the problem is not in AP but in Excel itself. It looks like the font color is changed after user input and its caused by autoformatting that is based on the context. So only option here is to change style of previous data. Could not find any other solution to this.

Related

How to get border of a specific Cell using Apache POI

my Excel sheet seems like what you see above. I'm using Apache POI.
I need to create a XML element for each register you see. Using the Borders that you see. How can I get the border information of a specific cell?
I tried many code but they use the workbook and gets its style. I want to be able to get Cell border of one specific Cell.
System.out.println(workbook.getCellStyleAt(0).getBorderLeft());
Excel file:

How to get default Excel worksheet font?

We have Excel 2013-2016 Add-in in C#. We use following method to get worksheet font:
var defaultFont = Microsoft.Office.Interop.Excel.Worksheet.Cells.Font
Everything worked until user changed font of some particular cell. For example user changed cell A1 and set font "Arial". By default we have font "Calibri".
Thereafter when we try to get default font by Worksheet.Cells.Font we get nothing - just empty object. I assume that because of ambiguity: A1 has font "Arial", other cells - default font "Calibri". My goal to get default font, i.e. "Calibri".
So far I re-write my code and now I check Styles too:
var workbookFont = Microsoft.Office.Interop.Excel.Worksheet.Parent.Styles["Normal"].Font;
This workaround returns exactly what I need - my "Calibri" font. But then I found , that's because this font is default for all new worksheets/workbooks (it can be setup in Options of Excel). It doesn't work if user selected all cells in worksheet and changed default font to "Times New Roman", then I would get "Calibri" - because this is default font will be used once new worksheet/workbook created. Again I faced issue when I can't get real default font for current worksheet. My next thought was to get font of some particular cell at the edge of worksheet that is not much usable for user like:
var defaultFont = workSheet.Cells[1048576][16384].Font
It looks pretty weird, but it works. My assumption that user doesn't use the last cell on a worksheet. (The 1048576 and 16384 are max size of worksheet ). I don't know implications of these method, so I'm wondering does it exist some "legal" way to get default font of worksheet , without such crutch ?
You were on the right path when using:
var workbookFont = Microsoft.Office.Interop.Excel.Worksheet.Parent.Styles["Normal"].Font;
The Normal style is the default font for a sheet, but the sheet might not actually have any instances of the default font (or the Normal style), or, all cells decorated with the Normal style may have had the font overridden on each cell format.
If a user applies their own fonts to various ranges, then the sheet will potentially have numerous fonts, and none of them are guaranteed to be the same as the font in the Normal style. The Font.Name might differ across a sheet, in which case Font.Name returns null, even though the Font.Size might be consistent (or if it isn't, then it too returns null).
When a user applies a custom font to a range that already has the Normal style, then the Style remains associated with the range, and the font overrides any font defined in the style.
Furthermore, it is common practice to select all the cells on the sheet when changing the font of the UsedRange (so that the row heights adjust proportionally), even if the UsedRange is only a small portion of all Cells.
So, you have to choose a font that is representative of the fonts that are used in the sheet, or apply fonts to new ranges as if they were Normal. That choice should be informed by what you know about the sheet, and what you intend to do with the font:
If you're inserting a column or row, then I'd suggest that you mimic Excel behavior and use the adjacent formats.
If you're adding a new range that is not adjacent to the UsedRange, then you will probably want to default to the Normal style.
If you're looking for a representative font of the UsedRange, you might want to avoid the first few rows and columns, as these tend to be headers, and the last rows can sometimes be totals. You'll need to loop over the cells in the range to find an appropriate cell format.
If you're reproducing the sheet content in some other format, then you'll need to inspect the font of each cell and every cell in the UsedRange.
The bottom-right-most cell is not necessarily any more or less similar to Normal or the fonts used in the UsedRange, so I'd advise against using it.

Changing font style in excel from vb6?

i am currently coding a button to transfer all the data from a listview into an excel workbook so it can be printed as barcode labels. the way the barcode works is that it is a true type font. im running into the problem however through vb6 where i dont know how to format a range of cells to have a different font style so i can change certain cells to be able to show barcodes. ive gotten as far as
oSheet.range("D2:D10").Font
where D2:D10 is not the set range of cells just ones i was playing with. im not sure where to go after the font property here.
Have you tried:
oSheet.range("D2:D10").Font.Name = "BarCode Font Name"

Excel background colour changing automatically

Whenever I copy or move a cell in excel, the original cell location changes colour, how can I stop this?
I have tried clearing the format but it doesn't seem to help... and it only occurs in the main area that I am working in (i.e. if I scroll off to the side or bottom it doesn't seem to happen).
Anyone have any ideas? It's quite annoying.
Conditional Formatting should do the trick. Highlight the cells that concern you and use the option if Cell contains value... then select blank. The format should be auto background (or whatever color you would like)
You may find a custom format already there causing the issue. If so just delete it and you should be all set.

Live charts in Excel: for future dates, lines drop to zero

I run live market charts in Excel. A graph indicator runs from live data fed by the broker through the DDE facility in Microsoft Excel. It works fine and I am happy with it - except for one blemish I hope you can assist with.
The lines of the graph (it is a line chart type) are created in real time. The present time and the past time are great, very clean. Unfortunately in the period just ahead i.e. that hasn't arrived yet, the lines of the graph drop to zero and crawl along the x-axis into the future. This spoils the current reading of the graph.
Is there any way I can prevent this happening so that the lines (properly called curves) only exist in the past and current time period. The worksheet is set not to show zero values in formulae, but the charting facility does not appear to have this function.
If you put #N/A in the cell (using the =NA() function) the points are not drawn.
To hide the ugliness of your cells now having #N/A in them for future dates you can use a number format or conditional formatting to hide error values. You do this by adding a formula to your conditional formatting of
=ISNA(A1)
and setting the format to be a white font on a white background. Which makes the cells with #N/A in them appear blank. For versions of Excel prior to conditional formatting being available you can do the same thing with a number format but in reverse. i.e., set your font to white so all cells are invisible by default and set your number format to:
[black]0.0;[red]-0.0;[black]0.0;[black]
which should make anything that isn't an error appear with either a black or red font colour.
Regards
Dave
I once saved an Excel 97 Worksheet (with charts) in Excel 2003. After that, whenever I saved the file in '97, I got a prompt telling me that the spreadsheet was created in a newer version of Excel.
To eliminate the prompt I opened it in 2003 and saved it as an Excel 5.0/95 Workbook (*.xls). Then whenever I opened it in 97 the line charts dropped to zero at the end. They didn't do that before.
To correct the problem I right-clicked on the chart, selected Chart type, selected Custom Types, then "Colored Lines". Apparently when I saved it as Excel 5.0/95 that chart type was not supported, so the program defaulted to the type "Smooth Lines". After changing the chart type to "Colored Lines" it was only necessary to do some minor formatting.
This experience suggests a solution to the line dropping to zero problem regardless of what triggered it. Simply change the chart type. When I did that the dropped line disappeared and I didn't have to use the #N/A approach.

Resources