至此我们的jxl就导入成功了!
package 包名;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.*;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class Test {
static File file=new File("D:测试文件.xls");
//可读取工作表
static jxl.Workbook rwb;
//可写入工作表
static WritableWorkbook writableWorkbook;
static Sheet sheet;
static WritableSheet writableSheet;
//
static int ID_START;
static int ID_END;
//获得指定数据信息
static LabelCell labelCellName;
static LabelCell labelCellId;
static LabelCell labelCellScore;
//检索所有的人员所在的行的范围
static Cell[] cellsName ;
static Cell[] cellsId ;
static Cell[] cellsScore ;
static volatile boolean IS_RUN = true;
public static void main(String[] args) throws IOException, BiffException, WriteException {
//第一次启动时初始化表格
{
//初始化工作表
initWorkBookAndSheet();
//检索控制数据
inspectControlData();
//初始化平时成绩
for (int i = ID_START; i < ID_END; i++) {
if (cellsScore[i].getContents().equals("")) {
Label label =new Label(labelCellScore.getColumn(),i,"0");
writableSheet.addCell(label);
}
}
//写入数据并且关闭
writeInAndClose();
}
// 正式运行选人程序
try {
while (IS_RUN) {
System.out.println("【按回车键开始选人(按输入exit退出)】 >> :");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if(input.equals("exit")) {
break;
}
//
initWorkBookAndSheet(); //初始化工作表
inspectControlData(); //检索控制数据
//随机选人
int idIndex = new Random().nextInt(ID_END-ID_START)+ID_START;
System.out.println(
"恭喜! 学号:"+cellsId[idIndex].getContents()
+" 平时成绩为:"+cellsScore[idIndex].getContents()
+" 的"+cellsName[idIndex].getContents()+"同学被选中 \n");
//安全重置
writeInAndClose();
//是否加分
System.out.println("请输入为该同学加的分数(数字) >> :");
//获取输入
Scanner scanner2 = new Scanner(System.in);
float getScore=scanner2.nextFloat();
System.out.println(cellsName[idIndex].getContents()+"同学平时分 +"+getScore+" 分");
float quondamScore = Float.parseFloat(cellsScore[idIndex].getContents())+getScore;
initWorkBookAndSheet(); //初始化工作表
inspectControlData(); //检索控制数据
((Label)writableSheet.getWritableCell(labelCellScore.getColumn(),idIndex)).setString(quondamScore+"");
writeInAndClose(); //写入程序并且关闭
}
}catch (Exception e){
writeInAndClose();
}
}
/**
* 检索控制信息
* */
protected static void inspectControlData(){
//先获取姓名学号平时成绩的数据标签
labelCellName= sheet.findLabelCell("姓名");
labelCellId = sheet.findLabelCell("学号");
labelCellScore = sheet.findLabelCell("平时成绩");
//检索所有的人员所在的行的范围
cellsName = sheet.getColumn(labelCellName.getColumn());
cellsId = sheet.getColumn(labelCellId.getColumn());
cellsScore = sheet.getColumn(labelCellScore.getColumn());
ID_START=0; //学号起始位置
ID_END=0; //学号结束位置
for (int i = labelCellId.getRow()+1; i < cellsName.length; i++) {
if(ID_START==0 && !cellsId[i].getContents().equals("")){
ID_START=i;
}else if(ID_START!=0 && cellsId[i].getContents().equals("")){
ID_END=i;
break;
}
}
}
/**
* 初始化工作簿和表
* @throws BiffException
* @throws IOException
*/
protected static void initWorkBookAndSheet() throws BiffException, IOException {
//打开读表
rwb = Workbook.getWorkbook(file);
//打开一个可写表并导入指定表格数据
writableWorkbook = Workbook.createWorkbook(file,rwb);
//获取表
sheet = rwb.getSheet(0);
writableSheet = writableWorkbook.getSheet(0); //可读表
}
/**
* 工作簿写入
*/
protected static void writeInAndClose() throws IOException, WriteException {
writableWorkbook.write();
writableWorkbook.close();
rwb.close();
//数据重置
ID_END=0;
ID_START=0;
}
}
好了,以上就是jxl操作excel表格的简单示例,大家有疑问欢迎留言✌️