Java 本周五的相关研究——Excel 的文件管理(数据库初步)
Posted 初等变换不改变矩阵的秩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 本周五的相关研究——Excel 的文件管理(数据库初步)相关的知识,希望对你有一定的参考价值。
日期:2018.9.28
星期五
博客期:013
说到这里,就二话不多说了!这次研习的是与Excel表相关联的方法
1、导入jar包(需要自己下载)
2、AccountManager类的实现(关键看非重复部分!)
package pop; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Calendar; import java.util.Scanner; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; public class AccountManager { //----------------<数据库>-----------------// //数据组 private Account ku[] = new Account[5]; //记录名称 private String Infor = "files/accountinformation.xls"; private String List = "files/accountlist.xls"; private String Copy = "files/Lin.xls"; private String Today = "2018-09-28"; //----------------<界面函数>---------------// //初始界面 public void FirstWin() throws IOException, RowsExceededException, WriteException, BiffException{ System.out.println("***************************************************************"); System.out.println(" 欢迎使用中国工商银行自动柜员系统"); System.out.println("***************************************************************"); System.out.println(" 请输入您的账号:"); Scanner sc = new Scanner(System.in); String x = sc.next(); int existfor = 5;//此处用于测试是否账号不存在 for(int i=0;i<5;i++)//检测数组中是否存有该数据! if(ku[i].GetaccountID().compareTo(x)==0) { existfor = i; break; } if(existfor==5) { System.out.println(" #:该卡不是工行卡!"); FirstWin(); } else SecondWin(existfor); } //密码输入界面 public void SecondWin(int pass) throws IOException, RowsExceededException, WriteException, BiffException{ System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("***************************************************************"); System.out.println(" 请输入您的密码:"); int times = 0;//记录输入密码的次数 Scanner sc = new Scanner (System.in); while(times<3){ String saving = sc.nextLine(); if(ku[pass].Getaccountpassword().compareTo(saving)!=0) System.out.println("密码录入错误"); else break; times++; } if(times==3) FirstWin(); else MainWin(pass); } //主界面 void MainWin(int pass) throws IOException, RowsExceededException, WriteException, BiffException{ System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 1、存款;"); System.out.println(" 2、取款;"); System.out.println(" 3、转账汇款;"); System.out.println(" 4、修改密码;"); System.out.println(" 5、查询金额;"); System.out.println("****************************************************************"); System.out.println(" 请输入:"); Scanner sc = new Scanner (System.in); int temp = sc.nextInt(); switch(temp) { case 1:ku[pass].Setoperatetype(1);writefile();fun1(pass);break; case 2:ku[pass].Setoperatetype(2);writefile();fun2(pass);break; case 3:ku[pass].Setoperatetype(3);writefile();fun3(pass);break; case 4:ku[pass].Setoperatetype(4);writefile();fun4(pass);break; case 5:ku[pass].Setoperatetype(5);writefile();fun5(pass);break; default:MainWin(pass);break; } } //存款界面 public void fun1(int pass) throws IOException, RowsExceededException, WriteException, BiffException{ System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 请输入存款金额;"); Scanner sc = new Scanner (System.in); if(sc.hasNext("q")) FirstWin(); else { int JinE = sc.nextInt();//记录金额 if(JinE<=0) { System.out.println(" #:输入金额有误"); fun1(pass); } else { ku[pass].Setamount(ku[pass].Getamount()+JinE); ku[pass].Setaccountbalance(ku[pass].Getaccountbalance()+JinE); //-----------------------[数据载入文档] System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 当前账户存款操作成功。"); System.out.println(" 当前账户余额为:"+ku[pass].Getaccountbalance()+"元"); //----------------------------------------------------<以下为文件更新阶段 Copyfile(); File f = new File(List); WritableWorkbook workbook = Workbook.createWorkbook(f); WritableSheet sheet = workbook.createSheet("Test",0); for (int i = 0; i < 1; i++) { Label lab = new Label(0,i,"账号"); sheet.addCell(lab); lab = new Label(1,i,"操作日期"); sheet.addCell(lab); lab = new Label(2,i,"操作内容"); sheet.addCell(lab); lab = new Label(3,i,"操作金额"); sheet.addCell(lab); } for(int i=1;i<2;i++) { Label lab = new Label(0,i,ku[pass].GetaccountID()); sheet.addCell(lab); lab = new Label(1,i,Today); sheet.addCell(lab); lab = new Label(2,i,"SaveMoney"); sheet.addCell(lab); lab = new Label(3,i,Integer.toString(JinE)); sheet.addCell(lab); } Workbook readwb = Workbook.getWorkbook(new FileInputStream(Copy)); Sheet readsheet = readwb.getSheet(0); int rsColumns = readsheet.getColumns(); int rsRows = readsheet.getRows(); for(int i=0;i<rsRows;i++) { for(int j=0;j<rsColumns;j++) { Cell cell = readsheet.getCell(j,i); Label lab = new Label(j,i+2,cell.getContents()); sheet.addCell(lab); } } workbook.write(); workbook.close(); writefile(); //---------------------------------------- MainWin(pass); } } } //取款界面 public void fun2(int pass) throws IOException, RowsExceededException, WriteException, BiffException{ System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 当前账户每日可以支取2万元。"); System.out.println(" 1、100元"); System.out.println(" 2、500元"); System.out.println(" 3、1000元"); System.out.println(" 4、1500元"); System.out.println(" 5、2000元"); System.out.println(" 6、5000元;"); System.out.println(" 7、其他金额"); System.out.println(" 8、退卡"); System.out.println(" 9、返回"); System.out.println("****************************************************************"); System.out.println(" #:请选择"); Scanner sc = new Scanner (System.in); int choice = sc.nextInt(); switch(choice){ case 1: { if(ku[pass].Getaccountbalance()<100) { System.out.println("账户余额不足"); fun2(pass); } else { ku[pass].Setamount(ku[pass].Getamount()+100); ku[pass].Setaccountbalance(ku[pass].Getaccountbalance()-100); System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 当前账户取款操作100元成功。"); System.out.println(" 当前账户余额为:"+ku[pass].Getaccountbalance()+"元"); //----------------------------------------------------<以下为文件更新阶段 Copyfile(); File f = new File(List); WritableWorkbook workbook = Workbook.createWorkbook(f); WritableSheet sheet = workbook.createSheet("Test",0); for (int i = 0; i < 1; i++) { Label lab = new Label(0,i,"账号"); sheet.addCell(lab); lab = new Label(1,i,"操作日期"); sheet.addCell(lab); lab = new Label(2,i,"操作内容"); sheet.addCell(lab); lab = new Label(3,i,"操作金额"); sheet.addCell(lab); } for(int i=1;i<2;i++) { Label lab = new Label(0,i,ku[pass].GetaccountID()); sheet.addCell(lab); lab = new Label(1,i,Today); sheet.addCell(lab); lab = new Label(2,i,"GetMoney"); sheet.addCell(lab); lab = new Label(3,i,Integer.toString(100)); sheet.addCell(lab); } Workbook readwb = Workbook.getWorkbook(new FileInputStream(Copy)); Sheet readsheet = readwb.getSheet(0); int rsColumns = readsheet.getColumns(); int rsRows = readsheet.getRows(); for(int i=0;i<rsRows;i++) { for(int j=0;j<rsColumns;j++) { Cell cell = readsheet.getCell(j,i); Label lab = new Label(j,i+2,cell.getContents()); sheet.addCell(lab); } } workbook.write(); workbook.close(); writefile(); //----------------------------------- MainWin(pass); } break; } case 2: { if(ku[pass].Getaccountbalance()<500) { System.out.println("账户余额不足"); fun2(pass); } else { ku[pass].Setamount(ku[pass].Getamount()+500); ku[pass].Setaccountbalance(ku[pass].Getaccountbalance()-500); System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 当前账户取款操作500元成功。"); System.out.println(" 当前账户余额为:"+ku[pass].Getaccountbalance()+"元"); //----------------------------------------------------<以下为文件更新阶段 Copyfile(); File f = new File(List); WritableWorkbook workbook = Workbook.createWorkbook(f); WritableSheet sheet = workbook.createSheet("Test",0); for (int i = 0; i < 1; i++) { Label lab = new Label(0,i,"账号"); sheet.addCell(lab); lab = new Label(1,i,"操作日期"); sheet.addCell(lab); lab = new Label(2,i,"操作内容"); sheet.addCell(lab); lab = new Label(3,i,"操作金额"); sheet.addCell(lab); } for(int i=1;i<2;i++) { Label lab = new Label(0,i,ku[pass].GetaccountID()); sheet.addCell(lab); lab = new Label(1,i,Today); sheet.addCell(lab); lab = new Label(2,i,"GetMoney"); sheet.addCell(lab); lab = new Label(3,i,Integer.toString(500)); sheet.addCell(lab); } Workbook readwb = Workbook.getWorkbook(new FileInputStream(Copy)); Sheet readsheet = readwb.getSheet(0); int rsColumns = readsheet.getColumns(); int rsRows = readsheet.getRows(); for(int i=0;i<rsRows;i++) { for(int j=0;j<rsColumns;j++) { Cell cell = readsheet.getCell(j,i); Label lab = new Label(j,i+2,cell.getContents()); sheet.addCell(lab); } } workbook.write(); workbook.close(); writefile(); //----------------------------------- MainWin(pass); } break; } case 3: { if(ku[pass].Getaccountbalance()<1000) { System.out.println("账户余额不足"); fun2(pass); } else { ku[pass].Setamount(ku[pass].Getamount()+1000); ku[pass].Setaccountbalance(ku[pass].Getaccountbalance()-1000); System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 当前账户取款操作1000元成功。"); System.out.println(" 当前账户余额为:"+ku[pass].Getaccountbalance()+"元"); //----------------------------------------------------<以下为文件更新阶段 Copyfile(); File f = new File(List); WritableWorkbook workbook = Workbook.createWorkbook(f); WritableSheet sheet = workbook.createSheet("Test",0); for (int i = 0; i < 1; i++) { Label lab = new Label(0,i,"账号"); sheet.addCell(lab); lab = new Label(1,i,"操作日期"); sheet.addCell(lab); lab = new Label(2,i,"操作内容"); sheet.addCell(lab); lab = new Label(3,i,"操作金额"); sheet.addCell(lab); } for(int i=1;i<2;i++) { Label lab = new Label(0,i,ku[pass].GetaccountID()); sheet.addCell(lab); lab = new Label(1,i,Today); sheet.addCell(lab); lab = new Label(2,i,"GetMoney"); sheet.addCell(lab); lab = new Label(3,i,Integer.toString(1000)); sheet.addCell(lab); } Workbook readwb = Workbook.getWorkbook(new FileInputStream(Copy)); Sheet readsheet = readwb.getSheet(0); int rsColumns = readsheet.getColumns(); int rsRows = readsheet.getRows(); for(int i=0;i<rsRows;i++) { for(int j=0;j<rsColumns;j++) { Cell cell = readsheet.getCell(j,i); Label lab = new Label(j,i+2,cell.getContents()); sheet.addCell(lab); } } workbook.write(); workbook.close(); writefile(); //----------------------------------- MainWin(pass); } break; } case 4: { if(ku[pass].Getaccountbalance()<1500) { System.out.println("账户余额不足"); fun2(pass); } else { ku[pass].Setamount(ku[pass].Getamount()+1500); ku[pass].Setaccountbalance(ku[pass].Getaccountbalance()-1500); System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 当前账户取款操作1500元成功。"); System.out.println(" 当前账户余额为:"+ku[pass].Getaccountbalance()+"元"); //----------------------------------------------------<以下为文件更新阶段 Copyfile(); File f = new File(List); WritableWorkbook workbook = Workbook.createWorkbook(f); WritableSheet sheet = workbook.createSheet("Test",0); for (int i = 0; i < 1; i++) { Label lab = new Label(0,i,"账号"); sheet.addCell(lab); lab = new Label(1,i,"操作日期"); sheet.addCell(lab); lab = new Label(2,i,"操作内容"); sheet.addCell(lab); lab = new Label(3,i,"操作金额"); sheet.addCell(lab); } for(int i=1;i<2;i++) { Label lab = new Label(0,i,ku[pass].GetaccountID()); sheet.addCell(lab); lab = new Label(1,i,Today); sheet.addCell(lab); lab = new Label(2,i,"GetMoney"); sheet.addCell(lab); lab = new Label(3,i,Integer.toString(1500)); sheet.addCell(lab); } Workbook readwb = Workbook.getWorkbook(new FileInputStream(Copy)); Sheet readsheet = readwb.getSheet(0); int rsColumns = readsheet.getColumns(); int rsRows = readsheet.getRows(); for(int i=0;i<rsRows;i++) { for(int j=0;j<rsColumns;j++) { Cell cell = readsheet.getCell(j,i); Label lab = new Label(j,i+2,cell.getContents()); sheet.addCell(lab); } } workbook.write(); workbook.close(); writefile(); //----------------------------------- MainWin(pass); } break; } case 5: { if(ku[pass].Getaccountbalance()<2000) { System.out.println("账户余额不足"); fun2(pass); } else { ku[pass].Setamount(ku[pass].Getamount()+2000); ku[pass].Setaccountbalance(ku[pass].Getaccountbalance()-2000); System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 当前账户取款操作2000元成功。"); System.out.println(" 当前账户余额为:"+ku[pass].Getaccountbalance()+"元"); //----------------------------------------------------<以下为文件更新阶段 Copyfile(); File f = new File(List); WritableWorkbook workbook = Workbook.createWorkbook(f); WritableSheet sheet = workbook.createSheet("Test",0); for (int i = 0; i < 1; i++) { Label lab = new Label(0,i,"账号"); sheet.addCell(lab); lab = new Label(1,i,"操作日期"); sheet.addCell(lab); lab = new Label(2,i,"操作内容"); sheet.addCell(lab); lab = new Label(3,i,"操作金额"); sheet.addCell(lab); } for(int i=1;i<2;i++) { Label lab = new Label(0,i,ku[pass].GetaccountID()); sheet.addCell(lab); lab = new Label(1,i,Today); sheet.addCell(lab); lab = new Label(2,i,"GetMoney"); sheet.addCell(lab); lab = new Label(3,i,Integer.toString(2000)); sheet.addCell(lab); } Workbook readwb = Workbook.getWorkbook(new FileInputStream(Copy)); Sheet readsheet = readwb.getSheet(0); int rsColumns = readsheet.getColumns(); int rsRows = readsheet.getRows(); for(int i=0;i<rsRows;i++) { for(int j=0;j<rsColumns;j++) { Cell cell = readsheet.getCell(j,i); Label lab = new Label(j,i+2,cell.getContents()); sheet.addCell(lab); } } workbook.write(); workbook.close(); writefile(); //----------------------------------- MainWin(pass); } break; } case 6: { if(ku[pass].Getaccountbalance()<5000) { System.out.println("账户余额不足"); fun2(pass); } else { ku[pass].Setamount(ku[pass].Getamount()+5000); ku[pass].Setaccountbalance(ku[pass].Getaccountbalance()-5000); System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 当前账户取款操作5000元成功。"); System.out.println(" 当前账户余额为:"+ku[pass].Getaccountbalance()+"元"); //----------------------------------------------------<以下为文件更新阶段 Copyfile(); File f = new File(List); WritableWorkbook workbook = Workbook.createWorkbook(f); WritableSheet sheet = workbook.createSheet("Test",0); for (int i = 0; i < 1; i++) { Label lab = new Label(0,i,"账号"); sheet.addCell(lab); lab = new Label(1,i,"操作日期"); sheet.addCell(lab); lab = new Label(2,i,"操作内容"); sheet.addCell(lab); lab = new Label(3,i,"操作金额"); sheet.addCell(lab); } for(int i=1;i<2;i++) { Label lab = new Label(0,i,ku[pass].GetaccountID()); sheet.addCell(lab); lab = new Label(1,i,Today); sheet.addCell(lab); lab = new Label(2,i,"GetMoney"); sheet.addCell(lab); lab = new Label(3,i,Integer.toString(5000)); sheet.addCell(lab); } Workbook readwb = Workbook.getWorkbook(new FileInputStream(Copy)); Sheet readsheet = readwb.getSheet(0); int rsColumns = readsheet.getColumns(); int rsRows = readsheet.getRows(); for(int i=0;i<rsRows;i++) { for(int j=0;j<rsColumns;j++) { Cell cell = readsheet.getCell(j,i); Label lab = new Label(j,i+2,cell.getContents()); sheet.addCell(lab); } } workbook.write(); workbook.close(); writefile(); //----------------------------------- MainWin(pass); } break; } case 7: { System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 请输入取款金额:"); int num = sc.nextInt(); if(ku[pass].Getaccountbalance()<num) { System.out.println("账户余额不足"); fun2(pass); } else { ku[pass].Setamount(ku[pass].Getamount()+num); ku[pass].Setaccountbalance(ku[pass].Getaccountbalance()-num); System.out.println("***************************************************************"); System.out.println(" 欢迎"+ku[pass].Getaccountname()+"使用中国工商银行自助柜员系统"); System.out.println("****************************************************************"); System.out.println(" 当前账户取款操作"+num+"元成功。"); System.out.println(" 当前账户余额为:"+ku[pass].Getaccountbalance()+"元"); writefile(); //----------------------------------------------------<以下为文件更新阶段 Copyfile(); File f = new File(List); WritableWorkbook workbook = Workbook.createWorkbook(f); WritableSheet sheet = workbook.createSheet("Test",0); for (int i = 0; i < 1; i++) { Label lab = new Label(0,i,"账号"); sheet.addCell(lab); lab = new Label(1,i,"操作日期"); sheet.addCell(lab); lab = new Label(2,i,"操作内容"); sheet.addCell(lab); lab = new Label(3,i,"操作金额"); sheet.addCell(lab); } for(int i=1;i<2;i++) { Label lab = new Label(0,i,ku[pass].GetaccountID()); sheet.addCell(lab); lab = new Label(1,i,Today); sheet.addCell(lab); lab = new Label(2,i,"GetMoney"); sheet.addCell(lab); lab = new Label(3,i,Integer.toString(num)); sheet.addCell(lab); } Workbook readwb = Workbook.getWorkbook(new FileInputStream(Copy)); Sheet readsheet = readwb.getSheet(0); int rsColumns = readsheet.getColumns(); int rsRows = readsheet.getRows(); for(int i=0;i<rsRows;i++) { for(int j=0;j<rsColumns;j++) { Cell cell = readsheet.getCell(j,i); Label lab = new Label(j,i+2,cell.getContents()); sheet.addCell(lab); } } workbook.write(); workbook.close(); writefile(); //----------------------------------- MainWin(pass); } break; } case 8: { FirstWin(); break; } case 9: { MainWin(pass); break; } default:fun2(pass);break; } } //转账汇款界面 public void fun3(java操作office和pdf文件java读取word,excel和pdf文档内容