JAVA对Excel的操作 jxl篇

首先需要到网上下个jxl.jar的包

练习代码如下:(注意里面的图片替换成自己的就可以了)

import java.io.*;
import java.util.Random;
import java.util.Date;
import jxl.*;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.Boolean;
public class CreateXL
{
    public CreateXL()
    {
       
    }
    public static void main(String[] args)
    {
        //读Excel
        //CreateXL.readExcel("d:/abc.xls");
       
        //创建新的Excel
        CreateXL.writeExcel("d:/new.xls");
       
        //更新Excel
        CreateXL.updateExcel("d:/new.xls");
       
    }
    //jxl暂时不提供修改已经存在的数据表,这里通过一个小办法来达到这个目的,不适合大型数据更新!
    //这里是通过覆盖原文件来更新的.
    public static void updateExcel(String filePath)
    {
        try
        {
            Workbook rwb = Workbook.getWorkbook(new File(filePath));
            WritableWorkbook wwb = Workbook.createWorkbook(new File("d:/new.xls"),rwb);//copy
            WritableSheet ws = wwb.getSheet(0);
            WritableCell wc = ws.getWritableCell(0,0);
            //判断单元格的类型,做出相应的转换
            Label label = (Label)wc;
            label.setString("The value has been modified");
            wwb.write();
            wwb.close();
            rwb.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void writeExcel(String filePath)
    {
        try
        {
            //创建工作薄
            WritableWorkbook wwb = Workbook.createWorkbook(new File(filePath));
           
            //创建工作表
            WritableSheet ws = wwb.createSheet("Sheet1",0);
            //System.out.println("create ok!");
           
            //添加标签文本
            Random rnd=new Random((new Date()).getTime());
            int forNumber=rnd.nextInt(100);
            for(int i=0;i<forNumber;i++)
            {
                ws.addCell(new Number(rnd.nextInt(50),rnd.nextInt(50),rnd.nextInt(1000)));
            }
           
            //添加图片(注意此处jxl暂时只支持png格式的图片)
      //0,1分别代表x,y    2,5代表宽和高占的单元格数
      ws.addImage(new WritableImage(0,1,2,5,new File("png\\cs.png")));
           
            wwb.write();
      wwb.close();
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
    }
    public static void readExcel(String filePath)
    {
        /**
         *后续考虑问题,比如Excel里面的图片以及其他数据类型的读取
         **/
        try
        {
            InputStream is=new FileInputStream(filePath);
            //声名一个工作薄
            Workbook rwb = Workbook.getWorkbook(is);
           
            //获得工作薄的个数
            //rwb.getNumberOfSheets();
               
            //在Excel文档中,第一张工作表的缺省索引是0
            Sheet st = rwb.getSheet("Sheet1");
           
            //通用的获取cell值的方式,getCell(int column, int row) 行和列
            int Rows=st.getRows();
            int Cols=st.getColumns();
            System.out.println("当前工作表的名字:"+st.getName());
            System.out.println("总行数:"+Rows);
            System.out.println("总列数:"+Cols);
       
            Cell c;
            for(int i=0;i<Cols;++i)
            {
                for(int j=0;j<Rows;++j)
                {
                    //getCell(Col,Row)获得单元格的值
                    System.out.print((st.getCell(i,j)).getContents()+"\t");
                }
                System.out.print("\n");
            }
            //操作完成时,关闭对象,释放占用的内存空间
            rwb.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(java,工作,Excel,J#)