點餐系統上線這段時間,有很多朋友反饋,是否可以添加一個菜肴批量導出的功能。因為平常比較忙,始終沒有時間把食材批量導出的功能加進來。明天剛好空下來時間了,就來教你們實現下食材批量導出的功能。前面會把這節功能錄制成視頻放在點餐系統的課程里。
傳送門:點餐系統,java后臺+點餐小程序
老規矩,先看療效圖
選擇excel食材
導出數據成功
之前有看過我課程的朋友肯定曉得,我之前是沒有批量導出的類目的,不錯,這個類目就是我們明天新加的功能。
實現步驟很簡單:
下邊我們就來具體講解下實現步驟
一,引入excel操作泛型
我們這兒主要用到了下邊紅框里的兩個解釋器
泛型寫在pom.xml里,不要忘掉做操作
二,添加導出excel的后臺網頁
添加食材類目導出頁
添加商品(食材)導出頁
里面的代碼,我會加入到點餐系統里,有訂購點餐系統課程的朋友小程序點餐,去刷新下之前的云盤鏈接即可獲取最新代碼。
三,編撰工具類
把完整代碼給你們貼下來,雖然很簡單小程序點餐,就是在工具類里定義一個導出食材類目和食材的方式。
注意:對應的導出方式是解析excel里的數據,所以你的excel數據必須和我的保持一致,就是第幾列是哪些數據,要和我的對應上去。要不然會造成數據存錯的問題。
package com.qcl.utils;
import com.qcl.dataobject.ProductCategory;
import com.qcl.dataobject.ProductInfo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
/*
* 操作excel
* */
@Slf4j
public class ExcelUtils {
/*
* 菜品類目批量導入
* 要求
* 1,必須以.xlsx結尾的excel文件
* 2,表格內容必須按照下面順序
* 0:類目名,1:type值
*
* */
public static List excelToProductCategoryList(InputStream inputStream) {
List list = new ArrayList<>();
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(inputStream);
inputStream.close();
//工作表對象
Sheet sheet = workbook.getSheetAt(0);
//總行數
int rowLength = sheet.getLastRowNum();
System.out.println("總行數有多少行" + rowLength);
//工作表的列
Row row = sheet.getRow(0);
//總列數
int colLength = row.getLastCellNum();
System.out.println("總列數有多少列" + colLength);
//得到指定的單元格
Cell cell = row.getCell(0);
for (int i = 1; i <= rowLength; i++) {
ProductCategory goodInfo = new ProductCategory();
row = sheet.getRow(i);
for (int j = 0; j < colLength; j++) {
cell = row.getCell(j);
if (cell != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
String data = cell.getStringCellValue();
data = data.trim();
//列:0:類目名,1:type值
if (j == 0) {
goodInfo.setCategoryName(data);
} else if (j == 1) {
goodInfo.setCategoryType(Integer.parseInt(data));
}
}
}
list.add(goodInfo);
// log.error("每行數據={}", menuInfo);
}
} catch (Exception e) {
log.error("excel導入拋出的錯誤={}", e);
}
return list;
}
/*
* 菜品(商品)批量導入
* 要求
* 1,必須以.xlsx結尾的excel文件
* 2,表格內容必須按照下面順序
* 0商品名,1單價,2庫存,3類目,4描述,5圖片鏈接
*
* */
public static List excelToProductInfoList(InputStream inputStream) {
List list = new ArrayList<>();
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(inputStream);
inputStream.close();
//工作表對象
Sheet sheet = workbook.getSheetAt(0);
//總行數
int rowLength = sheet.getLastRowNum();
//工作表的列
Row row = sheet.getRow(0);
//總列數
int colLength = row.getLastCellNum();
//得到指定的單元格
Cell cell = row.getCell(0);
for (int i = 1; i <= rowLength; i++) {
ProductInfo goodInfo = new ProductInfo();
row = sheet.getRow(i);
for (int j = 0; j < colLength; j++) {
cell = row.getCell(j);
if (cell != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
String data = cell.getStringCellValue();
data = data.trim();
//列: 0商品名,1單價,2庫存,3類目,4描述,5圖片鏈接
if (j == 0) {
goodInfo.setProductId(KeyUtil.genUniqueKey());
goodInfo.setProductName(data);
} else if (j == 1) {
goodInfo.setProductPrice(new BigDecimal(data));
} else if (j == 2) {
goodInfo.setProductStock(Integer.parseInt(data));
} else if (j == 3) {
goodInfo.setCategoryType(Integer.parseInt(data));
} else if (j == 4) {
goodInfo.setProductDescription(data);
} else if (j == 5) {
goodInfo.setProductIcon(data);
}
}
}
list.add(goodInfo);
}
} catch (Exception e) {
log.error("excel導入拋出的錯誤={}", e);
}
return list;
}
}
四,編撰對應的插口
里面的工具類封裝好之后,我們接出來就須要在對應的類里添加導出數據的方式了。
/*
* excel導入網頁
* */
@GetMapping("/excel")
public ModelAndView excel(Map map) {
return new ModelAndView("category/excel", map);
}
/*
* 批量導入excel里的菜品類目到數據庫
* */
@RequestMapping("/uploadExcel")
@ResponseBody
public ModelAndView uploadExcel(@RequestParam("file") MultipartFile file,
Map map) {
String name = file.getOriginalFilename();
if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
map.put("msg", "文件格式錯誤");
map.put("url", "/sell/seller/category/excel");
return new ModelAndView("common/error", map);
}
List list;
try {
list = ExcelUtils.excelToProductCategoryList(file.getInputStream());
log.info("excel導入的list={}", list);
if (list == null || list.size() <= 0) {
map.put("msg", "導入失敗");
map.put("url", "/sell/seller/category/excel");
return new ModelAndView("common/error", map);
}
//excel的數據保存到數據庫
try {
for (ProductCategory excel : list) {
if (excel != null) {
//如果類目type值已存在,就不再導入
List typeList = categoryService.findOneByType(excel.getCategoryType());
log.info("查詢類目type是否存在typeList={}", typeList);
if (typeList == null || typeList.size() < 1) {
System.out.println("保存成功");
categoryService.save(excel);
}
}
}
} catch (Exception e) {
log.error("某一行存入數據庫失敗={}", e);
}
} catch (Exception e) {
e.printStackTrace();
map.put("msg", e.getMessage());
map.put("url", "/sell/seller/category/excel");
return new ModelAndView("common/error", map);
}
map.put("url", "/sell/seller/category/list");
return new ModelAndView("common/success", map);
}
/*
* excel導入網頁
* */
@GetMapping("/excel")
public ModelAndView excel(Map map) {
return new ModelAndView("product/excel", map);
}
/*
* 批量導入excel里的菜品(商品)到數據庫
* */
@RequestMapping("/uploadExcel")
@ResponseBody
public ModelAndView uploadExcel(@RequestParam("file") MultipartFile file,
Map map) {
String name = file.getOriginalFilename();
if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
map.put("msg", "文件格式錯誤");
map.put("url", "/sell/seller/product/excel");
return new ModelAndView("common/error", map);
}
List list;
try {
list = ExcelUtils.excelToProductInfoList(file.getInputStream());
log.info("excel導入的list={}", list);
if (list == null || list.size() <= 0) {
map.put("msg", "導入失敗");
map.put("url", "/sell/seller/product/excel");
return new ModelAndView("common/error", map);
}
//excel的數據保存到數據庫
try {
for (ProductInfo excel : list) {
if (excel != null) {
//如果類目type值已存在,就不再導入
productService.save(excel);
}
}
} catch (Exception e) {
log.error("某一行存入數據庫失敗={}", e);
}
} catch (Exception e) {
e.printStackTrace();
map.put("msg", e.getMessage());
map.put("url", "/sell/seller/product/excel");
return new ModelAndView("common/error", map);
}
map.put("url", "/sell/seller/product/list");
return new ModelAndView("common/success", map);
}
到這兒我們就完整的實現了點餐系統批量導出食材數據到數據庫的功能了。
完整的代碼我會放在點餐系統里,訂購我點餐系統課程的朋友,記得去刷新下之前的云盤鏈接,就可以獲取最新的代碼了
演示視頻也早已錄制下來了,你們可以去看下
完整的點餐系統,包含Java后臺和掃碼點餐小程序,療效圖如下。
免責聲明:部分文章信息來源于網絡以及網友投稿,本站只負責對文章進行整理、排版、編輯,出于傳遞更多信息之目的,并不意味著贊同其觀點或證實其內容的真實性,如本站文章和轉稿涉及版權等問題,請作者在及時聯系本站,我們會盡快為您處理。