jxl操作excel

java代码

package exc;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class SgaqglddUtil {
/**
* 
* 函数功能说明 :读取excel
* @param path
* @throws
*/
     public static void read(String path){
     try   {
              Workbook book  =  Workbook.getWorkbook( new  File( path ));
               //  获得第一个工作表对象 
               Sheet sheet  =  book.getSheet( 0 );
               //  得到第一列第一行的单元格 
               Cell cell1  =  sheet.getCell( 0 ,  0 );
              String result  =  cell1.getContents();
              System.out.println(result);
              book.close();
          }   catch  (Exception e)  {
              System.out.println(e);
          } 
      } 
     
     
     public static void write(String path){
    try   {
             //  Excel获得文件 
             Workbook wb  =  Workbook.getWorkbook( new  File(path));
             //  打开一个文件的副本,并且指定数据写回到原文件 
             
             
             WritableWorkbook book  =  Workbook.createWorkbook( new  File(path),
                    wb);
             
            
             WritableSheet sheet1=book.getSheet(0); //  获取第一个工作表 
           //  WritableSheet sheet  =  book.createSheet( " 第二页 " ,  1 );//创建一个工作表 
             int y=0,x=1;
            sheet1.addCell( new  Label( y , x,  "测试数据 "));//在第向x+1行,y+1列添加“测试数据”
            book.write();
            book.close();
        }   catch  (Exception e)  {
            System.out.println(e);
        } 
     }

 public   static   void  main(String args[]) throws IOException  {
 String path="E:\\bb.xls";
 SgaqglddUtil.copyFile(path, "E:\\excel\\b2.xls");
       SgaqglddUtil.write(path);
       SgaqglddUtil.read(path);
   }
 
// 复制文件
   public static void copyFile(String sourceFile, String targetFile) throws IOException {
    
//     File file=new File(targetFile);
//     file.mkdirs();
       BufferedInputStream inBuff = null;
       BufferedOutputStream outBuff = null;
       try {
           // 新建文件输入流并对它进行缓冲
           inBuff = new BufferedInputStream(new FileInputStream(sourceFile));


           // 新建文件输出流并对它进行缓冲
           outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));


           // 缓冲数组
           byte[] b = new byte[1024 * 5];
           int len;
           while ((len = inBuff.read(b)) != -1) {
               outBuff.write(b, 0, len);
           }
           // 刷新此缓冲的输出流
           outBuff.flush();
       } finally {
           // 关闭流
           if (inBuff != null)
               inBuff.close();
           if (outBuff != null)
               outBuff.close();
       }
   }
} 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值