print 内容

恩,我想应该是print excel 文件的内容的时候了!在read()方法中加入一下代码,并且给read()添加一个参数string tabe  print处所有的sql语句来

for(int rowflag=0;rowflag<rownum;rowflag++)

        {

        //enter into row loop

             HSSFRow row = sheet.getRow(rowflag);

             int cells = row.getPhysicalNumberOfCells();

             System.out.println("the total number of cell in this row"+cells);

             System.out.println("this ROW= " + row.getRowNum());

            

             //

             String cellV[]=new String[cells];

             for(int cellflag=0;cellflag<cells;cellflag++)

             {

             //enter into cell loop

                 

                HSSFCell  cell= row.getCell((short)cellflag);

                double    valuenum = 0;

                String    valuestr=null;

                String    type=null;

                // i got the code below from org.apache.poi.hssf.dev.hssf class

                // and modify some

                switch (cell.getCellType())

                {

                                case HSSFCell.CELL_TYPE_FORMULA :

                                    type = "FORMULA ";

                                    valuestr="null";

                                    break;

                                case HSSFCell.CELL_TYPE_NUMERIC :

                                    type = "NUMERIC";

                                     valuenum=cell.getNumericCellValue();

                                    break;

                                case HSSFCell.CELL_TYPE_STRING :

                                    type = "STRING";

                                    valuestr=cell.getStringCellValue();

                                    break;

                                default :

                                    type=null;

                 }

                 if(type!=null)

                 {

                    System.out.print("the type of this cell is==="+type);

                    if(valuestr!=null)

                    {

                      cellV[cellflag]=valuestr.trim();

                      System.out.println("the value of this cell is==="+valuestr);

                    }

                    else  

                    {

                      cellV[cellflag]=Double.toString(valuenum);

                      System.out.println("the value of this cell is==="+valuenum);

                    }

                 }

                 else

                 {

                      cellV[cellflag]="null";

                      System.out.println("erro in the file ,there is a null type cell :"+cellflag);

                 }

              }

              //here i will manipulate the 'insert' directive of database

              StringBuffer sql=new StringBuffer("insert into "+table+"  values (");

              for(int j=1;j<cells;j++)

              {

                   sql.append("'"+cellV[j-1]+"',");

             }

                      sql.append("'"+cellV[cells-1]+"')");

                      System.out.println("sql  "+"="+sql.toString());

             

             

        }

恩看来excel支持的数据类型有限,它支持四种,FORMULA ,NUMERIC,STRING,date。  我们只用到了三种,很显然我常常会在粘切时莫名其妙的在excel插入空列,很讨厌阿,还是加上type验证吧!

One thought on “print 内容”

Comments are closed.