display number as text in export excel using poi - apache-poi

I write java program export excel using apache poi
set cell value 00400 but it automatically convert 400 how can set value as 00400
cell type is set to String but cursor is enter to cell it also change to 400

You do this exactly the same way you do in Excel itself - enter the number 400 then apply cell formatting to force it to have the leading zeros
As covered in the Apache POI docs, you'd do something like:
// Do this once
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("format sheet");
DataFormat format = wb.createDataFormat();
// Styles are per-workbook not per-cell
CellStyle style5Zeros = wb.createCellStyle();
style5Zeros.setDataFormat(format.getFormat("00000"));
// Create a cell and style
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(400);
cell.setCellStyle(style5Zeros);

Related

Ho do you hide / highlight rows based on the value cells

I am new to excel and want to programmatically do two things.
Hide entire row if Code = 0
Set background and border to all cells in rows if columns I - O = 0
Any help would be appreciated. Thanks in advance.
Place the following code in a new module (VBA Editor, Insert->Module).
Public Sub ToggleRows()
Dim Sheet As Worksheet
Dim Row As Long
Set Sheet = ThisWorkbook.Worksheets("Sheet1") ' Replace with the name of your worksheet
' Get Row Number of last Row
Row = Sheet.UsedRange.Rows.Count + Sheet.UsedRange.Row - 1
Application.ScreenUpdating = False
While Row > 1 ' (exclude header row)
' Hide/Unhide Row depending on value in Column A
Sheet.Rows(Row).EntireRow.Hidden = IIf(Sheet.Cells(Row, "A").Value = 0, True, False)
Row = Row - 1
Wend
Application.ScreenUpdating = True
End Sub
After copying it you should have a "ToggleRows" macro in the list when you press the Macros button in the Developer tab.
If you wanted the macro to run automatically, you need to add a few more lines of code.
Open the code module for your worksheet and put the following
Private Sub Worksheet_Calculate()
ToggleRows
End Sub
For the second part of your question, you should be able to use Conditional Formatting.
There are many ways you can do it, one way is:
Select Columns I->O and select New Rule from the conditional formatting menu, and then select Use a formula to determine which cells to format
Enter a formula (example below) and then specify the formatting.
=IF(SUM(INDIRECT("I"&ROW()&":O"&ROW()))=0,TRUE,FALSE)
If Sum of Cells I-O equals 0, apply the formatting, otherwise dont.

VBA Excel - How can a formula be placed in a range of cells of a body range of a newly added column in a table

I use this to add Columns in an existing table.
position = 1 ' or 5 or 12
With ActiveWorkbook.Sheets(PREPARE_CALC_SHEET).ListObjects(PREP_CALC_TABLE_NAME)
.ListColumns.Add Position:= position
.HeaderRowRange.Value = "Title"
.ListColumns(position).DataBodyRange.Formula = "=1*9"
End With
How do I get the range of the body cells inside the column that I just added?
How can I apply a formula to those column body cells using that range?
I not just want using ".ListColumns(1).DataBodyRange.Formula = "=1*9" ", but want to loop through the range of cells.
How can I translate this
.ListColumns(position).DataBodyRange
to a Range to walk through?
Any suggestions are appreciated
However the question itself was answered, I do get a Debug dialog on the second line
.ListColumns(1).DataBodyRange.NumberFormat = "General"
.ListColumns(1).DataBodyRange.Formula = "=CONCATENATE([#Sort2];[#Sort3];[#Sort4];[#Sort7])"
Why?
Please, try this way:
With ActiveWorkbook.Sheets(PREPARE_CALC_SHEET).ListObjects(PREP_CALC_TABLE_NAME)
.ListColumns.Add Position:=Position
.ListColumns(Position).name = "Title"
.DataBodyRange.cells(1, Position).Formula = "=Today()"
End With

Add fixed text to Excel userform textbox

I would like to add additional fixed text to textbox data in specific cells in an Excel spreadsheet. For example, in the textbox the user would enter "g0/0" but in the cell A2 it should read "interface g0/0". Here is what i have so far:
Private Sub Populate_Click()
Dim ws As Worksheet
Set ws = Worksheets("Remote")
ws.Range("A2") = Interface.Value
So I assume i would add "interface" somewehere on that last line but i am unsure of the syntax.
thanks!
You may need to first sonstrict the required value in a string, then make that the value of "A2". Assuming your input text box is on a form...
Dim InterfaceValue as string
InterfaceValue = "interface" & FormName.InputTextBox.Value
ws.Range("A2").value = InterfaceValue
You can try to loop all the rows, and insert fixed string value to a cell.
Ex.
for(XMLResource report: reports) {
Row row = sheet.createRow(rowNum++);
sheet.setColumnWidth(0, 1000);
row.createCell(0)
.setCellValue(report.getPage());
row.createCell(1)
.setCellValue(report.getTestName());
row.createCell(2)
.setCellValue(report.getDescription());
row.createCell(3)
.setCellValue(report.getSTATUS());
row.createCell(4)
.setCellValue(report.getBuisImpact());
row.createCell(5)
.setCellValue(report.getVali());
}
(report is string only object)

how to merge excel cell in vsto

I am trying to merge two cell of excel row, it works but when I insert a new column and than trying to merge row cell including new column cell it does not and I required to insert a new column what should I do?
Range rgSum = UsedArea.Cells[i, cellnum];
rgSum.EntireColumn.Insert();
Range rgSum2 = UsedArea.Cells[i,cellnum+1];
Range RgFinal = sheet.get_Range(rgSum, rgSum2);
RgFinal.Merge();

How to extract cells of a particular colour in pivot table?

I have applied a conditional formatting to a column of a pivot table in Excel, which automatically colours the cells greater than a particular value in red.
I also want to extract those red cells separately to a specific area in the same Sheet.
Can it be done by using macros or an 'if' condition would help here?
What is your conditional formatting parameter? You could use the same logic programmatically. Alternatively you could write the code to check the color of the cell.
'specify the cell you want the color for
x = InputBox("what cell")
Range("a1") = Range(x).Interior.Color
If Range(x).Interior.Color = Range("a1") Then
MsgBox "yes the color is right"
End If
for a pivot table example
'where cell is something your looking for that is in the cell
ActiveSheet.PivotTables("PivotTable1").PivotSelect "'cell'", xlDataAndLabel + xlFirstRow, True
x = Selection.Interior.Color

Resources