JExcelApi: multiple formats in one cell? - excel

In Excel, I can have multiple text styles in a single cell. Is there a way to create a file like this using JExcelApi? I'm not seeing anything so far: setCellFormat is a method on WritableCell, and there doesn't seem to be any way to set a format for anything within a single cell.
Am I just missing it (quite possible!), or is this not implemented?
As a bonus: how hard would this be to implement? Is there any other Excel-export library which does implement this, from which I could borrow the code?

#Cosmic There is another way to read that question: multiple formats in separate areas of a single cell.
Like: "Italics Bold Text" with "italics" and "bold" set in different style, i.e. bold not italics, respectively.
Can this be done in JExcelAPI? I am not aware of this. Anyone?

With variables WritableSheet ws, int col, int row
The following code will set your cell's font to bold.
WritableCell wc = ws.getWritableCell(col, row);
WritableCellFormat cf = wc.getCellFormat() != null ? new WritableCellFormat(wc.getCellFormat()) : new WritableCellFormat();
WritableFont wf = new WritableFont(cf.getFont());
try {
wf.setBoldStyle(WritableFont.BOLD);
// refer to http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/write/WritableFont.html for other text styles
cf.setFont(wf);
wc.setCellFormat(cf);
} catch ...
A CellFormat/WritableCellFormat contains lots of different formatting options, such as the font, borders, background colour and wrap.
So, yes. You were just missing it :p
EDIT: As I didn't make it clear enough, for multiple styles you can call multiple methods on your WritableFont, e.g setBoldStyle(), setItalic(), setUnderlineStyle(), setStruckout(), setColour(), etc.

Related

Python Docx Table row height

So column width is done using cell width on all cells in one column ike this:
from docx import Document
from docx.shared import Cm
file = /path/to/file/
doc = Document(file)
table = doc.add_table(4,2)
for cell in table.columns[0].cells:
cell.width = Cm(1.85)
however, the row height is done using rows, but I can't remember how I did it last week.
Now I managed to find a way to reference the rows in a table, but can't seem to get back to that way. It is possible to change the height by using the add_row method, but you can't create a table with no rows, so the the top row will always be the default height, which about 1.6cms.
There is a way to access paragraphs without using add_paragraph, does anyone know how to access the rows without using the add_row method because it was that that I used to set row height in a table as a default.
I have tried this but it doesn't work:
row = table.rows
row.height = Cm(0.7)
but although this does not give an error, it also has no effect on the height.
table.rows is a collection, in particular a sequence, so you need to access each row separately:
for row in table.rows:
row.height = Cm(0.7)
Also check out row.height_rule for some related behaviors you have access to:
https://python-docx.readthedocs.io/en/latest/api/table.html#row-objects
When you assign to table.rows.height, it just adds a .height instance attribute that does nothing. It's one of the side-effects of a dynamic language like Python that you encounter a mysterious behavior like this. It goes away as you gain more experience though, at least it has for me :)
Some additional information:
The answer here is correct, but this will give a minimum row height. Using WD_ROW_HEIGHT_RULE.EXACTLY will fix the cell height to the set row height ignoring the contents of the cell, this can result in cropping of the text in an undesirable way.
para = table.cell(0,0).add_paragrph('some text')
SOLUTION:
add_paragraph actually adds a blank line above the text.
Use the following instead to avoid using add_paragraph:
table.cell(0,0).paragraphs[0].text = 'some text'
or using add_run can make it easier to also work with the text:
run = table.cell(0,0).paragraphs[0].add_run('some text')
run.bold = True

getFillForegroundColor() is returning 0 for a coloured cell

While reading color of a XSSFCell, cellStyle.getFillForegroundColor() is returning 0 even though there is some user defined color in the cell. But i am able to get the non-zero value for few well known colors.
for ( int rowNo=0; rowNo<=sheet.getLastRowNum(); rowNo++){
Row row = sheet.getRow(rowNo);
for(int cellNo=0; cellNo<row.getLastCellNum(); cellNo++) {
Cell cell = row.getCell(cellNo, Row.CREATE_NULL_AS_BLANK);
CellStyle cellStyle = cell.getCellStyle();
System.out.println("ForegroundColor:"+cellStyle.getFillForegroundColor());
System.out.println("BackgroundColor:"+cellStyle.getFillBackgroundColor());
}
}
Please suggest on how to read the custom color value from XLSX file.
Excel has two different notions of color, an indexed one and a custom one. Indexed colors are predefined colors where the color-value is defined by Microsoft. Custom colors store the usual RGB-value triplet.
XSSFCellStyle has different functions depending if you want to read indexed or custom color, e.g. getFillForegroundColor() returns the indexed color, getFillForegroundColorColor() returns a custom color.
These methods will not try to "convert", so if you have a custom color, the indexed-method will return 0 and if you have an indexed color, requesting the custom color will return null.
See also the related JavaDoc.

How to determine the color of the one part of the text in cell by Apache POI

