隨筆-179  評(píng)論-666  文章-29  trackbacks-0

          使用Windows操作系統(tǒng)的朋友對(duì)Excel(電子表格)一定不會(huì)陌生,但是要使用Java語(yǔ)言來(lái)操縱Excel文件并不是一件容易的事。在Web應(yīng)用日益盛行的今天,通過(guò)Web來(lái)操作Excel文件的需求越來(lái)越強(qiáng)烈,目前較為流行的操作是在 JSP或Servlet 中創(chuàng)建一個(gè)CSV (comma separated values)文件,并將這個(gè)文件以MIME,text/csv類(lèi)型返回給瀏覽器,接著瀏覽器調(diào)用Excel并且顯示CSV文件。這樣只是說(shuō)可以訪問(wèn)到Excel文件,但是還不能真正的操縱Excel 文件,本文將給大家一個(gè)驚喜,向大家介紹一個(gè)開(kāi)放源碼項(xiàng)目 Java Excel API,使用它大家就可以方便地操縱Excel文件了。

          總結(jié)了兩種主要文件格式操作實(shí)例

          一:java 對(duì).csv 文件格式的操作

            1/*
            2 * Created on 2007-11-21
            3 *
            4 * TODO To change the template for this generated file go to
            5 * Window - Preferences - Java - Code Style - Code Templates
            6 */

            7package com.job5156.xlstodb;
            8
            9import java.io.BufferedReader;
           10import java.io.FileReader;
           11import java.io.IOException;
           12import java.util.ArrayList;
           13import java.util.Iterator;
           14import java.util.List;
           15
           16/**
           17 * @author Alpha
           18 *  JAVA 操作 excel 中的 .csv文件格式
           19 */

           20public class CsvUtil {
           21
           22  private String filename = null;   
           23    
           24  private BufferedReader bufferedreader = null;   
           25    
           26  private List list =new ArrayList();   
           27    
           28  public CsvUtil() {   
           29    
           30  }

           31  
           32  public static void main(String[] args) throws IOException {
           33      CsvUtil test = new CsvUtil();
           34      test.run("D:/alpha/abc.csv");
           35  }
           
           36    
           37  public CsvUtil(String filename) throws IOException{   
           38      this.filename = filename; 
           39      bufferedreader = new BufferedReader(new FileReader(filename)); 
           40      String stemp; 
           41      while((stemp = bufferedreader.readLine()) != null)
           42          list.add(stemp); 
           43      }

           44  }
           
           45  
           46  public List getList() throws IOException 
           47      return list; 
           48  }

           49  
           50  public int getRowNum()
           51      return list.size(); 
           52  }

           53  
           54  public int getColNum()
           55      if(!list.toString().equals("[]")) 
           56          if(list.get(0).toString().contains(",")) 
           57              return list.get(0).toString().split(",").length; 
           58          }
          else if(list.get(0).toString().trim().length() != 0
           59              return 1
           60          }
          else
           61              return 0
           62          }
           
           63      }
          else
           64          return 0;
           65      }

           66  }

           67  
           68  public String getRow(int index) 
           69      if (this.list.size() != 0
           70          return (String) list.get(index); 
           71      else 
           72          return null
           73  }

           74  
           75  public String getCol(int index)
           76      if (this.getColNum() == 0)
           77          return null
           78      }
           
           79      StringBuffer scol = new StringBuffer(); 
           80      String temp = null
           81      int colnum = this.getColNum(); 
           82      if (colnum > 1)
           83          for (Iterator it = list.iterator(); it.hasNext();) 
           84              temp = it.next().toString(); 
           85              scol = scol.append(temp.split(",")[index] + ","); 
           86          }

           87      }
          else{
           88              for (Iterator it = list.iterator(); it.hasNext();) 
           89              temp = it.next().toString(); 
           90              scol = scol.append(temp + ","); 
           91          }

           92      }

           93      String str=new String(scol.toString()); 
           94      str = str.substring(0, str.length() - 1); 
           95      return str; 
           96  }
           
           97  
           98  public String getString(int row, int col) 
           99      String temp = null
          100      int colnum = this.getColNum(); 
          101      if(colnum > 1)
          102          temp = list.get(row).toString().split(",")[col]; 
          103      }
          else if(colnum == 1
          104          temp = list.get(row).toString(); 
          105      }
          else
          106          temp = null
          107      }
           
          108      return temp; 
          109  }
           
          110  
          111  public void CsvClose() throws IOException 
          112      this.bufferedreader.close(); 
          113  }
           
          114  
          115  public void run(String filename) throws IOException {
          116      CsvUtil cu = new CsvUtil(filename);
          117     /* List tt = cu.getList();
          118      for (Iterator itt = tt.iterator(); itt.hasNext();){ 
          119          System.out.println("==="+itt.next().toString());
          120      }*/

          121      for(int i=0;i<cu.getRowNum();i++){
          122          
          123          String name = cu.getString(i,0);//得到第i行.第一列的數(shù)據(jù).
          124          String email = cu.getString(i,1);;//得到第i行.第二列的數(shù)據(jù).
          125          String tel = cu.getString(i,2);; 
          126          String number = cu.getString(i,3);;
          127          
          128          System.out.println("===name:"+name);
          129          System.out.println("===email:"+email);
          130          System.out.println("===tel:"+tel);
          131          System.out.println("===number:"+number);
          132          System.out.println(" ");
          133      }

          134      
          135      /*System.out.println("aaa:"+cu.getRowNum());
          136      System.out.println("bbb:"+cu.getColNum());
          137      System.out.println("ccc:"+cu.getRow(0));
          138      System.out.println("ddd:"+cu.getCol(0));
          139      System.out.println("eee:"+cu.getString(0, 0));*/

          140      
          141      cu.CsvClose();
          142  }

          143
          144}

          145

           

          二、java 對(duì).xls 文件格式的操作

           1
           2package com.job5156.xlstodb;
           3
           4import java.io.FileInputStream;
           5import java.io.InputStream;
           6
           7import jxl.Cell;
           8import jxl.Workbook;
           9
          10/**
          11 * @author Alpha
          12 * JAVA 操作 excel 中的 .xls文件格式
          13 */

          14public class ExcelUtil
          15{
          16    public static void main(String[] args) 
          17    {
          18        ExcelUtil eu = new ExcelUtil();
          19        eu.run("D:/alpha/ab.xls");
          20    }

          21    
          22    private void run(String filename)
          23    {
          24        try
          25        {
          26            InputStream is = new FileInputStream(filename);
          27            jxl.Workbook rwb = Workbook.getWorkbook(is);
          28            //獲得總 Sheets
          29            //Sheet[] sheets = rwb.getSheets();
          30            //int sheetLen = sheets.length;
          31            //獲得單個(gè)Sheets 含有的行數(shù)
          32            jxl.Sheet rs = rwb.getSheet(0); //讀取第一個(gè)工作表的數(shù)據(jù)
          33            //Cell[] cell_domain = rs.getColumn(0);//讀取第一列的值
          34            int num = rs.getRows();//得到此excel有多少行..
          35            for(int i=0;i<num;i++)
          36            {
          37                Cell[] cell = rs.getRow(i);//得到第i行的數(shù)據(jù)..返回cell數(shù)組
          38                String name = cell[0].getContents();//得到第i行.第一列的數(shù)據(jù).
          39                String email = cell[1].getContents();//得到第i行.第二列的數(shù)據(jù).
          40                String tel = cell[2].getContents(); 
          41                String number = cell[3].getContents(); 
          42                
          43                System.out.println("===name:"+name);
          44                System.out.println("===email:"+email);
          45                System.out.println("===tel:"+tel);
          46                System.out.println("===number:"+number);
          47                System.out.println(" ");
          48            }

          49        }

          50        catch(Exception ex)
          51        {
          52                ex.printStackTrace();
          53        }

          54        finally{
          55        }

          56    }

          57}

          58


           

          posted on 2007-11-21 10:29 Alpha 閱讀(6451) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): Java J2EE JSP

          評(píng)論:
          # re: JAVA 讀取 EXCEL不同文件格式的內(nèi)容(代碼) 2007-12-02 14:57 | vzless
          請(qǐng)教一下 ,你是怎么復(fù)制的行號(hào)和方框樹(shù)啊?notepad++在哪里設(shè)置啊?
          謝謝...還有關(guān)鍵字顏色怎么都能不變的導(dǎo)出呢???  回復(fù)  更多評(píng)論
            
          # re: JAVA 讀取 EXCEL不同文件格式的內(nèi)容(代碼) 2007-12-08 16:19 | smildlzj
          apache 的 poi可以操作excel

          老大,請(qǐng)看這留言:http://www.aygfsteel.com/Alpha/archive/2007/12/08/147495.html#166297  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 梓潼县| 缙云县| 东乌珠穆沁旗| 清流县| 武义县| 泸州市| 长治市| 西华县| 红河县| 张家川| 卫辉市| 开封市| 卢氏县| 泉州市| 漾濞| 株洲市| 文安县| 刚察县| 诏安县| 敦化市| 临猗县| 会东县| 五大连池市| 平塘县| 乐陵市| 武定县| 玉屏| 常宁市| 潮安县| 博野县| 鄄城县| 聊城市| 淮阳县| 会东县| 宜良县| 茌平县| 龙泉市| 舟曲县| 石泉县| 兴和县| 荥经县|