java - Creating a copy of an excel file is not working as expected -
i want copy entire content of 1 excel(.xls) after replacing " , '. however, code creating new excel copying last column(in case, 7th column). please advice going wrong code...
public class rename {
static hssfrow row_read = null; static hssfrow row_write = null; static cell cell; static fileoutputstream output = null; static hssfworkbook workbook_read = null; static hssfworkbook workbook_write = null; static hssfsheet sheet_read = null; static hssfsheet sheet_write = null; public static void removechar() { try{ fileinputstream input = new fileinputstream("inputpath//test_input.xls"); workbook_read = new hssfworkbook(input); sheet_read = workbook_read.getsheet("report"); workbook_write = new hssfworkbook(); sheet_write = workbook_write.createsheet("test"); dataformatter formatter = new dataformatter(); int rowcount = sheet_read.getlastrownum(); system.out.println(rowcount); for(int rownum = 0; rownum<=rowcount; rownum++) { for(int cellnum = 0; cellnum<=7; cellnum++) { //cell cell = null; row_read=sheet_read.getrow(rownum); row_write = sheet_write.createrow(rownum); string temp = formatter.formatcellvalue(row_read.getcell(cellnum)); //system.out.println(temp); string temp1 = temp.replaceall("\'", ""); string temp2 = temp1.replaceall("\"", ""); system.out.println(temp2); cell = row_write.createcell(cellnum); cell.setcellvalue(temp2); } } output = new fileoutputstream("outputpath\\test_output.xls"); workbook_write.write(output); } catch(exception e) { e.printstacktrace(); } } public static void main(string[] argv) throws ioexception { rename.removechar(); }
}
thank
the issue following lines..
row_read=sheet_read.getrow(rownum); row_write = sheet_write.createrow(rownum);
it need outside cell loop (for(int cellnum = 0; cellnum<=7; cellnum++)
)
it should like.
(int rownum = 0; rownum <= rowcount; rownum++) { row_read = sheet_read.getrow(rownum); row_write = sheet_write.createrow(rownum); (int cellnum = 0; cellnum <= 7; cellnum++) { // cell cell = null; string temp = formatter.formatcellvalue(row_read.getcell(cellnum));
also might need change import static cell cell;
import hssfcell cell;
here complete code.
import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.ss.usermodel.dataformatter; public class rename { static hssfrow row_read = null; static hssfrow row_write = null; static hssfcell cell; static fileoutputstream output = null; static hssfworkbook workbook_read = null; static hssfworkbook workbook_write = null; static hssfsheet sheet_read = null; static hssfsheet sheet_write = null; public static void removechar() { try { fileinputstream input = new fileinputstream("d://test_input.xls"); workbook_read = new hssfworkbook(input); sheet_read = workbook_read.getsheet("report"); workbook_write = new hssfworkbook(); sheet_write = workbook_write.createsheet("test"); dataformatter formatter = new dataformatter(); int rowcount = sheet_read.getlastrownum(); system.out.println(rowcount); (int rownum = 0; rownum <= rowcount; rownum++) { row_read = sheet_read.getrow(rownum); row_write = sheet_write.createrow(rownum); (int cellnum = 0; cellnum <= 7; cellnum++) { // cell cell = null; string temp = formatter.formatcellvalue(row_read.getcell(cellnum)); // system.out.println(temp); string temp1 = temp.replaceall("\'", ""); string temp2 = temp1.replaceall("\"", ""); system.out.println(temp2); cell = row_write.createcell(cellnum); cell.setcellvalue(temp2); } } output = new fileoutputstream("d://test_output.xls"); workbook_write.write(output); } catch (exception e) { e.printstacktrace(); } } public static void main(string[] argv) throws ioexception { rename.removechar(); } }
Comments
Post a Comment