怎么处理excel

一个jacob操作Word的例子,其他操作excel,pdf的sample里都有


  import java.io.File;
  import com.jacob.com.*;
  import com.jacob.activeX.*;
  public class WordTest {
  
   public static void main(String[] args) {
     WordBean word=new WordBean();
     word.openWord(true);
     word.createNewDocument();
     word.insertText("Hello word.");
   }
  }

  import com.jacob.activeX.*;
  import com.jacob.com.*;
  public class WordBean extends java.awt.Panel
  {
   private ActiveXComponent MsWordApp = null;
   private Dispatch document = null;
   public WordBean()
   {
     super();
   }
   public void openWord(boolean makeVisible)
   {
  //Open Word if we've not done it already
     if (MsWordApp == null)
     {
       MsWordApp = new ActiveXComponent("Word.Application");
     }
  //Set the visible property as required.
     Dispatch.put(MsWordApp, "Visible",
            new Variant(makeVisible));
   }
   public void createNewDocument()
   {
  //Find the Documents collection object maintained by Word
     Dispatch documents =
         Dispatch.get(MsWordApp,"Documents").toDispatch();
  //Call the Add method of the Documents collection to create
  //a new document to edit
     document = Dispatch.call(documents,"Add").toDispatch();
   }
   public void insertText(String textToInsert)
   {
  // Get the current selection within Word at the moment. If
  // a new document has just been created then this will be at
  // the top of the new doc
     Dispatch selection =
         Dispatch.get(MsWordApp,"Selection").toDispatch();
  //Put the specified text at the insertion point
     Dispatch.put(selection,"Text",textToInsert);
   }
   public void saveFileAs(String filename)
   {
     Dispatch.call(document,"SaveAs",filename);
   }
   public void printFile()
   {
  //Just print the current document to the default printer
     Dispatch.call(document,"PrintOut");
   }
   public void closeDocument()
   {
  // Close the document without saving changes
  // 0 = wdDoNotSaveChanges
  // -1 = wdSaveChanges
  // -2 = wdPromptToSaveChanges
     Dispatch.call(document, "Close", new Variant(0));
     document = null;
   }
   public void closeWord()
   {
     Dispatch.call(MsWordApp,"Quit");
     MsWordApp = null;
     document = null;
   }
  }

 


2、 Java Excel 操作excel


  从Excel文件读取数据表


  Java Excel API 既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表。读取Excel数据表的第一步是创建Workbook(术 语:工作薄),下面的代码片段举例说明了应该如何操作:(完整代码见ExcelReading.java)


import java.io.*;
import jxl.*;
… … … …
try
{
//构建Workbook对象, 只读Workbook对象
//直接从本地文件创建Workbook
//从输入流创建Workbook
  InputStream is = new FileInputStream(sourcefile);
  jxl.Workbook rwb = Workbook.getWorkbook(is);
}
catch (Exception e)
{
e.printStackTrace();
}


一旦创建了Workbook,我们就可以通过它来访问Excel Sheet(术语:工作表)。参考下面的代码片段:

 

//获取第一张Sheet表
Sheet rs = rwb.getSheet(0);


我们既可能通过Sheet的名称来访问它,也可以通过下标来访问它。如果通过下标来访问的话,要注意的一点是下标从0开始,就像数组一样。


一旦得到了Sheet,我们就可以通过它来访问Excel Cell(术语:单元格)。参考下面的代码片段:

 

//获取第一行,第一列的值
Cell c00 = rs.getCell(0, 0);
String strc00 = c00.getContents();

//获取第一行,第二列的值
Cell c10 = rs.getCell(1, 0);
String strc10 = c10.getContents();

//获取第二行,第二列的值
Cell c11 = rs.getCell(1, 1);
String strc11 = c11.getContents();

System.out.println("Cell(0, 0)" + " value : " + strc00 + "; type : " + c00.getType());
System.out.println("Cell(1, 0)" + " value : " + strc10 + "; type : " + c10.getType());
System.out.println("Cell(1, 1)" + " value : " + strc11 + "; type : " + c11.getType());


