java利用jxl包操作excel入库,有例子

首先下载jxl.jar包,然后引用.具体操作如下:
其中fileName是从上一个页面传递过来的excel文件
InputStream is = new FileInputStream(fileName);
      jxl.Workbook wb = Workbook.getWorkbook(is);///得到工作薄
      int sheetnum=wb.getNumberOfSheets();取得sheet的数量

jxl.Sheet st = wb.getSheet(kk);///实例化sheet
      int allRec=st.getRows();///得到该sheet的行数
int row = st.getColumns();    得到该sheet的列数

for(int i=1;i<=allRec-1;i++)////行循环
      {
         for(j=0;j<=row;j++)///列循环
        {
         switch(j)
{
              case 0:
                Cell c00= st.getCell(0,i);///0是列数,i是行数
                temp = c00.getContents();
                case 1:
                 ..............
        }
      }
////////excel的读法是先列后行,例如getcell(0,1),表示是第二行第一列

 

 

 

package excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;

public class ReadExcel
{
public void readExcel()
{
try {
//构建Workbook对象, 只读Workbook对象
//直接从本地文件创建Workbook //从输入流创建Workbook
File file = new File("E:/temp/HodeOA_2005/Book1.xls");
InputStream is = new FileInputStream(file);
Workbook rwb = Workbook.getWorkbook(is);

//获得工作薄(Workbook)中工作表(Sheet)的个数
//int sheets = rwb.getNumberOfSheets();
//返回工作薄(Workbook)中工作表(Sheet)对象数组
Sheet[] sheets = rwb.getSheets();
for (int i = 0; i {
System.out.println("-------------------------------");
System.out.println("【工作表Sheet"+i+"的信息】");
//获取获取工作表(Sheet)的名称
String sheetName = sheets[i].getName();
System.out.println("Sheet名称:" + sheetName);
//获取Sheet表中所包含的总列数
int rsColumns = sheets[i].getColumns();
System.out.println("Sheet总列数:" + rsColumns);
//获取Sheet表中所包含的总行数
int rsRows = sheets[i].getRows();
System.out.println("Sheet总行数:" + rsRows);
//逐行输出单元格信息
System.out.println("Sheet内容:" + rsRows);
for (int j = 0; j {
System.out.print("行"+j+":");
//获取某一行的所有单元格,返回的是单元格对象数组,示例子:
Cell[] cell = sheets[i].getRow(j);
for (int k = 0; k {
//获取单元内容
String cellContent = cell[k].getContents();
//获取单元类型
CellType cellType= cell[k].getType();

System.out.print(" "+cellContent+"["+cellType+"] ");
}
System.out.println();
}
}
rwb.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
ReadExcel re = new ReadExcel();
re.readExcel();
}
}

package excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;

public class ReadExcel
{
public void readExcel()
{
try {
//构建Workbook对象, 只读Workbook对象
//直接从本地文件创建Workbook //从输入流创建Workbook
File file = new File("E:/temp/HodeOA_2005/Book1.xls");
InputStream is = new FileInputStream(file);
Workbook rwb = Workbook.getWorkbook(is);

//获得工作薄(Workbook)中工作表(Sheet)的个数
//int sheets = rwb.getNumberOfSheets();
//返回工作薄(Workbook)中工作表(Sheet)对象数组
Sheet[] sheets = rwb.getSheets();
for (int i = 0; i {
System.out.println("-------------------------------");
System.out.println("【工作表Sheet"+i+"的信息】");
//获取获取工作表(Sheet)的名称
String sheetName = sheets[i].getName();
System.out.println("Sheet名称:" + sheetName);
//获取Sheet表中所包含的总列数
int rsColumns = sheets[i].getColumns();
System.out.println("Sheet总列数:" + rsColumns);
//获取Sheet表中所包含的总行数
int rsRows = sheets[i].getRows();
System.out.println("Sheet总行数:" + rsRows);
//逐行输出单元格信息
System.out.println("Sheet内容:" + rsRows);
for (int j = 0; j {
System.out.print("行"+j+":");
//获取某一行的所有单元格,返回的是单元格对象数组,示例子:
Cell[] cell = sheets[i].getRow(j);
for (int k = 0; k {
//获取单元内容
String cellContent = cell[k].getContents();
//获取单元类型
CellType cellType= cell[k].getType();

System.out.print(" "+cellContent+"["+cellType+"] ");
}
System.out.println();
}
}
rwb.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
ReadExcel re = new ReadExcel();
re.readExcel();
}
}

 

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