How to set width for particular cell in word table? - apache-poi

Is it possible to set width for particular cell in word table using XWPF document without altering other cell width?
I am using the below code to set the width, but its setting for entire column.
tableRow.getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(5000));
my wishes
my works

I assume that the cell is already created :
Long cellWidth = 2000;
CTTblWidth width = cell.getCTTc().addNewTcPr().addNewTcW();
width.setType(STTblWidth.DXA);
width.setW(BigInteger.valueOf(cellWidth));

Related

How to adjust row height in excel using openpyxl

I'm trying to update text content of a cell by adding multiple lines, but when I save the file the row height is not adjusted and the full content is not visible. Is there any way to get the cell height and put it in ws.row_dimensions[i].height = cell_height

Create progress line using VBA excel

I try to create progress line by using VBA Excel command
I have all of data to define what progress is but
I don't know how to use VBA for creating progress line
for an example I tried to use connector line but It's seem not work
ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 60, 405,800, 477).Select
Range("Z20").Select
ActiveSheet.Shapes.Range(Array("Straight Connector 11671")).Select
I would not use a shape for a progress bar, instead use the data bars conditional formatting on a cell where the cell value is updated by the code
Create a named range on one cell on the worksheet as, for example, "ProgressBar"
Adjust the width to suit your needs
Format the cell as a Percentage
Choose Conditional Formatting -> New Rule -> Format all cells based on their values
In the "Format Style" drop down, choose "Data Bar"
Select the "Show Bar Only" checkbox
In the "Type" drop downs, for Minimum and Maximum, choose "Number"
In the "Value" text boxes, enter 0 for Minimum, 1 for Maximum
Set the formatting as you wish
In the VB Editor, enter the code below
Sub UpdateProgressBar()
Dim Rows As Integer
Dim RowItem As Integer
Rows = 1000
For RowItem = 0 To Rows
Sheet1.Range("ProgressBar").Value = RowItem / Rows
Next RowItem
MsgBox "Done!"
Sheet1.Range("ProgressBar").Value = 0
End Sub
This will then update the value in the cell and the data bar will grow as the value increases.
You will need to adapt to your own code, but this should be a starting point.
some ways of doing a progress bar:
use the application.statusbar
or, use a userform, in wich you change the width property of any rectangle type (i use a button with nice picture font)
use a shape, from wich you change the size by altering its width property.
the hard part is more making it fast enough so it doesn't affect performance, and sometimes
when adding application.screenupdating=false in conjonction to doevents, it might blink... (or not)
if you need some code i can provide you a sample, and you can change it according to your needs

VBA: Change Excel cell width

I have some VBA and batch Scripts that read the Mac Address Table out of some switches and import it into Excel to format it.
But the text is too long for the default cell width.
Is it possible to change the displayed cell width?
(When saying displayed cell width I mean: this)
Use this:
Range("A1").ColumnWidth = ...
The units for this value are as following:
One unit of column width is equal to the width of one character in the Normal style. For proportional fonts, the width of the character 0 (zero) is used.
For example, the column width for freshly opened Excel file with default styles is 8.43 which is equal to 64 pixels.
...or this to autofit width:
Range("A1").EntireColumn.AutoFit
Another method would be:
.Columns("A").ColumnWidth = 20 (or whatever value you need)
I didn't compare it speedwise but why my guess would be that it's more efficient to just use Columns() instead of Range().
For more info on the ColumnWidth-Value -> MSDN Doc for the columnwidth-property

Can I adjust the widths of Excel columns without setting them each individually?

I'm using cfspreadsheet to generate an Excel spreadsheet using ColdFusion. I insert a header row, and then use spreadsheetAddRows to dump a query into the sheet. The problem is that the columns are often not wide enough. I know I can use SpreadsheetSetColumnWidth to adjust each column individually, but is there any way that I can just apply an auto-width to the entire sheet? I don't know the max width of each column, and I don't want to apply it to each column individually. Excel has an auto-width feature for columns — is there any way to trigger it from the ColdFusion code? (Or even better: Can I add on to the auto-width — set each column to the max width + 2 or something?)
Last I checked there was not a documented CF function. However you can use POI's autoSizeColumn(columnIndex) method to auto size each column. Just note POI uses base zero for sheet and column indexes.
<cfscript>
// create a workbook and add a long value
wb = SpreadSheetNew();
spreadSheetSetCellValue(wb, repeatString("x", 200), 1, 1);
// get the first sheet
sheet = wb.getWorkBook().getSheetAt( javacast("int", 0) );
// resize first column ie "A"
sheet.autoSizeColumn( javacast("int", 0) );
spreadSheetWrite( wb, "c:/test.xls", true );
</cfscript>

Apply percentage format with VSTO - multiply by 100 behind the scenes

I have a piece of code that formats cell according to requirements:
Range.Font.Italic = Microsoft.Office.Core.MsoTriState.msoTrue;
Range.HorizontalAlignment = XlHAlign.xlHAlignGeneral;
Range.NumberFormat = "0.0_%_);(0.0)_%;-_%_)";
And this code is invoked then button on custom ribbon is pressed. It's similar to percentage cell format. One more thing I have to add is multiplying the cell value by 100.
For instance
cell value is set to 0.15, I click button and value changes to 15%.
cell value is set to 2.5, I click button and value changes to 250% etc. (very similar to default Excel Precentage cell style)
However, if I do something like this:
decimal result = Convert.ToDecimal(cell.Value);
cell.Value = result * 100;
and user hits button multiple times, value is multiplied every time. Is there a way to specify something like display format, so that actual value is preserved and only displayed value is multiplied by 100? Or another way to prevent value from being multiplied multiple times?
Well you don't need to multiply it by 100
If the cell has .15 then apply the formatting
Range.NumberFormat = "0.00%"
It will automatically change to 15.00 % and you don't need to multiply it by 100.
FOLLOW UP
An out of the box thinking... Why not hide the % symbol by setting the color of the % to white?
VBA CODE
Sub HidePercentage()
Dim Temp As String
Temp = Format(ActiveCell.Value, "0.00%")
With ActiveCell
.NumberFormat = "#"
.HorizontalAlignment = xlRight
.Formula = CStr(Temp)
.Characters(Start:=Len(Temp), Length:=1).Font.ColorIndex = 2
End With
End Sub
SNAPSHOT
A dirty way would be maintain a hidden sheet, Move original value, or maintain flag for that cell in hidden sheet on button click

Resources