write data to excel file in java using apache POI -
i having problem here while writing data excel sheet using apache poi. want call function several times write data excel sheet have created.
is there way can move pointer next row every time @ end of function???
my code given below...
public static void excellog(string filename , string message) { string dest="d:\\testexcel.xls"; hssfworkbook myworkbook = new hssfworkbook(); hssfsheet mysheet = myworkbook.createsheet(); hssfrow myrow = null; hssfcell mycell = null; string exceldata [][] = new string [1][2]; exceldata[0][0]=filename; exceldata[0][1]=message; myrow = mysheet.createrow(rownum); (int cellnum = 0; cellnum < 2 ; cellnum++){ mycell = myrow.createcell((short) cellnum); mycell.setcellvalue(exceldata[rownum][cellnum]); } rownum++; try{ fileoutputstream out = new fileoutputstream(dest); myworkbook.write(out); out.close(); }catch(exception e){ e.printstacktrace(); } }
please me in this. thank :)
this modified code it's support dynamic column size. idea create row once , reuse if row exists.
import java.io.fileoutputstream; 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; public class poisample { private static string dest = "d:\\testexcel.xls"; private static hssfworkbook myworkbook = new hssfworkbook(); private static hssfsheet mysheet = myworkbook.createsheet(); private static void excellog(int row, int col, string value) { hssfrow myrow = mysheet.getrow(row); if (myrow == null) myrow = mysheet.createrow(row); hssfcell mycell = myrow.createcell(col); mycell.setcellvalue(value); } public static void main(string[] args) { int numcol = 10; // assume 10 cols (int = 0; < 10; i++) { (int j = 0; j < numcol; j++) { excellog(i, j, "row : " + + ", cell : " + j); } } try { fileoutputstream out = new fileoutputstream(dest); myworkbook.write(out); out.close(); } catch (exception e) { e.printstacktrace(); } }
}
Comments
Post a Comment