Apache Poi excel remove blank rows - java

I have an excel file with 3000 rows. I remove the 2000 (with ms excel app), but when i call the sheet.getLastRowNum() from code , it gives me 3000 (instead of 1000).. How can i remove the blank rows?
I tried the code from here but it doesn't works....

There are two ways for it:
1.) Without code:
Copy the content of your excel and paste it in a new excel, and later rename is as required.
2.) With code(I did not find any functions for it so I created my own function):
You need to check each of the cells for any type of blank/empty string/null kind of things.
Before processing the row(I am expecting you are processing row wise also I am using org.apache.poi.xssf.usermodel.XSSFRow), put a if check, and check for this method's return type in the if(condition), if it is true that means the row(XSSFRow) has some value other wise move the iterator to next row
public boolean containsValue(XSSFRow row, int fcell, int lcell)
{
boolean flag = false;
for (int i = fcell; i < lcell; i++) {
if (StringUtils.isEmpty(String.valueOf(row.getCell(i))) == true ||
StringUtils.isWhitespace(String.valueOf(row.getCell(i))) == true ||
StringUtils.isBlank(String.valueOf(row.getCell(i))) == true ||
String.valueOf(row.getCell(i)).length() == 0 ||
row.getCell(i) == null) {}
else {
flag = true;
}
}
return flag;
}
So finally your processing method will look like
.
.
.
int fcell = row.getFirstCellNum();// first cell number of excel
int lcell = row.getLastCellNum(); //last cell number of excel
while (rows.hasNext()) {
row = (XSSFRow) rows.next();//increment the row iterator
if(containsValue(row, fcell, lcell) == true){
.
.
..//processing
.
.
}
}
Hope this will help. :)

I haven't found any solution on how to easily get the "real" number of rows but I've found a solution to remove such rows which might be useful to someone who's tackling similar issue. See bellow.
I've searched a bit and found this solution
All it does is it deletes those empty rows from the bottom which might be exactly what you want.

As per my understanding for deleting rows you Must have selected all the cells and pressed Delete button. If I am right then you have deleted the rows by wrong way. By this way the cells become blank not deleted so the rows actually contain cells with blank values and that is why get included in the row count.
The correct way to do this is select the row from the left of its first cell where row numbers are appearing. Clicking there on row numbers will select the complete row. Select all required rows with the help of shift key. Now right click and then select delete.
This may be helpful for you.

remove rows/columns by poi api
transfer xls to csv
transfer csv to xls
hope this will help you

Related

how to get the next cell in the same row when using poi

I am using poi(v4.0.0) to import the excel document. But when I tried to get the next cell carModelCell, it always return null, this is my Java 8 code looks like:
public void verifyCar(Cell cell, int relativeRowIndex, Head head) {
if (cell.getRowIndex() > 0 && head.getFieldName().equals("car")) {
if (StringUtils.isBlank(cell.getStringCellValue())|| cell.getStringCellValue().equals("无车")) {
return;
}
Cell carModelCell = cell.getRow().getCell(cell.getColumnIndex() + 1);
if (carModelCell == null || StringUtils.isBlank(carModelCell.getStringCellValue())) {
SparkUserParseResult result = new SparkUserParseResult();
result.setSuccess(false);
UploadSparkUserDataListener.parseSuccess.set(result);
return;
}
}
}
I am tried to get row from Cell, and get the next cell value with the same row and do some check, but the next cell carModelCell always return null. I have already sure the next cell of current row have a value. why would this happen? what should I do to fix this problem? This code block was in CellStyleWriteHandler which extend AbstractCellStyleStrategy in easy excel (version 2.2.11):
public class CellStyleWriteHandler extends AbstractCellStyleStrategy {
#Override
protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
impl(cell, head, relativeRowIndex);
}
}
I tried to get the last index num was 14, the current column index number was 13. the total column of my imported excel was 24, seems the easy excel did not pass the full column, is it possible to fix this problem? How to get the next cell of current row?
i also user poi to parse excel, i think problem in this line:
Cell carModelCell = cell.getRow().getCell(cell.getColumnIndex() + 1);
code above is error, becase cell can get from row,like:
Cell cell = sheetColumnRow.getRow()
one raw can cantain many Cell,but you can not get complete row from Cell, Parse row Cell value can not reverse; wish help you;

Get cell by its content

