Read and write Excel

Source: Internet
Author: User



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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.