JavaBean Generating Excel

Source: Internet
Author: User

You must use more than 2007 versions. xlsx file name

POI Bag

Http://poi.apache.org/download.html

Package Com.xls;import Java.beans.introspectionexception;import Java.beans.propertydescriptor;import java.io.File; Import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.lang.reflect.field;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.Method; Import Java.util.list;import Org.apache.poi.ss.usermodel.cell;import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.xssf.usermodel.xssfcellstyle;import Org.apache.poi.xssf.usermodel.xssfcolor;import Org.apache.poi.xssf.usermodel.xssfsheet;import Org.apache.poi.xssf.usermodel.xssfworkbook;public Class Dealxlsimpl {/** @author JOHNGF * Pass in a List and write it to the specified Excel file */public boolean generatexls (list<?> objs,file file) {if ( Objs.size () <=0) {return false;} else if (!file.exists ()) {try {file.createnewfile ()} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} else if (file.exists ()) {file.delete (); try {file.createnewfile ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}      Create Blank workbookclass<?> clazz = objs.get (0). GetClass ();       Xssfworkbook workbook = new Xssfworkbook ();      int number = Objs.size ()/104857 + 1;      System.out.println ("The number of worksheets required is" +number);        for (int i=0;i<number;i++) {Xssfsheet sheet = workbook.createsheet ("sheet" +i);      field[] fields = Objs.get (0). GetClass (). Getdeclaredfields ();     System.out.println ("Number of columns is" +fields.length);      String[] titles = GetTitle (objs.get (0));      Write header row Row row = Sheet.createrow (0);      Xssfcellstyle style = Workbook.createcellstyle ();      Style.setfillbackgroundcolor (New Xssfcolor (New byte[]{100,25,13}));      for (Int J =0; j<fields.length;j++) {Cell c = Row.createcell (j);      C.setcellvalue (Fields[j].getname ());      C.setcellstyle (style);     }//title has been written to System.out.println ("i=" +i+ "Number of objects" + objs.size ());      System.out.println (Objs.size ()-(104587 * i)); for (int m = 104587 * i; m < Objs.size ()-(104587 * i);     m++) {row = Sheet.createrow (M + 1);     SYSTEM.OUT.PRINTLN ("Created rows" + (m+1));  for (int n =0; n < fields.length;n++) {try {propertydescriptor PD = new PropertyDescriptor (Fields[n].getname (), Clazz); method = Pd.getreadmethod (); Row.createcell (n). Setcellvalue (Method.invoke (Objs.get (m), null). ToString ()); catch (Introspectionexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IllegalArgumentException e) {//Todo auto-generated cat CH Blocke.printstacktrace ();}              catch (InvocationTargetException e) {//TODO auto-generated catch Blocke.printstacktrace ();} }}}//create file system using specific name FileOutputStream Out;try    {out = new FileOutputStream (file), Workbook.write (out); Workbook.close ();    Out.close (); } catch (FilenotfoundexCeption e) {//TODO auto-generated catch Blocke.printstacktrace ();} Write operation workbook using file out object catch (IOException e) {//TODO auto-generated catch Blocke.printstacktra           CE ();} return false;} public boolean Parsexls (file file) {//TODO auto-generated method Stubreturn false;}}

JavaBean Generating Excel

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.