Is there a way to get the cell object or coordinate by the data the cell contains?
For example if the cell with coordinates (1;5) contains the string "FINDME", i'd like to do something like Workbook.GetCellByData("FINDME") and it should return the Cell object or (1;5).
I have found a code snippet on the Apache POI website that could be useful. I could just read the whole workbook and find the data with an IF-statement, but that's kind of dirty...
EDIT:
I have coded the "dirty" solution as follows:
public Cell getCellByContent(String data) {
for (Row row : wb.getSheetAt(0)) {
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING){
System.out.println(String.format("Found String type at (%s,%s) and read: %s", row.getRowNum(), cell.getColumnIndex(), cell.getStringCellValue()));
if (cell.getStringCellValue() == data) { //HERE
return cell; //HERE
} //HERE
}
}
}
System.out.println("Can't find it bruh!");
return null;
For some reason it fails at the if-statement. Id like to get the Cell with the content "%title%".
Output:
Found String type at (0,0) and read: %title% <------ IT'S RIGHT HERE!
Found String type at (2,0) and read: Test Information
...
Can't find it bruh!
Does someone have an idea why this is not working?
To fix the dirty solution replace
if (cell.getStringCellValue() == data)
with
if (cell.getStringCellValue().equals(data))
I think I can help you. you just make two for() loops for rows and columns and then type Workbook.getCellValue(i,j) (i is the number of the row and j is the number of the column

Formulas are lost when updating a row

I am using Google SpreadSheet API in Java to read and update a Spreadsheet. Let's say I have few columns named A and B.
A contains a formula.
B is pure text.
When I cycle through the SpreadSheet to update some specific rows:
URL listFeedUrl = new URI(worksheet.getListFeedUrl().toString() + "?sq=somefield=" + URLEncoder.encode("\"" + somevalue+ "\"").toString()).toURL();
ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);
for (ListEntry row : listFeed.getEntries()) {
if (something.compareTo(somethingelse) == 0) {
row.getCustomElements().setValueLocal("B", request.getParameter("B"));
row.update();
}
}
The formula in the column A is lost. Only the result of the formula is kept. I guess it has something to do with the update() method but it looks like every row that I read looses the formula, not just the one where I am executing the update. What can I do in order to preserve the formula? I am not even reading/editing that cell... Thanks.
EDIT: As indicated in the documentation the list-based feed does not handle formulas but the thing is, I am not reading and/or modifying the cell that contains a formula...
You could try saving the formula in an array first, then assigning it back to the sheet.
function myFunction() {
sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
range = 'A1:A5';
myFormulas = sheet.getRange(range).getFormulas();
sheet.getRange(range).setFormulas(myFormulas);
}

POI says Cell is empty but cell has a value

I'm opening a Excel (xls) file in my Java Application with POI.
There are 30 Lines in this Excelfile.
I need to get the Value at ColumnIndex 9.
My code:
Workbook wb;
wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
if (row.getLastCellNum() >= 6) {
for (Cell cell : row) {
if(cell.getColumnIndex == 9) {
//do something
}
}
}
}
Every Row in Excel has Values in Columns 1-14.
My problem is, only some Values are recognized. I wrote the same value in every cell in ColumnIndex 9 (10th Column in my Excel sheet), but the Problem is still the same.
What could cause this problem?
Make sure you set the same Date format for all cells in column (select column and set format explicity) And i belive using DataUtil class to get data is more appropriate, than call cell.getDateCellValue().
POI uses 0 based counting for columns. So, if you want the 9th Column, you need to fetch the cell with index 8, not 9. It looks like you're checking for column with index 9, so are one column out.
If you're not sure about 0 based indexing, then the safest thing is to use the CellReference class to help you. This will translate between Excel style references, eg A1, and POI style 0-based offsets eg 0,0. Use something like:
CellReference ref = new CellReference("I10");
Row r = sheet.getRow(ref.getRow());
if (r == null) {
// That row is empty
} else {
Cell c = r.getCell(ref.getCol());
// c is now the cell at I10
}
Seems to be a Problem with the excel document(s).
Converting them to csv and then back to xls solves the problem.

How can Selenium RC process dynamic rows?

I have a table similar to the following:
Part #
Price
Status
1st Part #
$1.00
OK
2nd Part #
$2.00
Discontinued
Nth Part #
$N.00
Reordered
My java code will be looking for the status of "Nth Part #" where I have no idea how big the table is, how many columns it has, and no idea what N is (until run time). In Ruby/WATIR, I would have used the table's id to grab it's HTML, and then used Ruby to iterate over the rows until the part # matched, and then check that row's corresponding status in the Status column (whichever column that might be, but it's set in the hd header's row).
Selenium's standard table lookup function selenium.getTable("table.1.2") only works for static tables that contain the same contents for each test. The overkill selenium.get_html_source is a waste since selenium knows how to find the table already, plus then I have to parse the entire web page.
Any ideas on how I can grab the html of the table, and what would be the best way to iterate over the rows and/or columns?
Thanks in advance.
The easiest thing to do would be to use getTable like this
selenium.getTable("table." + (1 + n) + ".3")
to get the "Status" cell for the nth row if you know what n will be at runtime.
If you are trying to iterate over all of the rows in the table, you could do something like this
try {
for(int n = 1; true; n++) {
String cellContents = selenium.getTable("table." + n + ".3");
//do something with n
}
}
catch {
//handle end of table
}
or, alternatively
final int rowCount = (int)selenium.getXPathCount("id('table')/tbody/tr");
for(int n = 1; n < rowCount; n++) {
String cellContents = selenium.getTable("table." + n + ".3");
}
Remember that in getTable(locator.row.column), row and column start at 1.
Not exactly what you're asking for, but I solved a similar problem by assigning the unique id (part number it sounds like in your case) to be the html id of the tr. Then I used the Selenium xpath locators to get the row and columns I needed for my test.

Categories

Resources