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)
Related
I want to be able to programmatically filter a Calc DataPilot (pivot table) with C# or Java.
At the moment, I am able to do the below:
go to the sheet that contains the DataPilot
get a reference to the DataPilot
Read its rowfields
Get a reference to the rowfield of interest
Pending:
By default, all values under the row field are checked. From that list, I want to be uncheck all of them and keep only specific.
Is this doable programmatically? If yes, what is the missing code to achieve this?
Current code
XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)document;
XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
//mSheetName is the name of the sheet that has the DataPilot I want to filter
// get reference to the sheet that has my DataPilot
XSpreadsheet sheet = (XSpreadsheet)xSpreadsheets.getByName(mSheetName).Value;
//Get a reference to the DataPilot that I want to uncheck/check its row field values
XDataPilotTablesSupplier xSupplier = (XDataPilotTablesSupplier)sheet;
XDataPilotTables xSheetPilotTables = xSupplier.getDataPilotTables();
//mPivotTableName is the name of the DataPilot
uno.Any xDPTableObj = xSheetPilotTables.getByName(mPivotTableName);
XDataPilotTable xPilotTable = (XDataPilotTable)xDPTableObj.Value;
XDataPilotDescriptor xDPDesc = (XDataPilotDescriptor)xPilotTable;
// get available row fields
XNameAccess rowFields = (XNameAccess)xDPDesc.getRowFields();
//get the row field of interest
//mFieldName is the name of the row field
uno.Any xRowItemObj = rowFields.getByName(mFieldName);
Example: if we assume the xRowItemObj has 2 checked values (e.g. 1 and 2) how do I keep value 1 checked only?
So far, any research in forums and libreOffice Documentation did not return a result. I am not even sure if it is doable
Get the list of items from the row field. Here is a Basic example that checks even-numbered boxes.
oRowItem = oDPTable.getRowFields().getByIndex(0)
oItems = oRowItem.getItems()
For i = 0 To oItems.getCount() - 1
oItem = oItems.getByIndex(i)
If (i Mod 2 = 0) Then
oItem.setPropertyValue("IsHidden", False)
Else
oItem.setPropertyValue("IsHidden", True)
End If
Next
If you haven't yet, be sure to use an introspection tool such as MRI.
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
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
I want to insert a string to a Matrix of Cells in Excel, Using Apache POI, Here is the code :
for (int j=0;j<13;j++){
for(int i=6;i<30;i++)
{
XSSFRow row=sheet6.createRow(i);
cell0=row.createCell(j);
cell0.setCellValue("SampleRules_CI_01");
}
}
After executing my program, I get the column j=12 correctly filled, but the other columns (from 0 to 11) are still empty
You are replacing the row each time you get back around to the same value of i, when you call the createRow method. This has the effect of erasing whatever was on that row already, and only the last column doesn't get erased this way.
First, inside your i for loop, call the getRow method to see if it already exists. If it returns null, then the row doesn't exist yet and then you can call createRow.
XSSFRow row = sheet6.getRow(i);
if (row == null)
{
row = sheet6.createRow(i);
}
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);