I have Excel table.
In the cell we can have text (for example text1;text2) and part 1 (text1) of this text will have black color,
but another one (text2) have, for example, red color.
For read Excel file I use Apache POI.
Could you please ask how to determine the color (which in our case is red) of THAT second part of the text.
For determination of the whole text in the cell I use now this code:
HSSFCellStyle cellStyle = (HSSFCellStyle) currentCell.getCellStyle();
short cellColor = cellStyle.getFont(currentCell.getSheet().getWorkbook()).getColor();
I don't know if it determinate it correct
You need to fetch the cell's contents as a RichTextString and then get the formatting information from that
For HSSF, your code would want to be something like:
HSSFRichTextString richTextString = hssfCell.getRichStringCellValue();
for (int i=0; i<richTextString.0,r.numFormattingRuns(); i++) {
if (richTextString.getFontAtIndex(i) == HSSFRichTextString.NO_FONT) {
// Default cell formatting rules apply to this bit
} else {
HSSFFont font = workbook.getFontAt(richTextString.getFontAtIndex(i));
// This bit of the string has the above font
}
}
You can use methods like getIndexOfFormattingRun() to work out where in the string the font changes

HowTo: Visio SDK page Re-Layout FlowChart Top to Bottom

I'm creating a dynamic VSD from a hierarchical set of data that represents a flowchart. I don't want/need to fuddle with absolute positioning of these elements - the automatic layout options will work just fine.
The problem is I can't figure out how to perform this command via code. In the UI (Visio 2010), the commands are on the ribbon here: Design (tab) -> Layout (group) -> Re-Layout (SplitButton).
Any of these will do. Looking through the Visio SDK documentation and Googling for a couple days have turned up nothing of very much use.
Any ideas? (using C#, but VB/VBA would do)
The Page.Layout() method itself is not enough.
In the WBSTreeView.sln sample project (VB.Net) I found how to accomplish this, but couldn't post my answer until 8 hours later :-x
The other layout types are possible by looking through the enums used below.
Compact -> DownRight just ended up being better for most of the flows we're creating.
Translated to C#:
// auto-layout, Compact Tree -> Down then Right
var layoutCell = this._page.PageSheet.get_CellsSRC(
(short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowPageLayout,
(short)VisCellIndices.visPLOPlaceStyle);
layoutCell.set_Result(
VisUnitCodes.visPageUnits,
(short)VisCellVals.visPLOPlaceCompactDownRight);
layoutCell = this._page.PageSheet.get_CellsSRC(
(short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowPageLayout,
(short)VisCellIndices.visPLORouteStyle);
layoutCell.set_Result(
VisUnitCodes.visPageUnits,
(short)VisCellVals.visLORouteFlowchartNS);
//// to change page orientation
//layoutCell = this._page.PageSheet.get_CellsSRC(
// (short)VisSectionIndices.visSectionObject,
// (short)VisRowIndices.visRowPrintProperties,
// (short)VisCellIndices.visPrintPropertiesPageOrientation);
//layoutCell.set_Result(
// VisUnitCodes.visPageUnits,
// (short)VisCellVals.visPPOLandscape);
// curved connector lines
layoutCell = this._page.PageSheet.get_CellsSRC(
(short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowPageLayout,
(short)VisCellIndices.visPLOLineRouteExt);
layoutCell.set_Result(
VisUnitCodes.visPageUnits,
(short)VisCellVals.visLORouteExtNURBS);
// perform the layout
this._page.Layout();
// optionally resize the page to fit the space taken by its shapes
this._page.ResizeToFitContents();
//
Changing Connector Line Colors
If you're unfamiliar with how formulas for colors work, this might also be very frustrating. By default you can give an int as a string to get pre-defined colors, but this isn't very helpful because there isn't an easy way to figure out what each of those colors are. (There is a Page.Colors collection, but you have to inspect each of their RGB values and figure out the color from them.)
Instead, you can use your own RGB values for the formula.
private void SetConnectorLineColor(Shape connector, string colorFormula)
{
var cell = connector.get_Cells("LineColor");
cell.Formula = colorFormula;
}
internal static class AnswerColorFormula
{
public static string Green = "RGB(0,200,0)";
public static string Orange = "RGB(255,100,0)";
public static string Yellow = "RGB(255,200,0)";
public static string Red = "RGB(255,5,5)";
}
Call the Layout method on the Page object. If there are shapes selected on this page then this method will only operate on the current selection. You may want to call DeselectAll on the ActiveWindow first.

Can I force a display font from inside a formula in Crystal Reports?

I have a report with a lot of formulas that translate the word "TRUE" into an "X" and "FALSE" into a blank space.
I use these to put an "X" in a checkbox.
Sometimes there is text after my checkbox. To avoid slight shifts to the left and right, I print the "X" and the " " in a fixed width font. It is very tedious to manually set the font for each field.
Is there a way to specify a display font from inside the formula?
Something like this would be nice:
DisplayFontName = "Courier New"; //wishful thinking
DisplayFontSize = 8; //wishful thinking
//this is the code I'm currently using
if Uppercase({table.somefield}) = "TRUE" then "X"
else " "
No, this is not possible. The context of a formula is the property for which the formula is set only. You cannot access properties of the whole object e.g. a field. Perhaps you could think about using two images suppressed by a formula depending on the value of your field. Then you would get rid of the font problem.
From what I remember, you can set the Font using formula field where you can write
if Uppercase({table.somefield}) = "TRUE" then "Courier New"

Resources