get Excel Sheet Column Name of a Chosen Cell using Java - java

I've a problem with importing excel sheet data to work with Java. I've a Table that look likes the following.
Image | Name | Order
1011V | Holly | ON-10121
As you can see, I've column Headings ,
Image
Name
Order
I want to get the Column Name like A , B or C in Excel, given that I've provided the Column Headings ( Cell Values ) Image , Name or Order.
For Example Something like :- getExcelColumnName("Image") , getExcelColumnName()

Suppose you want to know header no of 5th Column then you have cell no 5. Take cell of row 1 and 5th Column by.
Cell cell = Sheet1.getRow(0).getCell(5);
now take a string
String cellNumber = cell.getReference();
It will give you E1. I have given you an idea of using cell.getReference(); you can use it by many ways to get the desired result.

I found the following API will work:
CellReference.convertNumToColString(5)
will return "F"

I got this answer usefull..
int lastcell=spreadsheet.getRow(0).getLastCellNum();
//Non empty Last cell Number or index return
for(int i=0;i<=lastcell;i++)
{
try
{
System.out.println(CellReference.convertNumToColString(i));
}catch(Exception e)
{}
}
static
Cell cell = spreadsheet.getRow(0).getCell(2);
tmp=64+cell.getColumnIndex();
//convert ascii code to character
System.out.println(Character.toString((char)tmp));
Visit Full Describe : Get columns' names in Excel file using Apache POI

Related

Apache POI: Create a cell with type number, but to be empty

I'm trying to create an excel document. What I need is to have a column with type number. The problem, which I have is that not every cell in this column has a value( some of them should be empty).
I have one method, which I call in order to create the cells. I have problem when from the methods parameters get "null", because the setCellValue(double value) can't take null as parameter. This is why I decided to check if it is null to put the cell type: empty.
enter image description here
enter image description here
At the end what worked for me is: I just put something to the cell if the value is not null. This worked for me.
private void createCell(String cellStyle, String cellValue){
cell = row.createCell(cellNumber++);
cell.setCellStyle(styles.get(cellStyle));
if (cellStyle.equals("numberWithDelimiters")) {
if(cellValue!=null){
cell.setCellValue(Integer.parseInt(cellValue));
}
}else if(cellStyle.equals("date")){
Date date=new Date(Long.parseLong(cellValue));
date.setHours(0);
cell.setCellValue(date);
}else{
cell.setCellValue(cellValue);
}
}
According to the documentation
XSSFCell cell = row.createCell(1);
cell.setCellStyle(Cell.CELL_TYPE_BLANK);
cell.setCellType(Cell.CELL_TYPE_NUMERIC );
#Mohit sharma answer is deprecated, for new POI API use:
cell.setCellType(CellType.BLANK);

Aspose Cells for Java: get original CSV row

I am using Aspose to read a CSV file.
I do not beforehand know the number of cells for each row of the file, but I will need to know it for further processing.
Unfortunately, I see no way to find out the number of cells in a CSV row.
Imagine the following row in the CSV file. It contains 7 cells, 4 of which are empty:
1,2,,4,,,
Using
row.iterator();
Aspose will only return 3 cells, as it ignores all empty cells.
As an alternative, I now do the following:
Cell lastCell = row.getLastCell();
int count = 0;
do {
cell = row.getCellOrNull(count);
String cellValue = cell == null ? "" : cell.getStringValueWithoutFormat();
//do something with the cell value...
count++;
} while (cell == null || ! lastCell.equals(cell));
This works better, as it returns the first 4 cells.
However, it still ignores the last 3 cells .
Is there any way to get information about the missing cells?
(It would be sufficient for me if Aspose could return the original Row as a String - I could then count the number of commas and find out the number of cells this way)
You may use Worksheet.getCells().getMaxDisplayRange() method to get the maximum display range.
Please consider this CSV. If you open it in MS-Excel and check the last cell, you will find it is Q2
Book1.csv
2,,,,1,,,,,,,,,,,,,
,,3,,,,
Aspose.Cells returns the same via the following code.
TxtLoadOptions opts = new TxtLoadOptions(LoadFormat.CSV);
Workbook wb = new Workbook("Book1.csv", opts);
Worksheet ws = wb.getWorksheets().get(0);
Range rng = ws.getCells().getMaxDisplayRange();
System.out.println(rng);
Here is the console output of the code.
Console Output
Aspose.Cells.Range [ Sheet1!A1:Q2 ]
Note: I am working as Developer Evangelist at Aspose

