How to Delete Rows from Microsoft Excel worksheet using POI HSSF Library

 

Hi,

I try to load excel sheet which is having Macro using HSSF libraries. Once load the worksheet I am updating some data into rows and columns. It works fine.

When I remove a row from the spreadsheet using Apache POI Library the cell values in the row get clear and the row still exist with empty cells.

Anybody help me to delete rows from worksheet using Apache POI library.

HSSFWorkbook workbook = null;

InputputStream istream= new FileInputStream ("D://Review_Macros.xls");

POIFSFileSystem inputStream = new POIFSFileSystem(new BufferedInputStream(istream));
workbook = new HSSFWorkbook(istream);

HSSFRow row = null;
row= sheet.getRow(11);

//The below method removes only cell values not row.
sheet.removeRow(row);

FileOutputStream out = new FileOutputStream ("D:/Review_New.xls");
workbook.write(out);

Not knowing the script language

But from my limited knowledge

HSSFRow row = null;

sets the contents (values) of the row to null.

--
ɐ‾nsǝɹ Just one click away from the end of the Internet

selected row needs to be deleted

Thanks for your comment.

sheet.removeRow(4) - this command removes row values and row will be empty.

I need to delete the selected row from the worksheet instead of remaining the empty row.

selected row needs to be deleted

Just click on the line number in the left column and then delete the row it will remove the row and all info.

--
johnm405 660 & MSS&T

shiftRows() ?

Would following your removeRow() with a shiftRows() work?

shiftRows() ?

Not that I am aware of. When you delete a row just right click and selcet delete. It doesn't shift rows it takes the row out. I don't use the apatche thing as I have never heard of it. I just use the commands within excel.

--
johnm405 660 & MSS&T

He's trying to use a script

johnm405 wrote:

Not that I am aware of. When you delete a row just right click and selcet delete. It doesn't shift rows it takes the row out. I don't use the apatche thing as I have never heard of it. I just use the commands within excel.

He's using a scripting language and not actually in the file but trying to automate the process so he doesn't have to open the file and select lines to be deleted.

--
ɐ‾nsǝɹ Just one click away from the end of the Internet

He's trying to use a script

to show my dummy side, how do you remove lines in a file if you don't have the file open to see what lines need to be removed. confused

--
johnm405 660 & MSS&T

When I get stuck scripting in Excel

I record a macro and look at the resulting code:

1. Start recording
2. Left-click to highlight Row 11
3. Right-click and delete Row 11
4. Stop recording

The recorded macro shows:

Rows("11:11").Select
Selection.Delete Shift:=xlUp

Let us know if this helps.

--
Netpilot - Garmin 885T - Exploring 70+ screens in Test Mode

Using a script

The two lines containing "inputStream" programmatically open the file.

a_user meant so he doesn't have to manually open the file.

--
Netpilot - Garmin 885T - Exploring 70+ screens in Test Mode

I need to delete rows using java based Apache API

Thanks for your comments.

I want to delete rows from excel worksheet by writing pure java based Apache POI library.

how to delete row instead removing the content from Excel file using Apache POI.
HSSFSheet sheet
row= sheet.removeRow(3);

The above method removes the values from 3rd row instead deleting the rows.
Please some can help me to how to remove row from excel using java based Apache POI library.

Why is this here?

I understood this was a Points Of Interest forum.

What possible application can interfacing Excel with Java have to do with topics here?

--
Zumo 550 & Zumo 665 My alarm clock is sunshine on chrome.