POI實現圖書館管理系統的數據存儲
目的:通過這個系統POI操作Excel表,實現了對其數據的增、刪、改、查功能
系統功能描述:
備註: ①各項功能的詳細操作流程參見壓縮文件中視頻,項目效果演示。
1:增加圖書
添加一個圖數信息(圖書編號、名稱、價格、類別、簡介)
2:刪除圖書
允許管理員輸入要刪除的圖書編號,然後完成圖書的刪除
3:修改圖書信息
允許管理員輸入要修改的部門的編號,然後輸入該圖書修改後的新名稱、新價格、新類別、新簡介。最後完成修改
4:查看所有圖書信息
展示該系統中所有圖書信息。
5:根據價格區間查找圖書
管理員輸入,最小价格,和最大價格,然後展示所有圖書價格處於這個之間的所有圖書信息。
6:根據類別查找圖書
管理員輸入類別名稱,然後展示該類別的所有圖書。
1、主函數
public class Library_main {
public static void main(String[] args) {
// 將數據存入excle表格中
Library a = new Library();
Scanner sc = new Scanner(System.in);
while (true) {
show();
System.out.println("輸入您的選項:");
int i = sc.nextInt();
switch (i) {
case 1:
System.out.println("請輸入書的編號:");
Integer id = sc.nextInt();
System.out.println("請輸入書的名稱:");
String name = sc.next();
System.out.println("請輸入書的價格:");
Double price = sc.nextDouble();
System.out.println("請輸入書的類別:");
String cla = sc.next();
System.out.println("請輸入書的簡介:");
String produce = sc.next();
Book book = new Book();
book.setBook_id(id);
book.setBook_title(name);
book.setBook_price(price);
book.setBook_produce(produce);
book.setBook_class(cla);
a.addBook(book);
break;
case 2:
System.out.println("請輸入書的編號:");
Integer id1 = sc.nextInt();
a.removeBook(id1);
break;
case 3:
System.out.println("請輸入要更新的書的編號:");
Integer id2 = sc.nextInt();
System.out.println("請輸入編號爲" + id2 + "的書的新的名字:");
String name2 = sc.next();
System.out.println("請輸入編號爲" + id2 + "的書的新的價格:");
Double price2 = sc.nextDouble();
System.out.println("請輸入編號爲" + id2 + "的書的新的類別:");
String class2 = sc.next();
System.out.println("請輸入編號爲" + id2 + "的書的新的簡介:");
String product2 = sc.next();
a.updateBook(id2, name2, price2, class2, product2);
break;
case 4:
List<Book> li = a.getAllBooks();
for (Book book2 : li) {
System.out.println(book2);
}
break;
case 5:
System.out.println("輸入最低價格:");
Double minPrice = sc.nextDouble();
System.out.println("輸入最高價格:");
Double maxPrice = sc.nextDouble();
List<Book> li1 = a.getBooksByPriceRange(minPrice, maxPrice);
for (Book book2 : li1) {
System.out.println(book2);
}
break;
case 6:
System.out.println("請輸入類別查找圖書:");
String category = sc.next();
List<Book> li2 = a.getBooksByCategory(category);
for (Book book2 : li2) {
System.out.println(book2);
}
break;
case 7:
System.exit(0);
}
}
}
public static void show() {
System.out.println("1、添加一個圖書信息(圖書編號、名稱、價格、類別、簡介):");
System.out.println("2、根據編號刪除圖書:");
System.out.println("3、修改圖書信息:");
System.out.println("4、查看所有圖書信息:");
System.out.println("5、根據價格區間查找圖書:");
System.out.println("6、根據類別查找圖書:");
System.out.println("7、退出系統");
}
}
2、接口
package com.baizhi.test.project;
import java.util.List;
/**
* 圖書管理系統功能標準(接口) 作者:sqc 時間:2018-4-3 下午21:43:41 描述:TODO
*/
public interface LibrarySystem {
/**
* 描述:TODO 將參數book對象添加到數據倉庫類中
*/
public void addBook(Book book);
/**
* 描述:TODO 根據參數bookId在數據倉庫中找到對應的圖書,然後刪除。
* 提示:1.遍歷數據倉庫中的books屬性中所有的圖書,找到圖書編號和bookId相同的圖書下標
* 2.在遍歷的循環外面,通過list的remove(index)方法刪除圖書
*/
public void removeBook(int bookId);
/**
* 描述:TODO 根據bookId找到對應的圖書,將圖書的其餘屬性分別修改爲參數中傳遞過來的值
*/
public void updateBook(int bookId, String newName, double newPrice,
String newCategory, String newDesc);
/**
* 描述:TODO 獲得所有圖書
*/
public List<Book> getAllBooks();
/**
* 描述:TODO 根據價格區間找到 minPrice<=price<=maxPrice的所有圖書
*/
public List<Book> getBooksByPriceRange(double minPrice, double maxPrice);
/**
* 描述:TODO 根據類名查找所有的圖書
*/
public List<Book> getBooksByCategory(String category);
}
3、實現接口
class Library implements LibrarySystem {
@Override
public void addBook(Book book) {
try{
File file = new File("0.xls");
if(!file.exists()){
file=Excel.createExcel(file);
}
FileInputStream fis = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);//獲取第一行(excel中的行默認從0開始,所以這就是爲什麼,一個excel必須有字段列頭),即,字段列頭,便於賦值
int lastRowNum = sheet.getLastRowNum();//得到最後一行的信息
System.out.println(lastRowNum);
FileOutputStream fos = new FileOutputStream(file);
row = sheet.createRow((short)(lastRowNum+1)); //現有行之後追加數據
for(int i=0;i<5;i++){
switch(i){
case 0:row.createCell(0).setCellValue(book.getBook_id());break;
case 1:row.createCell(1).setCellValue(book.getBook_title());break;
case 2:row.createCell(2).setCellValue(book.getBook_price());break;
case 3:row.createCell(3).setCellValue(book.getBook_class());break;
case 4:row.createCell(4).setCellValue(book.getBook_produce());break;
}
}
wb.write(fos);
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void removeBook(int bookId) {
try{
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("0.xls"));
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row ;
int lastRowNum = sheet.getLastRowNum();
//得到第二行的數據
int i = sheet.getFirstRowNum()+1;
//從第二行開始遍歷,直至最後,因爲第一行是表頭
while(i<=lastRowNum){
//得到第i行
row = sheet.getRow(i);
//得到第i行的第一個單元格
int removeRow = (int) row.getCell(0).getNumericCellValue();
if(bookId == removeRow){
sheet.shiftRows(i, sheet.getLastRowNum(), -1);
sheet.removeRow(sheet.getRow(lastRowNum));//刪除最後一個空行,避免下次存入數據有空行出現
FileOutputStream fos = new FileOutputStream("0.xls");
wb.write(fos);
fos.close();
System.out.println("數據刪除完成!");
return ;
}
i++;
}
System.out.println("不存在這條數據!");
}catch(Exception e){
e.printStackTrace();
}
}
public void updateBook(int bookId, String newName, double newPrice,
String newCategory, String newDesc) {
try{
File file = new File("0.xls");
FileInputStream fis = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
//得到第二行的行號
int firstRowNum = sheet.getFirstRowNum()+1;
//得到最後一行的行號
int lastRowNum = sheet.getLastRowNum();
while(firstRowNum<=lastRowNum){
//得到一行數據
row = sheet.getRow(firstRowNum);
//進行第一個單元格和bookId作比較
if((int)row.getCell(0).getNumericCellValue() == bookId){
row.getCell(1).setCellValue(newName);//設置firstRowNum的第二個單元格
row.getCell(2).setCellValue(newPrice);//設置firstRowNum的第三個單元格
row.getCell(3).setCellValue(newCategory);//設置firstRowNum的第四個單元格
row.getCell(4).setCellValue(newDesc);//設置firstRowNum的第五個單元格
FileOutputStream fos = new FileOutputStream(file);
wb.write(fos);
fos.close();
return;
}
firstRowNum++;
}
System.out.println("此條數據不存在!");
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public List<Book> getAllBooks() {
List<Book> list = new ArrayList<Book>();
try{
FileInputStream fis = new FileInputStream(new File("0.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
int firstRowNum = sheet.getFirstRowNum()+1;
int lastRowNum = sheet.getLastRowNum();
System.out.println(firstRowNum+" "+lastRowNum);
while(firstRowNum<=lastRowNum){
System.out.println(firstRowNum+"------------->");
row = sheet.getRow(firstRowNum);
Book book = new Book();
int id= (int) row.getCell(0).getNumericCellValue();
String name= row.getCell(1).getStringCellValue();
double price = row.getCell(2).getNumericCellValue();
String newCategory = row.getCell(3).getStringCellValue();
String newDesc = row.getCell(4).getStringCellValue();
book.setBook_id(id);
book.setBook_title(name);
book.setBook_price(price);
book.setBook_class(newCategory);
book.setBook_produce(newDesc);
list.add(book);
firstRowNum++;
}
}catch(Exception e){
e.printStackTrace();
}
return list;
}
@Override
public List<Book> getBooksByPriceRange(double minPrice, double maxPrice) {
List<Book> list = new ArrayList<Book>();
try{
FileInputStream fis = new FileInputStream(new File("0.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
int firstRowNum = sheet.getFirstRowNum()+1;
int lastRowNum = sheet.getLastRowNum();
while(firstRowNum<=lastRowNum){
double price = sheet.getRow(firstRowNum).getCell(2).getNumericCellValue();
if(price>minPrice && price<maxPrice){
row = sheet.getRow(firstRowNum);
Book book = new Book();
int id= (int) row.getCell(0).getNumericCellValue();
String name= row.getCell(1).getStringCellValue();
String newCategory = row.getCell(3).getStringCellValue();
String newDesc = row.getCell(4).getStringCellValue();
book.setBook_id(id);
book.setBook_title(name);
book.setBook_price(price);
book.setBook_class(newCategory);
book.setBook_produce(newDesc);
list.add(book);
}
firstRowNum++;
}
}catch(Exception e){
e.printStackTrace();
}
return list;
}
@Override
public List<Book> getBooksByCategory(String category) {
List<Book> list = new ArrayList<Book>();
try{
FileInputStream fis = new FileInputStream(new File("0.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
int firstRowNum = sheet.getFirstRowNum()+1;
int lastRowNum = sheet.getLastRowNum();
while(firstRowNum<=lastRowNum){
String cg = sheet.getRow(firstRowNum).getCell(3).getStringCellValue();
if(category.equals(cg)){
row = sheet.getRow(firstRowNum);
Book book = new Book();
int id= (int) row.getCell(0).getNumericCellValue();
String name= row.getCell(1).getStringCellValue();
Double price = row.getCell(2).getNumericCellValue();
String newCategory = row.getCell(3).getStringCellValue();
String newDesc = row.getCell(4).getStringCellValue();
book.setBook_id(id);
book.setBook_title(name);
book.setBook_price(price);
book.setBook_class(newCategory);
book.setBook_produce(newDesc);
list.add(book);
}
firstRowNum++;
}
}catch(Exception e){
e.printStackTrace();
}
return list;
}
}
4、Book類package com.baizhi.test.project;
public class Book {
private Integer book_id;
private String book_title;
private Double book_price;
private String book_class;
private String book_produce;
public Book(){}
public Book(Integer book_id, String book_title, Double book_price,
String book_class, String book_produce) {
super();
this.book_id = book_id;
this.book_title = book_title;
this.book_price = book_price;
this.book_class = book_class;
this.book_produce = book_produce;
}
public Integer getBook_id() {
return book_id;
}
public void setBook_id(Integer book_id) {
this.book_id = book_id;
}
public String getBook_title() {
return book_title;
}
public void setBook_title(String book_title) {
this.book_title = book_title;
}
public Double getBook_price() {
return book_price;
}
public void setBook_price(Double book_price) {
this.book_price = book_price;
}
public String getBook_class() {
return book_class;
}
public void setBook_class(String book_class) {
this.book_class = book_class;
}
public String getBook_produce() {
return book_produce;
}
public void setBook_produce(String book_produce) {
this.book_produce = book_produce;
}
@Override
public String toString() {
return "Book [book_id=" + book_id + ", book_title=" + book_title
+ ", book_price=" + book_price + ", book_class=" + book_class
+ ", book_produce=" + book_produce + "]";
}
}
5、輔助操作Excel表的類,功能未寫完
class Excel {
//創建一個文件
public static File createExcel(File file) {
try {
FileOutputStream os = new FileOutputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("信息");
HSSFRow row ;
HSSFCell cell ;
// 創建表頭
row = sheet.createRow(0);
for (int i = 0; i < 5; i++) {
cell = row.createCell(i);
switch (i) {
case 0:cell.setCellValue("圖書編號");break;
case 1:cell.setCellValue("名稱");break;
case 2:cell.setCellValue("價格");break;
case 3:cell.setCellValue("類別");break;
case 4:cell.setCellValue("簡介");break;
}
}
workbook.write(os);
os.close();
System.out.println("創建成功!");
} catch (Exception e) {
}
return file;
}
}