Apache POI XWPFTableRow remove column Word - java

I am trying to create a function in Java which removes a column from a Word table with header name "Serious". Please find this function below. When I run the code nothing is happening. Has anyone had any issues using the removeCell function?
public static void remCells(XWPFTable table) {
for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) {
XWPFTableRow row = table.getRow(rowIndex);
for (int colIndex = 0; colIndex < row.getTableCells().size()-1; colIndex++) {
XWPFTableCell cell = row.getCell(colIndex);
if(table.getRow(5).getCell(colIndex).getText().equals("Serious")) {
row.removeCell(colIndex);
}
}
}
}

Found solution here :
https://www.codota.com/code/java/methods/org.apache.poi.xwpf.usermodel.XWPFTableRow/removeCell
We have to do
row.getCtRow().removeTc(colIndex);
before
row.removeCell(colIndex);

Related

Is there a way to check excel sheet has rows null in POI java library?

I could not find any method to check POI sheet has null rows.
I have tried, Sheet.getLastRowNum(),Sheet.getFirstRowNum(),Sheet.rowIterator(), they all issue NPE.
I am really really confusing right now.
Note: But Sheet object itself is not null though.
int numberOfSheets = wb.getNumberOfSheets();
Sheet sheet;
int maxColumnsCount = 0, headerRowIndex = 0;
Row row;
Member member;
List<Member> members = new ArrayList<>();
for (int i = 0; i < numberOfSheets; i++) {
sheet = wb.getSheetAt(i);
//find the maximum cells in a row, it is assumed that max cells container is only valid data. Otherwise Header or Footer.
//THISE LINE('sheet.getPhysicalNumberOfRows()') GIVE ME NULL POINTER EXCEPTION.
for (int rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (maxColumnsCount < row.getPhysicalNumberOfCells()) {
maxColumnsCount = row.getPhysicalNumberOfCells();
headerRowIndex = rowIndex;
} else {
break;
}
}
for (int rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (headerRowIndex < rowIndex) {
member = parseMember(row, memberExcelImportType);
member.getTownship().setId(townshipId);
if (StringUtils.isNotEmpty(member.getFullName()))
members.add(member);
}
}
}
Sorry, here's the stack trace.
java.lang.NullPointerException
at org.apache.poi.xssf.usermodel.XSSFSheet.getPhysicalNumberOfRows(XSSFSheet.java:842)
at com.taraaung.tarabar.tarabarlibs.utility.ImportUtilPOI.readExcelFileUsingPOI(ImportUtilPOI.java:45)
at com.taraaung.tarabar.tarabarlibs.utility.ImportUtils.readExcelFile(ImportUtils.java:32)
at com.taraaung.taraserver.server.personExportImportUrl.PersonExportImportHttpServe.doPersonImport(PersonExportImportHttpServe.java:153)
at com.taraaung.taraserver.server.personExportImportUrl.PersonExportImportHttpServe.serve(PersonExportImportHttpServe.java:86)
at com.taraaung.taraserver.server.MyServer$ValidateSession.serve(MyServer.java:2608)
at com.taraaung.taraserver.server.MyServer.serve(MyServer.java:770)
at com.taraaung.taraserver.server.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:1557)
at com.taraaung.taraserver.server.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:1152)
at java.lang.Thread.run(Thread.java:745)
Umm, just found that the sheet which issues NPE is ChartSheet (i.e, it does not have any kind of rows), then I just check the instance of sheet and skip chart sheets. :D if (sheet instanceof XSSFChartSheet) {
continue;
}
'Thanks for the help'

Apache POI : how to manage getLastRowNum errors

