Delete Excel Blank Columns permanently using Java -
my excel sheet this:
column index: [0] [1] [2] [3] column name : name password address xyz 111 delhi abc 222 chennai
here column @ index[2] cell type blank.i want delete columns of cell_type_blank.
i able find columns , index numbers cell_type_blank, don't find method delete columns permanently excel sheet.how code meet requirement?
my output excel sheet should below after deleting cell_type_blank columns.
column index: [0] [1] [2] column name : name password address xyz 111 delhi abc 222 chennai import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.openxml4j.exceptions.invalidformatexception; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workbookfactory; import org.apache.poi.ss.usermodel.workbook; public class deletecolumn { public static void main(string[] args) throws invalidformatexception, filenotfoundexception, ioexception { workbook wb = workbookfactory.create(new fileinputstream("delete_columns.xls")); sheet sheet = wb.getsheetat(0); cell cell; column column; row row= sheet.getrow(0); int numxolumns = row.getlastcellnum(); for(int col=0; col< numxolumns; col++) { cell = row.getcell(col); int type = cell.getcelltype(); if (type == hssfcell.cell_type_blank) { system.out.println("[" + cell.getrowindex() + ", "+ cell.getcolumnindex() + "] = blank cell"+ cell.tostring()); int delete_column_index=cell.getcolumnindex(); } } fileoutputstream fileout = new fileoutputstream("output_excel.xls"); wb.write(fileout); fileout.close(); } }
the code implemented delete rows is
public static void removerow() { try { int getlastcell=row.getlastcellnum()-1; int lastindex = sheet.getlastrownum(); (int i=0; i<=lastindex; i++) { row=sheet.getrow(i); if(row.getcell(getlastcell)!=null && ((row.getcell(getlastcell).tostring().equalsignorecase("not valid app type".trim()))|| (row.getcell(getlastcell).tostring().equalsignorecase("not applicable".trim())))) { sheet.shiftrows(i+1, lastindex, -1); i--; } } } catch(nullpointerexception e) { e.printstacktrace(); } catch(exception e) { e.printstacktrace(); } }
how delete columns?
you cannot delete column in 1 shot. need delete 1 cell on each row.
Comments
Post a Comment