`
yaoyao19851023
  • 浏览: 31512 次
  • 性别: Icon_minigender_2
  • 来自: 大连
最近访客 更多访客>>
社区版块
存档分类
最新评论

jxl处理excel文件

    博客分类:
  • jxl
阅读更多
haha先留下以后几不用搜索了。    

之前都没有用java去操作过excel,项目需要,所以专门去了解了下,看到有用到jxl这个插件,感觉很实用。特地建个备忘录,以便以后自己查询:
public static void main(String[] args) {
  try {
      Workbook wb=Workbook.getWorkbook(new File("e:\\test.xls"));
      Sheet sheet=wb.getSheet(0);
      Cell[] cellcol=sheet.getColumn(0);
      for (int j=0;j<cellcol.length;j++) {      ①
       if(cellcol[j].getContents().equals("测试酒店1")){
Cell[] cell=sheet.getRow(j);
for(int i=0;i<cell.length;i++){
    System.out.println(i+":"+cell[i].getContents());
}
        }
       }
      wb.close();
  } catch (BiffException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
}
① 因为头回接触 , 不知道怎么获取特定行里头的数据,所以用了个很原始的方法,先获取某一特定列的值,然后匹配你所特定的,然后获取那行数据





/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

import java.io.*;
import java.net.URL;
import java.io.File;
import java.util.Date;
import jxl.*;
import jxl.read.biff.*;
import tel.db.DBConnect;
import tel.util.*;
import java.sql.*;
import javax.servlet.http.*;

public class xsl {
  public xsl() {

  }
  public  void    test(){
    System.out.println("test okccccccccccccc");
  }
  //readWrite  写入count 表 2004-11-22 update by renchanglin 上传的data.xsl 只有3列
   public void  readWrite(String xlsFile ,String date)   throws   Exception{
     String myxslFile=xlsFile;
      FileInputStream fileOut=null;
      URL url=this.getClass().getResource("../../../upload/data.xls");

      try {
        Workbook workbook = Workbook.getWorkbook(new File(url.getFile()));
        Sheet sheet = workbook.getSheet(0);

int  cNum=sheet.getColumns();
int fcode=22;

String  user="信息";
String  danwei ="信息";
DBConnect dbc = new DBConnect();
dbc.prepareStatement("delete from account");
dbc.executeUpdate();
int rNum=sheet.getRows();
for(int i=1;i<rNum;i++)
        {

          Cell a0=sheet.getCell(0,i);
          //   NumberCell b0=(NumberCell)a0;
     Double   b0=new Double(a0.getContents());
             fcode=b0.intValue();


             Cell a1=sheet.getCell(1,i);
            user= a1.getContents();
             Cell a2=sheet.getCell(2,i);
             danwei = a2.getContents();
             danwei=danwei.trim();




          dbc.prepareStatement("INSERT INTO account (fcode,user,danwei) VALUES (?,?,?)");
          dbc.setInt(1, fcode);


         dbc.setBytes(2, user.getBytes("GBK"));
         dbc.setBytes(3, danwei.getBytes("GBK"));
          dbc.executeUpdate();
        }

workbook.close();
      }
      catch (BiffException ex2) {
        System.out.println("iiiiiiiiiiiiiiiiii");
      }
      catch (IOException ex2) {
      }


   }



   public void  addData(String xlsFile ,String date)   throws   Exception{
     String myxslFile=xlsFile;
      FileInputStream fileOut=null;
      URL url=this.getClass().getResource("../../../upload/data.xls");
      System.out.println("222222222222222");
      try {
        Workbook workbook = Workbook.getWorkbook(new File(url.getFile()));
        Sheet sheet = workbook.getSheet(0);
//  Cell a1 = sheet.getCell(2,4);

// Cell b2 = sheet.getCell(1,1);
// Cell c2 = sheet.getCell(2,1);

int ddh=3402;
float shyzf=35.0f;//月租费
float shthf=0.0f;//市话费
float  bdwhf=0.0f;//本地网话费
float    qzdchf=0.0f; //长话费
float sjf=0.0f;//数据费
float yhje=0.0f;//优惠费
float qthf=0.0f;//其它话费
float total=0.0f;//化费合计
String fdate=date;//日期

DBConnect dbc = new DBConnect();
int rNum=sheet.getRows();
int cNum=sheet.getColumns();
for(int i=1;i<rNum-1;i++)
        {
          Cell a0=sheet.getCell(0,i);
          //   NumberCell b0=(NumberCell)a0;
     Double   b0=new Double(a0.getContents());
             ddh=b0.intValue();
             Cell a1=sheet.getCell(2,i);
          Double   b1=new Double(a1.getContents());
             shyzf=b1.floatValue();
             Cell a3=sheet.getCell(3,i);
             Double   b3=new Double(a3.getContents());
              shthf=b3.floatValue();
                  Cell a4=sheet.getCell(4,i);
                  Double   b4=new Double(a4.getContents());
                  bdwhf=b4.floatValue();
                   Cell a6=sheet.getCell(6,i);
                   Double   b6=new Double(a6.getContents());
                   qzdchf=b6.floatValue();
                  Cell a18=sheet.getCell(18,i);
                  Double   b18=new Double(a18.getContents());
                  sjf=b18.floatValue();

                  Cell a26=sheet.getCell(26,i);
                  Double   b26=new Double(a26.getContents());
                  yhje=b26.floatValue();
                  float  tempdata=0.0f;

               for(int j=7;j<cNum-1;j++)
               {
                 Cell a7=sheet.getCell(j,i);
                 Double   b7=new Double(a7.getContents());
                float   f7=b7.floatValue();
                 tempdata=tempdata+f7;
               }
                  qthf=tempdata-yhje-sjf;
                  total=qthf+sjf+shyzf+shthf+bdwhf+qzdchf-yhje;
               // total=total+yzcharg;  rcl temp update
          dbc.prepareStatement("INSERT INTO alldata (ddh,shyzf,shthf,bdwhf,qzdchf,sjf,yhje,qthf,total,fdate) VALUES (?,?,?,?,?,?,?,?,?,?)");
          dbc.setInt(1, ddh);
          dbc.setFloat(2, shyzf);
          dbc.setFloat(3, shthf);
          dbc.setFloat(4, bdwhf);
          dbc.setFloat(5,qzdchf);
          dbc.setFloat(6, sjf);
          dbc.setFloat(7, yhje);
          dbc.setFloat(8, qthf);
          dbc.setFloat(9, total);
          dbc.setBytes(10, fdate.getBytes("GBK"));
          dbc.executeUpdate();
        }

workbook.close();
      }
      catch (BiffException ex2) {
        System.out.println("iiiiiiiiiiiiiiiiii");
      }
      catch (IOException ex2) {
      }


   }

   public void  initUser(String xlsFile ,String date)   throws   Exception{
     String myxslFile=xlsFile;
      FileInputStream fileOut=null;
      URL url=this.getClass().getResource("../../../upload/data.xls");
      System.out.println("222222222222222");
      try {
        Workbook workbook = Workbook.getWorkbook(new File(url.getFile()));
        Sheet sheet = workbook.getSheet(0);
//  Cell a1 = sheet.getCell(2,4);

// Cell b2 = sheet.getCell(1,1);
// Cell c2 = sheet.getCell(2,1);
int  cNum=sheet.getColumns();
int fcode=22;
int xcode=0;
      String  user="renchanglin";
      String  user1="renchanglin";
      String pcode="888888";
      int fclass=1;
      int cnum=0;
      float ccharg=0.0f;
      float cser=0.0f;
      int jcnum=0;
      float jccharg=0.0f;
      int mcnum=0;
      float mccharg=0.0f;
      int jnum=0;
      float jcharg=0.0f;
      float jser=0.0f;
      int dnum=0;
      float dcharg=0.0f;
      float dser=0.0f;
      int inum=0;
      float icharg=0.0f;
      float iser=0.0f;
      float yzcharg=0.0f;
      float btcharg=0.0f;
      float qtcharg=0.0f;
      float total=0.0f;
      String fdate=date;
DBConnect dbc = new DBConnect();
int rNum=sheet.getRows();
for(int i=1;i<rNum-1;i++)
        {

          Cell a0=sheet.getCell(0,i);
          //   NumberCell b0=(NumberCell)a0;
     Double   b0=new Double(a0.getContents());
             fcode=b0.intValue();
             Cell a1=sheet.getCell(1,i);
          Double   b1=new Double(a1.getContents());
             xcode=b1.intValue();
             Cell a2=sheet.getCell(2,i);
             user= a2.getContents();
            //  Cell a3=sheet.getCell(3,i);
          //   user1= a3.getContents();
              pcode="888888";
                Cell a5=sheet.getCell(5,i);
                Double   b5=new Double(a5.getContents());
                         fclass=b5.intValue();
                  Cell a6=sheet.getCell(6,i);
                  Double   b6=new Double(a5.getContents());
                  cnum=b6.intValue();
                   Cell a7=sheet.getCell(7,i);
                   Double   b7=new Double(a7.getContents());
                  ccharg=b7.floatValue();
                 Cell a8=sheet.getCell(8,i);
                Double   b8=new Double(a8.getContents());
                cser=b8.floatValue();
                Cell a9=sheet.getCell(9,i);
                Double   b9=new Double(a9.getContents());
                jcnum=b9.intValue();
                  Cell a10=sheet.getCell(10,i);
                Double   b10=new Double(a10.getContents());
               jcharg=b10.floatValue();
               Cell a11=sheet.getCell(11,i);
                              Double   b11=new Double(a11.getContents());
                              mcnum=b11.intValue();
                                Cell a12=sheet.getCell(12,i);
                              Double   b12=new Double(a12.getContents());
                             mccharg=b12.floatValue();
                             Cell a13=sheet.getCell(13,i);
       Double   b13=new Double(a13.getContents());
       jnum=b13.intValue();
        Cell a14=sheet.getCell(14,i);
        Double   b14=new Double(a14.getContents());
       jcharg=b14.floatValue();
      Cell a15=sheet.getCell(15,i);
     Double   b15=new Double(a15.getContents());
     jser=b15.floatValue();
     Cell a16=sheet.getCell(16,i);
Double   b16=new Double(a16.getContents());
dnum=b16.intValue();
Cell a17=sheet.getCell(17,i);
Double   b17=new Double(a17.getContents());
dcharg=b17.floatValue();
Cell a18=sheet.getCell(18,i);
Double   b18=new Double(a18.getContents());
dser=b18.floatValue();
    Cell a19=sheet.getCell(19,i);
    Double   b19=new Double(a19.getContents());
    inum=b19.intValue();
    Cell a20=sheet.getCell(20,i);
    Double   b20=new Double(a20.getContents());
    icharg=b20.floatValue();
    Cell a21=sheet.getCell(21,i);
    Double   b21=new Double(a21.getContents());
    iser=b21.floatValue();

    Cell a22=sheet.getCell(22,i);
   Double   b22=new Double(a22.getContents());
   yzcharg=b22.floatValue();
    Cell a23=sheet.getCell(23,i);
        Double   b23=new Double(a23.getContents());
        btcharg=b23.floatValue();
        Cell a24=sheet.getCell(24,i);
            Double   b24=new Double(a24.getContents());
            qtcharg=b24.floatValue();
            Cell a25=sheet.getCell(25,i);
                Double   b25=new Double(a25.getContents());
                total=b25.floatValue();
          dbc.prepareStatement("INSERT INTO user (fcode,user,pcode) VALUES (?,?,?)");
          dbc.setInt(1, fcode);
          dbc.setBytes(2, user.getBytes("GBK"));
          dbc.setBytes(3, pcode.getBytes("GBK"));
          dbc.executeUpdate();
        }

workbook.close();
      }
      catch (BiffException ex2) {
        System.out.println("iiiiiiiiiiiiiiiiii");
      }
      catch (IOException ex2) {
      }
   }

   public void  readWriteqk(String xlsFile ,String date)   throws   Exception{
  String myxslFile=xlsFile;
   FileInputStream fileOut=null;
   URL url=this.getClass().getResource("../../../upload/data.xls");

   try {
     Workbook workbook = Workbook.getWorkbook(new File(url.getFile()));
     Sheet sheet = workbook.getSheet(0);
    int  cNum=sheet.getColumns();
    String fdate=date;
DBConnect dbc = new DBConnect();
int rNum=sheet.getRows();
   String text[]=new String[11];
for(int i=1;i<rNum;i++)
     {
       for (int j=0;j<10;j++)
       {
         Cell a0 = sheet.getCell(j, i);
         text[j]=a0.getContents();
          System.out.println("zzzzzzzzz"+text[j]);
       }
       //   NumberCell b0=(NumberCell)a0;
        System.out.println("zzzzzzzzz"+text[1].getBytes("GBK"));
      dbc.prepareStatement("INSERT INTO book (idcode,title,author,author_affiliation,classify,serial_title,year,issue,keyword,abstract_C) VALUES (?,?,?,?,?,?,?,?,?,?)");
       for (int j=0;j<10;j++)
      {
        dbc.setString(j+1, text[j]);
      }
       dbc.executeUpdate();
     }
workbook.close();
   }
   catch (BiffException ex2) {
     System.out.println("iiiiiiiiiiiiiiiiii");
   }
   catch (IOException ex2) {
   }
}
}
分享到:
评论

相关推荐

    基于jxl对excel文件的row-bean的处理

    NULL 博文链接:https://x-fox.iteye.com/blog/1325160

    excel操作(jxl)

    java导入导出excel操作(jxl) ...最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确 的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式

    java处理excel文件相关jar包

    java处理excel文件相关jar包 jxl.jar 、poi.jar

    jxl.jar java处理Excel

    这是一个专门用于java处理Excel文件的包,功能强大,我已经试过了.

    jxl实现excel大数据导出

    jxl实现excel大数据导出,26000条记录,测试导出时间是19s,例子不算复杂,没有excel样式的处理,可以自己扩展哈,有数据库文件,部署即可看到效果!!! 有三个功能:代码构建数据的导出、数据库数据的导出(从配置...

    Java处理100万行超大Excel文件秒级响应

    由于项目需要对大量Excel数据进行输入输出处理,在使用JXL,POI后发现很容易出现OOM,最后在网上找到阿里的开源项目EasyExcel能很快速的读取写入超大Excel文件。经过大量的调试优化,现通过JAVA生成104万行20列的...

    jxl.jar包(java导入导出Excel文件)

    通过java来操作Excel表格的工具类库。 支持字体、数字、日期操作,能够修饰单元格属性,能支持图像和图表,可以正确的处理Excel文件。 附使用方法。

    用jxl实现的读取excel(可以处理html格式的excel)

    用jxl实现的读取excel文件,生成相应的对象集合。对于读取html格式的excel文件抛出的BiffException异常做了相应的处理

    JXL(Java操作Excel的源代码及例子)

    最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。 搭建环境 将下载后的文件...

    java开发Excel所需 poi jxl两种jar包

    poi是一个Java api组件,用于处理execel和doc文件 jxl可以读取,创建,更新Excel文件,使用该api非windows系统也可以通过纯java来处理Excel。

    JAVA导出Excel报表之jxl插件及实例源码

    包含:jxl插件以及源代码示例 jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有...但jExcelAPI对中文支持非常好,API是纯Java的, 并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。

    java导入导出excel

    其中JxlExcelHelper通过JXL处理excel2003文档,HssfExcelHelper和XssfExcelHelper通过POI分别处理excel2003和excel2007文档。 依赖的jar包:jxl.jar, poi-3.10.1.jar, poi-ooxml-3.10.1.jar, poi-ooxml-schemas-...

    JXL插件和使用说明文档

    但jExcelAPI对中文支持非常好,API是纯Java的, 不依赖Windows系统,即使运行在Linux下,也同样能够正确的处理Excel文件。 需要补充说明的是,jxl对图形和图表的支持很有限,而且 仅仅识别PNG格式的图片。

    JXL jar包(附API)

    JXL 处理excel的开源工具 这是2.4.3版本---用jdk1.4运行 如果自行下载,注意:2.6的版本需要jdk1.6运行

    jxl.jar使用文档

    jxl.jar ...最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。

    快速、简洁、解决大文件内存溢出的java处理Excel工具 .rar

    Alibaba EasyExcel:采用逐行读取的解析模式,将每一行的解析结果以观察者模式通知处理(AnalyEventListener),所以比较适合数据体量较大的Excel文件解析。 场景复现与问题定位 问题代码 这种方式POI会把文件的所有...

    Excel文档API(JXL)

    jxl是一个java操作excel的工具,jExcelAPI对中文支持非常好,API是纯Java的, 并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。 另外需要说明的是,这套API对图形和图表的支持很有限,而且...

    EXCEL生成与读取

    利用 java 以及 jxl 对 Excel文件进行 读写处理。可生成Excel文件,同时支持其他 针对 Excel的辅助功能。

    Excel数据操纵-jar包-jxl.jar

    最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。  搭建环境  将下载后的...

    jxl-2.6.jar

    jxl.jar是通过java操作excel...最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。

Global site tag (gtag.js) - Google Analytics