Preparation: Need to reference
Poi-3.10-final-sources.jar
Poi-3.10-final.jar
Read EXCEL
private void Readexcel () throws IOException {FileInputStream stream = new FileInputStream ("C:\\1.xls"); Hssfworkbook workbook = new Hssfworkbook (stream); Hssfsheet sheet = workbook.getsheetat (0);//private static final String Cell_reference_file = "B6"; Cellreference filereference = new Cellreference (cell_reference_file); Row row; Cell cell;iterator<row> rowiterator = Sheet.iterator (); while (Rowiterator.hasnext ()) {Row = Rowiterator.next (); int currentrow = Row.getrownum (); if (CurrentRow = = Filereference.getrow ()) {cell = Row.getcell (Filereference.getcol ()); System.out.println ("Data is" + cell.getstringcellvalue ());}} Stream.Close ();}
Write EXCEL
private void Writeexcel (arraylist<string> list) throws Exception {String filename= "./" +projectname+ "_file.xls"; File File = new file (fileName), if (!file.exists ()) file.createnewfile (); FileOutputStream out = new FileOutputStream (file, false); CellStyle style = null; CellStyle style1 = null; Hssfworkbook wb = new Hssfworkbook (); Sheet Sheet1 = Wb.createsheet ("TestResults"); style = Wb.createcellstyle (); Font Font=createfont (WB, "Calibri", False, (short) 11); Style.setfont (font); style.setalignment (Cellstyle.align_left); Style.setwraptext (true); Style1 = Wb.createcellstyle (); Hssfcolor color = Wb.getcustompalette (). Findcolor ((Byte) 255, (byte) 255, (byte) 0); Font Font1=createfont (WB, "Calibri", True, (short) one); Style1.setfont (font1); Style1.setfillpattern ((short) 1); Style1.setalignment (Cellstyle.align_left); Style1.setwraptext (true); Style1.setfillforegroundcolor ( Color.getindex ()); for (int i = 0; i < list.size (); I+ +) {Row rows = sheet1.createrow (i + 1); Cell cell = Rows.createcell (0); if (List.get (i). EndsWith (". C") | | List.get (i). EndsWith (". H")) {Cell.setcellstyle (style);} Else{cell.setcellstyle (Style1);} Cell.setcellvalue (List.get (i)); Sheet1.setcolumnwidth (0, 1), Wb.write (out), Out.close (), Sheet1.setcolumnwidth (); Private Font CreateFont (Hssfworkbook wb, String fontname, boolean isbold,short height) {font font = Null;font = Wb.createf Ont (); Font.setfontname (FontName); if (isbold) {font.setboldweight (hssffont.boldweight_bold);} Font.setfontheightinpoints (height); return font;}
Read and write Excel