I'm using Apache POI to work on an Excel file.
Most of the time it works fine, but sometimes, getLastRowNum() returns a wrong number which causes a null pointer Exception when I arrive at the end of the file.
I want to be sure everything works fine, even when there is something wrong with getLastRowNum().
for (int i = FIRST_ROW_TO_GET; i < sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
System.out.println(row.getCell(9));
}
I tried to use a if row.equals(null) but it doesn't change anything.
Any idea of what I can do?
You can't call row.equals(null) since row is null in this case. You can do the check this way:
for (int i = FIRST_ROW_TO_GET; i < sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
if (row != null){
System.out.println(row.getCell(9));
}
}
The below method will give the accurate last row number in excel, ignoring the blank cells :
int getRowCount(String sheetName) {
int number = 0;
int index = workbook.getSheetIndex(sheetName);
if (index == -1) {
return 0;
} else {
sheet = workbook.getSheetAt(index);
for(int i = 0; i < sheet.getLastRowNum(); i++) {
if(sheet.getRow(i)==null) {
sheet.shiftRows(i+1, sheet.getLastRowNum(), -1);
i--;
}
number = sheet.getLastRowNum() ;
}
}
return number;
}

Aspose cells to read table header row

How can i retrive excel table header row.
I able to retrive all rows except table header row.
Code
private static void printTableContent(final ListObject table) {
System.out.println(table.getShowHeaderRow());
Range range = table.getDataRange();
for (int row = 0; row < range.getRowCount(); row++) {
for (int column = 0; column < range.getColumnCount(); column++) {
System.out.print(range.get(row, column).getDisplayStringValue());
System.out.print("\t");
}
System.out.println();
}
}
ListObject.getDataRange() will only return the data rows.
To get the header row, use ListObject.getListColumns(). Add the following code in your method to print the list of header row.
for (int iColumn = 0 ; iColumn < table.getListColumns().getCount() ; iColumn++)
{
System.out.print(table.getListColumns().get(iColumn).getName());
}
System.out.println();
I work with Aspose as a Developer Evangelist.

Getting a nullPointerException in getting getLastCellNum() method in a blank Row, using apache poi

My scenario is, I have to quit the conversion of Excel to txt, when a blank row appears.
I have written the following code for it
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++)
{
Row row=sheet1.getRow(rowNum);
int lastColumn = row.getLastCellNum();
for (int cn = 0; cn < lastColumn; cn++)
{
Cell cell = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if(cell == null)
{
break;
}
switch(cell.getCellType())
{
//Remaining code for non-blank cells
}
}
}
The code works fine, but as soon as a blank row appears, a nullPointerException is thrown
in the getLastCellNum() method in line 4. Am I doing something wrong? Also I have set the missing cell policy for my workbook as
workbook1.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL);
It seems that this case may happen, if you want to find the lastCellNum of any null row, and the last row number of that sheet is more than the current row number.
For example, if total number of row is 10 in the sheet, but the 5th row is null. Null means not blank, but UN-initialized row. In that case Row row=sheet1.getRow(rowNum); will not show any error but row.getLastCellNum(); will show nullPointerException
To overcome this issue you need to check before getting last row number that the row should not be null.
Please check the following piece of code
int lastColumn=0;
for (int rowNum = 0; rowNum < rowEnd; rowNum++){
Row row=sheet1.getRow(rowNum);
if(row!=null)
lastColumn = row.getLastCellNum();
else
continue;
for (int cn = 0; cn < lastColumn; cn++){
Cell cell = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if(cell == null){
break;
}
switch(cell.getCellType()){
//Remaining code for non-blank cells
}
}
}

searchings rows from table

i want to select particular rows from the JTable which contains a particular string.. please help me for this..
Something like this will do the trick:
void selectMatchingRows(JTable table, String regex)
{
for (int row = 0; row < table.getModel().getRowCount(); row++)
{
for (int col = 0; col < table.getModel().getColumnCount(); col++)
{
if (table.getModel().getValueAt(row, col).toString().matches(regex))
{
table.getSelectionModel().setSelectionInterval(row, row);
}
}
}
}
Making sure the ListSelectionModel.selectionMode is MULTIPLE_INTERVAL_SELECTION.
Call JTable.getModel then just loop through using TableModel.getValueAt

Categories