java - How to copy a row from existing excel sheet to a new excel sheet using Apache POI? -
i want compare 2 excel sheets , find if row differs, if want copy single row differs excel new row in new excel. below code snippet took net , tried problem if copy 10th row of existing excel(for example) first row of new excel, 1st row copied in new excel remaining rows first excel copied new one. think problem way write new excel [workbook.write(out)] please help!! in advance!!
public class rowcopy { public static void main(string[] args) throws exception{ hssfworkbook workbook = new hssfworkbook(new fileinputstream("c:/input.xls")); hssfsheet sheet = workbook.getsheet("sheet1"); copyrow(workbook, sheet, 0, 1); fileoutputstream out = new fileoutputstream("c:/output.xls"); workbook.write(out); out.close(); } private static void copyrow(hssfworkbook workbook, hssfsheet worksheet, int sourcerownum, int destinationrownum) { // source / new row hssfrow newrow = worksheet.getrow(destinationrownum); hssfrow sourcerow = worksheet.getrow(sourcerownum); // if row exist in destination, push down rows 1 else create new row if (newrow != null) { worksheet.shiftrows(destinationrownum, worksheet.getlastrownum(), 1); } else { newrow = worksheet.createrow(destinationrownum); } // loop through source columns add new row (int = 0; < sourcerow.getlastcellnum(); i++) { // grab copy of old/new cell hssfcell oldcell = sourcerow.getcell(i); hssfcell newcell = newrow.createcell(i); // if old cell null jump next cell if (oldcell == null) { newcell = null; continue; } // copy style old cell , apply new cell hssfcellstyle newcellstyle = workbook.createcellstyle(); newcellstyle.clonestylefrom(oldcell.getcellstyle()); ; newcell.setcellstyle(newcellstyle); // if there cell comment, copy if (oldcell.getcellcomment() != null) { newcell.setcellcomment(oldcell.getcellcomment()); } // if there cell hyperlink, copy if (oldcell.gethyperlink() != null) { newcell.sethyperlink(oldcell.gethyperlink()); } // set cell data type newcell.setcelltype(oldcell.getcelltype()); // set cell data value switch (oldcell.getcelltype()) { case cell.cell_type_blank: newcell.setcellvalue(oldcell.getstringcellvalue()); break; case cell.cell_type_boolean: newcell.setcellvalue(oldcell.getbooleancellvalue()); break; case cell.cell_type_error: newcell.setcellerrorvalue(oldcell.geterrorcellvalue()); break; case cell.cell_type_formula: newcell.setcellformula(oldcell.getcellformula()); break; case cell.cell_type_numeric: newcell.setcellvalue(oldcell.getnumericcellvalue()); break; case cell.cell_type_string: newcell.setcellvalue(oldcell.getrichstringcellvalue()); break; } } }
just modify copyrow method, add parameter hssfsheet resultsheet modify newrow variable in method resultsheet
private static void copyrow(hssfworkbook workbook, hssfsheet worksheet, hssfsheet resultsheet, int sourcerownum, int destinationrownum) { // source / new row hssfrow newrow = resultsheet.getrow(destinationrownum); hssfrow sourcerow = worksheet.getrow(sourcerownum); take resultsheet destination "output.xls"
Comments
Post a Comment