如果仅仅是取得Cell的值,我们可以方便地通过getContents()方法,它可以将任何类型的Cell值都作为一个字符串返回。示例代码中 Cell(0, 0)是文本型,Cell(1, 0)是数字型,Cell(1,1)是日期型,通过getContents(),三种类型的返回值都是字符型。


如果有需要知道Cell内容的确切类型,API也提供了一系列的方法。参考下面的代码片段:

 

String strc00 = null;
double strc10 = 0.00;
Date strc11 = null;

Cell c00 = rs.getCell(0, 0);
Cell c10 = rs.getCell(1, 0);
Cell c11 = rs.getCell(1, 1);

if(c00.getType() == CellType.LABEL)
{
LabelCell labelc00 = (LabelCell)c00;
strc00 = labelc00.getString();
}
if(c10.getType() == CellType.NUMBER)
{
NmberCell numc10 = (NumberCell)c10;
strc10 = numc10.getValue();
}
if(c11.getType() == CellType.DATE)
{
DateCell datec11 = (DateCell)c11;
strc11 = datec11.getDate();
}

System.out.println("Cell(0, 0)" + " value : " + strc00 + "; type : " + c00.getType());
System.out.println("Cell(1, 0)" + " value : " + strc10 + "; type : " + c10.getType());
System.out.println("Cell(1, 1)" + " value : " + strc11 + "; type : " + c11.getType());


在得到Cell对象后,通过getType()方法可以获得该单元格的类型,然后与API提供的基本类型相匹配,强制转换成相应的类型,最后调用相应的取 值方法getXXX(),就可以得到确定类型的值。API提供了以下基本类型,与Excel的数据格式相对应:

 

每种类型的具体意义,请参见Java Excel API Document。


当你完成对Excel电子表格数据的处理后,一定要使用close()方法来关闭先前创建的对象,以释放读取数据表的过程中所占用的内存空间,在读取大量数据时显得尤为重要。参考如下代码片段:


//操作完成时,关闭对象,释放占用的内存空间

rwb.close();


Java Excel API提供了许多访问Excel数据表的方法,在这里我只简要地介绍几个常用的方法,其它的方法请参考附录中的Java Excel API Document。

Workbook类提供的方法


1. int getNumberOfSheets()


获得工作薄(Workbook)中工作表(Sheet)的个数,示例:


jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));

int sheets = rwb.getNumberOfSheets();


2. Sheet[] getSheets()


返回工作薄(Workbook)中工作表(Sheet)对象数组,示例:


jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));

Sheet[] sheets = rwb.getSheets();

  

3. String getVersion()


返回正在使用的API的版本号,好像是没什么太大的作用。


jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));

String apiVersion = rwb.getVersion();


Sheet接口提供的方法

 

1) String getName()

 

获取Sheet的名称,示例:


jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));

jxl.Sheet rs = rwb.getSheet(0);

String sheetName = rs.getName();


2) int getColumns()

 

获取Sheet表中所包含的总列数,示例:


jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));

jxl.Sheet rs = rwb.getSheet(0);

int rsColumns = rs.getColumns();


3) Cell[] getColumn(int column)

 

获取某一列的所有单元格,返回的是单元格对象数组,示例:


jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));

jxl.Sheet rs = rwb.getSheet(0);

Cell[] cell = rs.getColumn(0);


4) int getRows()

 

获取Sheet表中所包含的总行数,示例:


jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));

jxl.Sheet rs = rwb.getSheet(0);

int rsRows = rs.getRows();


5) Cell[] getRow(int row)


获取某一行的所有单元格,返回的是单元格对象数组,示例:


jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));

jxl.Sheet rs = rwb.getSheet(0);

Cell[] cell = rs.getRow(0);


6) Cell getCell(int column, int row)

 

获取指定单元格的对象引用,需要注意的是它的两个参数,第一个是列数,第二个是行数,这与通常的行、列组合有些不同。


jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));

jxl.Sheet rs = rwb.getSheet(0);

Cell cell = rs.getCell(0, 0);

欢迎加入群(19389265)JAVA之家,加的朋友请注明来自CSDN

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值