http://steve-wang-victor.iteye.com/blog/513262
http://steve-wang-victor.iteye.com/blog/513260
package com.lll.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.lll.util.Jobs;
import com.lll.util.JobsDA;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
* 实现生成excel文件并且把pubs表里的数据写倒excel中
* @author
*
*/
public class TestWrite {
//创建excel文件
public void createExcel(String path){
try {
//在path路径下建立一个excel文件
WritableWorkbook wbook=Workbook.createWorkbook(new File(path));
//创建一个工作表 第一个工作区
WritableSheet wsheet=wbook.createSheet("pubs数据库jobs表的信息", 0);
//设置excel里的字体
WritableFont wf=new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD,false);
//给标题规定字体的格式
WritableCellFormat titleFormat=new WritableCellFormat(wf);
String[] title={"job_id","job_desc","min_lvl","max_lvl"};
//设置表头
for(int i=0;i<title.length;i++){
//一列列的打印表头 按照我们规定的格式
Label excelTitle=new Label(i,0,title[i],titleFormat);
//把标头加到我们的工作区
wsheet.addCell(excelTitle);
}
int lll=1;//用于循环excel的行号
//把jobs表里的数据提取出来
List zhi=new JobsDA().jobsSelect();
for(int i=0;i<zhi.size();i++){
Jobs job=(Jobs)zhi.get(i);
Label jobid=new Label(0 , lll ,job.getJobId().toString());
Label jobdesc=new Label(1 ,lll ,job.getJobDesc());
Label minlvl=new Label(2 , lll ,job.getMinLvl().toString());
Label maxlvl=new Label(3 , lll ,job.getMaxLvl().toString());
//把值加到工作表中
wsheet.addCell(jobid);
wsheet.addCell(jobdesc);
wsheet.addCell(minlvl);
wsheet.addCell(maxlvl);
lll++;
}
//写入文件
wbook.write();
wbook.close();
System.out.println("创建成功!");
} catch (Exception e) {
// TODO: handle exception
}
}
//读取excel文件
public void readExcel(String path){
try {
//获得excel文件
Workbook bk=Workbook.getWorkbook(new File(path));
//获得工作表的对象 第一个 从0开始下标
Sheet sheet=bk.getSheet(0);
//获得第一行第一列的单元格
Cell cell1=sheet.getCell(1,0);
//获得他的值
String content=cell1.getContents();
//创建单元格对象
Cell cell=null;
String data="";
//循环遍历数据
//行
for(int i = 0;i< sheet.getRows();i++){
//列
for(int j = 0;j< sheet.getColumns();j++){
cell=sheet.getCell(j,i);//注意 先是列 后是行
if(cell.getType() == CellType.LABEL){//如果数据类型是label的
data+=cell.getContents()+"\t\t\t\t\t\t";
}else if(cell.getType() == CellType.NUMBER){//如果数据类型是number类型的
NumberCell nc=(NumberCell)cell;
data+=nc.getValue()+"\t\t\t\t\t\t";
}else if(cell.getType() == CellType.DATE){
DateCell cd=(DateCell)cell;
data+=cd.getDate()+"\t\t\t\t\t\t";
}
}
data+="\n";
}
System.out.println(sheet.getRows()+"-------------"+sheet.getColumns());
System.out.println(data);
bk.close();
} catch (Exception e) {
// TODO: handle exception
}
}
//将excel里的数据读取出来放到arraylist中
public ArrayList excelArray(String path){
ArrayList<Jobs> list = new ArrayList<Jobs>();
Jobs data = null;
// 创建单元格对象
Cell cell = null;
try {
// 获取 excel文件
Workbook book = Workbook.getWorkbook(new File(path));
// 获取 第一个 工作表对象
Sheet sheet = book.getSheet(0);
// 循环行
for (int i = 1; i < sheet.getRows(); i++) {
data = new Jobs();
for (int j = 0; j < sheet.getColumns(); j++) {
cell = sheet.getCell(j, i);//获取第i+1行第j+1列的单元格
switch (j) {
case 0:
data.setJobId(Short.parseShort(cell.getContents().toString()));
break;
case 1:
data.setJobDesc(cell.getContents());
break;
case 2:
data.setMinLvl(Short.parseShort(cell.getContents()));
break;
case 3:
data.setMaxLvl(Short.parseShort(cell.getContents()));
break;
}
}
list.add(data);
}
book.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
new TestWrite().createExcel("F:\\a.xls");
//new TestWrite().readExcel("F:\\a.xls");
//System.out.println(new TestWrite().excelArray("F:\\a.xls").size());
}
}
备注内容
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);
http://steve-wang-victor.iteye.com/blog/513260
package com.lll.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.lll.util.Jobs;
import com.lll.util.JobsDA;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
* 实现生成excel文件并且把pubs表里的数据写倒excel中
* @author
*
*/
public class TestWrite {
//创建excel文件
public void createExcel(String path){
try {
//在path路径下建立一个excel文件
WritableWorkbook wbook=Workbook.createWorkbook(new File(path));
//创建一个工作表 第一个工作区
WritableSheet wsheet=wbook.createSheet("pubs数据库jobs表的信息", 0);
//设置excel里的字体
WritableFont wf=new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD,false);
//给标题规定字体的格式
WritableCellFormat titleFormat=new WritableCellFormat(wf);
String[] title={"job_id","job_desc","min_lvl","max_lvl"};
//设置表头
for(int i=0;i<title.length;i++){
//一列列的打印表头 按照我们规定的格式
Label excelTitle=new Label(i,0,title[i],titleFormat);
//把标头加到我们的工作区
wsheet.addCell(excelTitle);
}
int lll=1;//用于循环excel的行号
//把jobs表里的数据提取出来
List zhi=new JobsDA().jobsSelect();
for(int i=0;i<zhi.size();i++){
Jobs job=(Jobs)zhi.get(i);
Label jobid=new Label(0 , lll ,job.getJobId().toString());
Label jobdesc=new Label(1 ,lll ,job.getJobDesc());
Label minlvl=new Label(2 , lll ,job.getMinLvl().toString());
Label maxlvl=new Label(3 , lll ,job.getMaxLvl().toString());
//把值加到工作表中
wsheet.addCell(jobid);
wsheet.addCell(jobdesc);
wsheet.addCell(minlvl);
wsheet.addCell(maxlvl);
lll++;
}
//写入文件
wbook.write();
wbook.close();
System.out.println("创建成功!");
} catch (Exception e) {
// TODO: handle exception
}
}
//读取excel文件
public void readExcel(String path){
try {
//获得excel文件
Workbook bk=Workbook.getWorkbook(new File(path));
//获得工作表的对象 第一个 从0开始下标
Sheet sheet=bk.getSheet(0);
//获得第一行第一列的单元格
Cell cell1=sheet.getCell(1,0);
//获得他的值
String content=cell1.getContents();
//创建单元格对象
Cell cell=null;
String data="";
//循环遍历数据
//行
for(int i = 0;i< sheet.getRows();i++){
//列
for(int j = 0;j< sheet.getColumns();j++){
cell=sheet.getCell(j,i);//注意 先是列 后是行
if(cell.getType() == CellType.LABEL){//如果数据类型是label的
data+=cell.getContents()+"\t\t\t\t\t\t";
}else if(cell.getType() == CellType.NUMBER){//如果数据类型是number类型的
NumberCell nc=(NumberCell)cell;
data+=nc.getValue()+"\t\t\t\t\t\t";
}else if(cell.getType() == CellType.DATE){
DateCell cd=(DateCell)cell;
data+=cd.getDate()+"\t\t\t\t\t\t";
}
}
data+="\n";
}
System.out.println(sheet.getRows()+"-------------"+sheet.getColumns());
System.out.println(data);
bk.close();
} catch (Exception e) {
// TODO: handle exception
}
}
//将excel里的数据读取出来放到arraylist中
public ArrayList excelArray(String path){
ArrayList<Jobs> list = new ArrayList<Jobs>();
Jobs data = null;
// 创建单元格对象
Cell cell = null;
try {
// 获取 excel文件
Workbook book = Workbook.getWorkbook(new File(path));
// 获取 第一个 工作表对象
Sheet sheet = book.getSheet(0);
// 循环行
for (int i = 1; i < sheet.getRows(); i++) {
data = new Jobs();
for (int j = 0; j < sheet.getColumns(); j++) {
cell = sheet.getCell(j, i);//获取第i+1行第j+1列的单元格
switch (j) {
case 0:
data.setJobId(Short.parseShort(cell.getContents().toString()));
break;
case 1:
data.setJobDesc(cell.getContents());
break;
case 2:
data.setMinLvl(Short.parseShort(cell.getContents()));
break;
case 3:
data.setMaxLvl(Short.parseShort(cell.getContents()));
break;
}
}
list.add(data);
}
book.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
new TestWrite().createExcel("F:\\a.xls");
//new TestWrite().readExcel("F:\\a.xls");
//System.out.println(new TestWrite().excelArray("F:\\a.xls").size());
}
}
备注内容
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);