java - I tried to read string from file and store it in Excel sheet -
this have tried
public class uniqueuser { static hssfworkbook hwb=new hssfworkbook(); static hssfsheet sheet = hwb.createsheet("new sheet"); public static void main(string[]args) throws ioexception { hssfrow row; hashset<string> names = new hashset<>(); bufferedreader br = new bufferedreader (new filereader("sample.log")); printstream out=new printstream("d:/excel.xls"); string str=null; while((str=br.readline())!=null) { if(str.contains("fltr")) { string user=str.substring(97, 135); names.add(user); hssfrow row1 = sheet.createrow((short)count); } } iterator itr=names.iterator(); while(itr.hasnext()) { out.println(itr.next()); } } }
this program storing values excel sheet when read same file using following program, getting exception , errors..
public class country1 { private string name; private string shortcode; public country1(string n, string c) { this.name=n; this.shortcode=c; } public void country(string name2, string shortcode2) { // todo auto-generated constructor stub } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getshortcode() { return shortcode; } public void setshortcode(string shortcode) { this.shortcode = shortcode; } @override public string tostring() { return name + "::" + shortcode; } } public class readexcel { public static list<country1> readexceldata(string filename) { list<country1> countrieslist = new arraylist<country1>(); try { //create input stream xlsx/xls file fileinputstream fis = new fileinputstream(filename); //create workbook instance xlsx/xls file input stream workbook workbook = null; if(filename.tolowercase().endswith("xlsx")) { workbook = new xssfworkbook(fis); } else if(filename.tolowercase().endswith("xls")) { workbook = new hssfworkbook(fis); } //get number of sheets in xlsx file int numberofsheets = workbook.getnumberofsheets(); //loop through each of sheets for(int i=0; < numberofsheets; i++) { //get nth sheet workbook sheet sheet = workbook.getsheetat(i); //every sheet has rows, iterate on them iterator<row> rowiterator = sheet.iterator(); while (rowiterator.hasnext()) { string name = ""; string shortcode = ""; //get row object row row = rowiterator.next(); //every row has columns, column iterator , iterate on them iterator<cell> celliterator = row.celliterator(); while (celliterator.hasnext()) { //get cell object cell cell = celliterator.next(); //check cell type , process accordingly switch(cell.getcelltype()) { case cell.cell_type_string: if(shortcode.equalsignorecase("")) { shortcode = cell.getstringcellvalue().trim(); } else if(name.equalsignorecase("")) { //2nd column name = cell.getstringcellvalue().trim(); } else { //random data, leave system.out.println("randomdata::"+cell.getstringcellvalue()); } break; case cell.cell_type_numeric: system.out.println("random data::"+cell.getnumericcellvalue()); } } //end of cell iterator country1 c = new country1(name, shortcode); countrieslist.add(c); } //end of rows iterator } //end of sheets loop //close file input stream fis.close(); } catch (ioexception e) { e.printstacktrace(); } return countrieslist; } public static void main(string args[]) { list<country1> list = readexceldata("d:\\excel.xls"); system.out.println("country list\n"+list); } }
i confused program correct , 1 wrong.. please me.. lot
exception below:
unable read entire header; 320 bytes read; expected 512 bytes @ org.apache.poi.poifs.storage.headerblock.alertshortread(headerblock.java:227) @ org.apache.poi.poifs.storage.headerblock.readfirst512(headerblock.java:208) @ org.apache.poi.poifs.storage.headerblock.<init>(headerblock.java:104) @ org.apache.poi.poifs.filesystem.poifsfilesystem.<init>(poifsfilesystem.java:128) @ org.apache.poi.hssf.usermodel.hssfworkbook.<init>(hssfworkbook.java:342) @ org.apache.poi.hssf.usermodel.hssfworkbook.<init>(hssfworkbook.java:323) @ com.unisys.readexcel.readexceldata(readexcel.java:32) @ com.unisys.readexcel.main(readexcel.java:97)
country list
[]
try change
iterator<row> rowiterator = sheet.iterator();
to
iterator<row> rowiterator = sheet.rowiterator()
also, when writing create rows, not write them
hssfrow row1 = sheet.createrow((short)count);
then create cell
row.createcell (0).setcellvalue (user);
update
just had @ code, why on earth doing
printstream out=new printstream("d:/excel.xls");
use poi api create xls.
Comments
Post a Comment