Format table XWPFTable using Apache Poi - apache-poi

How can I format a table while creating docx file using Apache POI. I want to set the size of the cells, font of the text, color, etc..
I found this example, but the cell size is set according the text size.
XWPFDocument document = new XWPFDocument();
// New 2x2 table
XWPFTable tableOne = document.createTable();
XWPFTableRow tableOneRowOne = tableOne.getRow(0);
tableOneRowOne.getCell(0).setText("Hello");
tableOneRowOne.addNewTableCell().setText("World");
XWPFTableRow tableOneRowTwo = tableOne.createRow();
tableOneRowTwo.getCell(0).setText("This is");
tableOneRowTwo.getCell(1).setText("a table");
I want to center the table and increase the cells size.

At the moment, I don't believe that the XWPFTable* classes have direct setters for this. Instead, you'll need to grab the appropriate CT* object from the Table, Row or Cell, and set the properties directly.
A .docx file is simply a zip file of xml files. So, what I'd suggest you do is create two simple word documents. One has a simple table in it, and the other has that table but with the formatting applied. Next, unzip both, and diff them to see what parameters were set to apply the formatting you want.
Finally, get the appropriate CT object and make an appropriate call. eg if the table cell had foo="bar" set on it, you'd do:
XWPFTableCell cell = xwpfTable.getRow(0).getCell(0);
CTTc rawCell = cell.getCTTc();
rawCell.setFoo("bar");
If you do find the appropriate trick, please consider sending in a patch to add the wrappers on the XWPFTable classes. Just open a bug on the POI bugzilla (https://issues.apache.org/bugzilla/) and upload the patch.

You can place the table to center using :
table.setTableAlignment(TableRowAlign.CENTER);

Related

Tabulator - Getting Columns including order and size

I am creating a table using Tabulator, which seems great and very powerful.
I want a way to save relevant data of the table so it can e recreated on the fly.
Currently, I think there are a few things I need...
The row data - I get this using table.getData();
The columns - I get this using table.getColumnDefinitions();
The row data seems perfect I can store that and use it. However, the column information I am saving doesnt appear to have the size of the columns if I have resized them?
Is there a way of getting ALL the relevant column info, so I can save and recreate it exactly?
Alternatively, if there's a single 1 function that saves everything (row data, columns (including order, size etc)) in one go as a JSON or something that may be handy
So you have a few options here.
Config Persistence
If you simply want the table to be the same way it was the last time the user used it on that computer, you could look at using the Peristent Configuration module. This will store a copy of the table column configuration on the browsers local storage so that next time they load the page it will be laid out the same.
Column Layout
If you want to store it externally then you are correct,
the column width is not updated in the definition after a user changes it.
If you want to get the current layout of the columns then you can use the getColumnLayout function to retrieve the current layout of columns:
var columnLayout = table.getColumnLayout();
Though this will only contain the key layout characteristics and not the full definition, you would need to merge them if you wanted to store them in one place.
More details on this method can be found in the Manual Column Layout Documentation

Using Office.js, how to get the exact styling of a table in Excel

I'm using Office.js to create an Add-in for Excel.
I'm able to get the exact styling (fill colors, font size, borders, etc) of individual cells of ranges and now need to do the same for cells of tables.
I'm able to get the the table's style and then get the style's properties. However, besides the fact that this is only supported since ExcelAPI 1.7, it seems to only describe the style in general terms. That is, it doesn't seem to describe the detailed table style properties such as "First row stripe", "Total row", etc.
(Note that getting the table's individual cell styles doesn't work like for cells of ranges. The styling properties, such as fill.color, don't represent the effectively applied styling of the cell if the cell's styling from the table's base style hasn't been overridden.)
Things I have considered:
Convert the table to a range. But I guess that this is destructive to the worksheet and I see no way of performing an undo.
Convert the table to a range and back to a table again. This is destructive as well and I'm afraid I could somehow not exactly recreate the table as it was before.
Create a copy of the worksheet and then covert the tables to ranges. That could work but it's ExcelAPI 1.7 only.
Read the raw style info from the file's OOXML representation but I don't think that that's possible with Office.js alone.
Any suggestion on how to get an Excel table's exact styling information using only Office.js and with an API version as low as possible?
Edit: Turns out we do have access to the whole .xlsx file with Office.js using the Office.context.document.getFileAsync method. I'll try to get the full style info by reading the xl/styles.xml file but I'd still prefer a better solution.
You can still get range from the table you want to handle. For example,
Var range = worksheet.getRange("A1:E1").
Although "A1:E1" is part of a table, you can still get the area as a range and do your further actions.

Change the dynamic text color in a same cell of excel sheet using apache poi?

I am using POI library to deal with an Excel file, what I need is
am dealing with an application in that if you modify anything the changes should be shown in red color.
Example: In my application one of the records has
"stackflowisgood" and then I modify it with "stackoverflowisgood",
now the new change is "over".
So there will be a link available to download an Excel file. After downloading, the Excel should be like "stackoverflowisgood" showing the "over" in red color in the same cell. So here my data changes all the time depends on the user change, so I cannot use any fixed index to color it,
this code is applicable for specific string
HSSFRichTextString richString = new HSSFRichTextString("Hello, World!");
richString.applyFont(6, 10, blueFont);
but I need for dynamic string.

SSRS - Columns Download to .CSV Despite Visibility Status

I have a Reporting Services 2012 table that hides certain columns based on parameter choices, since some choices will cause the dataset to exclude certain columns when run. So, a column such as PassportID would have a hiding criteria expression such as:
=IIF(Parameters!TransitMode.Value = "bus"
OR Parameters!TransitMode.Value = "train",True,False)
The columns are indeed hidden when the report is rendered, and when it is downloaded to Excel. The problem is that I need to download it to a .CSV file. The .CSV downloader in SSRS does not have a layout renderer that can preserve the hiding criteria the way Excel can.
I looked at the DataElementOutput property, but changing this from the "Auto" default only appears to be give the options of downloading or excluding unconditionally, rather than based on column visibility in the rendering.
Is there a way to exclude the entire rendered column from the downloaded .CSV?
The easy answer is to set the displayed value to a formula, using the Render format function. If the Render format is CSV, then set the "Displayed value" to be an empty string. The field will still be exported, but won't contain data.
That is, set the value of the textbox to something like:
=iif(Globals!RenderFormat.Name="CSV", "", Fields!MyDataField.Value)
A little bit more info:
SSRS 2012 and CSV export
Change the DataElementOutput from Auto to NoOutput.
DataElementOutput controls whether or not the data is included in the export. Column headers are already excluded by CSV and names of columns for CSV are derived from the name of the textbox for a data element. Visibility properties are not considered in the CSV export since visibility is a formatting feature.

Reading cell styles along with data-Excel sheet reading using apache POI

folks, I am able to read the excel sheet using apache POI in grails
using this I am inserting the cell values into different tables of mysql,
My problem is, I am not able to get the cell style like, bold or italic etc along with the data.
generally text area will automatically give the style of text while inserting into database.
I need to insert the data along with its style from each cell, please help me in solving it.
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html#getCellStyle()
This should get the CellStyle from the cell, which then you can use to get the information you want.

Resources