暂时 我只用到读的功能 ,读功能已经OK
代码类如下
调用方法:
private Excel m_ExcelFile ;
m_ExcelFile = new Excel("mnt/sdcard/market/fileExcel.xls",0);
package untils;
import java.io.File;
import java.io.IOException;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
import jxl.read.biff.BiffException;
import jxl.Workbook;
import jxl.Sheet;
public class Excel {
private String m_PathName;
private String m_ExcelSheet[][];
private int m_LinesCount;
private int m_ColumnsCount;
public Excel(String path_name,int sheetIndex)
{
m_PathName = path_name;
try
{
Workbook book = Workbook.getWorkbook(new File(m_PathName));
Sheet sheet = book.getSheet(sheetIndex);
m_LinesCount = sheet.getRows();// 获取行数
m_ColumnsCount = sheet.getColumns();
m_ExcelSheet = new String[m_LinesCount][m_ColumnsCount];
for(int i=0;i<m_LinesCount;i++)
{
for(int m =0;m<m_ColumnsCount;m++)
m_ExcelSheet[i][m] = sheet.getRow(i)[m].getContents();
}
} catch (BiffException e){} catch (IOException e){}
}
public boolean ExcelWriteLable(String sheet,int sheetIndex,int x,int y,String lable)
{
boolean r = true;
WritableWorkbook wwb = null;
//创建一个可写入的工作薄(Workbook)对象
try {wwb = Workbook.createWorkbook(new File(m_PathName));} catch (IOException e2) { }
if (wwb != null)
{
// 第一个参数是工作表的名称,第二个是工作表在工作薄中的位置
WritableSheet ws = wwb.createSheet(sheet, 0);
// 在指定单元格插入数据
Label lbl1 = new Label(x, y, "lable");
try
{
ws.addCell(lbl1);
} catch (RowsExceededException e1)
{r = false;}
catch (WriteException e1)
{r = false;}
try
{
// 从内存中写入文件中
wwb.write();
wwb.close();
} catch (IOException e)
{ r = false;}
catch (WriteException e)
{r = false;}
}
else
r = false;
return r;
}
public boolean ExcelWriteNum(String sheet,int sheetIndex,int x,int y,double num)
{
boolean r = true;
WritableWorkbook wwb = null;
//创建一个可写入的工作薄(Workbook)对象
try {wwb = Workbook.createWorkbook(new File(m_PathName));} catch (IOException e2) { }
if (wwb != null)
{
// 第一个参数是工作表的名称,第二个是工作表在工作薄中的位置
WritableSheet ws = wwb.createSheet(sheet, 0);
// 在指定单元格插入数据
Number n = new Number(x,y,num);;
try
{
ws.addCell(n);
} catch (RowsExceededException e1)
{r = false;}
catch (WriteException e1)
{r = false;}
try
{
// 从内存中写入文件中
wwb.write();
wwb.close();
} catch (IOException e)
{ r = false;}
catch (WriteException e)
{r = false;}
}
else
r = false;
return r;
}
public String ExcelRead(int x, int y)
{
String r_str = m_ExcelSheet[y][x];
return r_str;
}
public int ExcelLinesCount()
{
return m_LinesCount;
}
}