Get Cell by a Cell reference letter identifier with Apache POI

I am trying to get the Cell from the Row by a Cell reference and I have a problem. For example I have:
row.getCell(CellReference.convertColStringToIndex("B"));
That works fine if the Column index is 1, but if Column was deleted so the B Column index became 2 and the method: CellReference.convertColStringToIndex("B") is still converting it to 1 in which case I can't get my Column, I am getting null.
So the question is how can I get the Column from the Row depending on Cell identifier which is a letter?
Take a look at my answer to a previous question:
You probably want to use the CellReference utility class to help you out. It offers conversion between Excel style letter+number references, and POI style 0-based rows+columns. When using it, you can do something like:
Sheet sheet = workbook.getSheet("MyInterestingSheet");
CellReference ref = new CellReference("B12");
Row r = sheet.getRow(ref.getRow());
if (r != null) {
Cell c = r.getCell(ref.getCol());
}
That will let you find the cell at a given Excel-style reference (if it's defined, else null)

Writing to a particular cell location using Apache POI Excel

If I've got an list of parameters 'x,y,z' that aren't sorted, is there a straightforward way to write them to particular cells in an excel document created with POI, as though the first two parameters are X and Y coordinates?
For example, I have rows like:
10,4,100
Is it possible to write the value '100' in the cell at the 10th row, 4th column?
Looking at the documentation, it looks straightforward to iterate values into the next row, but I can't see any way of creating a fixed number of rows and columns and writing particular values to only certain cells.
Any advice or suggestions would be appreciated, thanks!
Sure, it's very easy, just remember that POI is 0 based not 1 based in addressing. Assuming you want to write to the 10th row, 4th column, you'd do something like
Row r = sheet.getRow(9); // 10-1
if (r == null) {
// First cell in the row, create
r = sheet.createRow(9);
}
Cell c = r.getCell(3); // 4-1
if (c == null) {
// New cell
c = r.createCell(3, Cell.CELL_TYPE_NUMERIC);
}
c.setCellValue(100);

How to add multiple different columns of excel using Apache POI & Java

I need to generate CodeKey that is combination of two columns of excel value and values in those columns can be Boolean, String, Numeric or combination of all of those.
Now I can go through if/else loop and check for every condition but what would be an efficient way of doing this.
For Example:
If i have ExCode = 7V and PrCode = A: then my CodeKey should be 7VA:
ExCode PrCode
6D: A:
6R TR
7V 6K
And all i want to do is generate CodeKey as 6D:A:, 6RTR and 7V6K respectively.
Don't want to do something like:
if(ExCodeCellValue.getCellType() == Cell.CELL_TYPE_STRING &&
PrCodeCellValue.getCellType() == Cell.CELL_TYPE_STRING){
System.out.println("Combined String Values: "+ExCodeCellValue.getStringValue()+PrCodeCellValue.getStringValue());
}
As there would be lots of if/else unnecessary stuffs to generate codeKey, any other efficient solution of doing this or is there any api in POI that would be useful for this case?
I think you should be able to use DataFormatter.formatCellValue(cell) which will give you a string that matches what Excel shows for a cell.
With that, you could would look something like (Assuming the ExCode is the 3rd column, PrCode in the 4th)
// Do this once
DataFormatter formatter = new DataFormatter();
// Once per row
for (Row row : sheet) {
String exCode = formatter.formatCellValue( row.getCell(2) );
String prCode = formatter.formatCellValue( row.getCell(3) );
Cell code = row.createCell(4, Cell.CELL_TYPE_STRING);
code.setCellValue(exCode + prCode);
}

Categories