一、頁面效果:
二、主要功能:
1、CRUD的操作
2、批量刪除
3、將Excel導入到數據庫
4、將數據表導出到Excel中
三、前端代碼:
1、頁面代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="../js/index.css">
<script src="../js/vue.js" type="text/javascript"></script>
<script src="../js/elementUI.js" type="text/javascript"></script>
<script src="../js/axios.js" type="text/javascript"></script>
</head>
<body>
<div id="app" style="margin-left: 25px;margin-top: 15px;">
<el-form :model="formData" :rules="rules" ref="formData" label-width="100px">
<el-row>
<el-col :span="6">
<el-form-item label="圖書名稱" prop="bookName">
<el-input v-model="formData.bookName"></el-input>
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="圖書作者" prop="bookAuthor">
<el-input v-model="formData.bookAuthor"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="6">
<el-form-item label="發行日期" prop="bookDate">
<el-input type="date" v-model="formData.bookDate"></el-input>
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="圖書價格" prop="bookPrice">
<el-input v-model="formData.bookPrice" ></el-input>
</el-form-item>
</el-col>
</el-row>
<el-col>
<el-form-item>
<el-button type="primary" icon="el-icon-plus" size="mini" @click="addBook('formData')">增加</el-button>
<el-button type="danger" icon="el-icon-delete" size="mini" @click="batchDelete">批量刪除</el-button>
<el-button icon="el-icon-search" size="mini" @click="searchBook">查詢</el-button>
<el-button type="success" icon="el-icon-right" size="mini" @click="importBook">導入Excel</el-button>
<el-button type="warning" icon="el-icon-back" size="mini" @click="exportBook">導出Excel</el-button>
</el-form-item>
</el-col>
</el-form>
<!--表格-->
<el-table :data="tableData" style="width: 1050px;" ref="multipleTable" @selection-change="handleSelectionChange" id="out-table">
<el-table-column min-width='140' type="selection"></el-table-column>
<el-table-column prop="bookId" label="圖書編號" width="150"> </el-table-column>
<el-table-column prop="bookName" label="圖書名稱" width="180"> </el-table-column>
<el-table-column prop="bookAuthor" label="圖書作者" width="150"> </el-table-column>
<el-table-column prop="bookDate" label="發行日期" width="150"> </el-table-column>
<el-table-column prop="bookPrice" label="圖書價格" width="150"> </el-table-column>
<el-table-column label="操作" width="210" align="center">
<template slot-scope="scope">
<el-button type="danger" icon="el-icon-delete" size="mini" @click="Delete(scope.row.bookId)">刪除</el-button>
<el-button type="success" icon="el-icon-edit" size="mini" @click="Update(scope.row)">修改</el-button>
</template>
</el-table-column>
</el-table>
<!--分頁組件-->
<el-pagination class="tabListPage"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="pageSizes"
:page-size="pageCount"
layout="total, sizes, prev, pager, next, jumper"
:total="totalCount"
>
</el-pagination>
<!--修改對話框-->
<el-dialog customClass="customWidth"
:modal="true"
:visible.sync="dialogVisible"
width="25%"
:title="Title"
>
<!--"修改"對話框中的表單-->
<div style="margin-right: 20px;">
<el-form :model="updateData" :rules="rules" ref="updateData" label-width="100px">
<el-row>
<el-col>
<el-form-item label="圖書編號" prop="update_bookId">
<el-input v-model="updateData.update_bookId" disabled></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col>
<el-form-item label="圖書名稱" prop="update_bookName">
<el-input v-model="updateData.update_bookName"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col>
<el-form-item label="圖書作者" prop="update_bookAuthor">
<el-input v-model="updateData.update_bookAuthor"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col>
<el-form-item label="發行日期" prop="update_bookDate">
<el-input type="date" v-model="updateData.update_bookDate"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col>
<el-form-item label="圖書價格" prop="update_bookPrice">
<el-input v-model="updateData.update_bookPrice"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
</div>
<!-- 底部按鈕 -->
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="handlerAddOk">確 定</el-button>
</div>
</el-dialog>
<!--導入Excel對話框-->
<el-dialog customClass="customWidth"
:modal="true"
:visible.sync="ImportdialogVisible"
width="29%"
:title="ImportTitle"
>
<el-form :model="form">
<el-form-item>
<el-upload class="upload-file"
drag
ref="uploadExcel"
action="http://localhost:8080/elementui/import"
:limit=limitNum
:auto-upload="false"
accept=".xlsx"
:before-upload="beforeUploadFile"
:on-change="fileChange"
:on-exceed="exceedFile"
:on-success="handleSuccess"
:on-error="handleError"
:file-list="fileList"
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">將文件拖到此處,或<em>點擊選擇Excel文件</em></div>
<!-- <div slot="tip" class="el-upload-list__item-name">{{fileName}}</div> -->
</el-upload>
</el-form-item>
<el-form-item>
<div style="margin-left: 120px;">
<el-button @click="ImportdialogVisible = false" size="small">取 消</el-button>
<el-button size="small" type="primary" @click="submitUpload()">立即導入</el-button>
</div>
</el-form-item>
</el-form>
</el-dialog>
</div>
2、JS代碼:
<script>
new Vue({
el:'#app',
data:{
tableData:[], //表格綁定的數據
multipleSelection:[], //多選時綁定的數據
currentPage:1,// 默認顯示第幾頁
totalCount:1,// 總條數,根據接口獲取數據長度(注意:這里不能為空)
pageSizes:[5,10,15,20,25,30],// 個數選擇器(可修改)
pageCount:5,// 默認每頁顯示的條數(可修改)
formData:{ //"新增"表單數據的初始化
bookName:'',
bookAuthor:'',
bookDate:'',
bookPrice:''
},
updateData:{ //"修改"對話框中表單數據的初始化
update_bookName:'',
update_bookAuthor:'',
update_bookDate:'',
update_bookPrice:''
},
dialogVisible:false, //"修改"對話框是否可見
Title:"修改記錄", //"修改"對話框的標題
/* --- 以下導入Excel文件對話框綁定的數據 --- */
ImportdialogVisible:false, //"導入"對話框是否可見
ImportTitle:'導入Excel', //"導入"對話框的標題
limitNum: 1,//導入文件的個數
form: { //"導入"表單綁定file,並初始化
file: ''
},
fileList: [],//"導入"文件列表
rules:{ //表單驗證
bookName:[{ required:true,message:'名稱不能為空',trigger:'blur'}],
bookAuthor:[{required:true,message:'作者不能為空',trigger:'blur'}],
bookDate:[{required:true,message:'發行日期不能為空',trigger:'blue'}],
bookPrice:[{required:true,message:'圖書價格不能為空',trigger:'blue'}]
}
},
methods:{
getData(){ //初始化獲取后台數據的函數
let self = this;
axios({
url:"http://localhost:8080/elementui/books",
method:"get",
params:{
"CurrentPage":self.currentPage,
"PageSize":self.pageCount
}
}).then(response=>{
console.log(response.data);
self.tableData = response.data.pageList;
self.totalCount = response.data.recordTotal;
console.log(self.tableData)
})
},
handleSizeChange(val) { //每頁顯示的記錄數(下拉列表)發生改變時綁定的函數
console.log(`每頁 ${val} 條`);
let self = this;
axios({
url:"http://localhost:8080/elementui/books",
methods:"get",
params:{
"CurrentPage":self.currentPage,
"PageSize":val
}
}).then(response=>{
self.tableData = response.data.pageList;
})
},
handleCurrentChange(val) { //"當前頁"發生改變時綁定的函數
console.log(`當前頁: ${val}`);
let self = this;
axios({
url:"http://localhost:8080/elementui/books",
methods:"get",
params:{
"CurrentPage":val,
"PageSize":self.pageCount
}
}).then(response=>{
self.tableData = response.data.pageList;
})
},
Delete(val){ //"刪除"按鈕綁定的函數
let self = this;
console.info("ID:"+val);
self.$confirm("確定刪除嗎?","操作提示",{
confirmButtonText:"確定",
cancelButtonText:"取消",
type:"warning"
}).then(()=>{ //當用戶點擊"確定"時,向后台發送刪除請求
axios({
url:"http://localhost:8080/elementui/delete",
methods:"get",
params:{
"bookId":val
}
}).then(response=>{
console.info(response.data)
if(response.data==true){
self.getData();
self.$message({
type:"success",
message:"操作成功",
offset:100,
center:true,
})
}else{
self.$message({
type:"error",
message:"操作失敗",
offset:100,
center:true,
})
}
})
}).catch(()=>{
})
},
Update(row){ //點擊"修改"按鈕彈出修改對話框
let self = this
self.dialogVisible = true;
self.updateData.update_bookId = row.bookId;
self.updateData.update_bookName = row.bookName;
self.updateData.update_bookAuthor = row.bookAuthor;
self.updateData.update_bookDate = row.bookDate;
self.updateData.update_bookPrice = row.bookPrice;
},
handlerAddOk(){ //修改對話框上的"確定"按鈕
let self = this;
axios({
url:"http://localhost:8080/elementui/update",
method:"get",
params:{
"bookId":self.updateData.update_bookId,
"bookName":self.updateData.update_bookName,
"bookAuthor":self.updateData.update_bookAuthor,
"bookDate":self.updateData.update_bookDate,
"bookPrice":self.updateData.update_bookPrice
}
}).then(response=>{
if(response.data == true){
self.getData();
self.$message({
type:"success",
message:"操作成功",
offset:100,
center:true,
})
}else{
self.$message({
type:"error",
message:"操作失敗",
offset:100,
center:true,
})
}
self.dialogVisible = false;
})
},
addBook(formName){ //"增加"按鈕綁定的函數
let self = this;
self.$refs[formName].validate((valid)=>{
if(valid){
axios({
url:"http://localhost:8080/elementui/insert",
method:"get",
params:{
bookName:self.formData.bookName,
bookAuthor:self.formData.bookAuthor,
bookDate:self.formData.bookDate,
bookPrice:self.formData.bookPrice
}
}).then(response=>{
if(response.data==true){
self.getData();
self.$message({
type:"success",
message:"操作成功",
offset:100,
center:true,
});
self.$refs[formName].resetFields();
}else{
self.$message({
type:"error",
message:"操作失敗",
offset:100,
center:true,
})
}
})
}
})
},
batchDelete(){ //"批量刪除"按鈕綁定的函數
let self = this;
let sid = '';
if(self.multipleSelection.length!=0){
self.$confirm("確定刪除嗎?","操作提示",{
confirmButtonText:"確定",
cancelButtonText:"取消",
type:"warning"
}).then(()=>{ //當用戶點擊"確定"時,獲取用戶選擇的行的id,並將id拼接成字符串提交給后台
for(let i=0;i<self.multipleSelection.length;i++){
sid = sid+self.multipleSelection[i].bookId;
sid = sid+",";
}
sid = sid.substring(0,sid.length-1);
console.log(sid);
axios({
url:"http://localhost:8080/elementui/batchDelete",
method:"get",
params:{
ids:sid
}
}).then(response=>{
console.info(response.data)
if(response.data==true){
self.getData();
self.$message({
type:"success",
message:"操作成功",
offset:100,
center:true,
})
}else{
self.$message({
type:"error",
message:"操作失敗",
offset:100,
center:true,
})
}
})
}).catch(()=>{
})
}else{
self.$message({
type:"error",
message:"請選擇要刪除的數據",
offset:100,
center:true,
})
}
},
searchBook(){ //"查詢"按鈕綁定的函數
},
importBook(){ //彈出導入對話框
let self = this;
self.ImportdialogVisible = true;
},
// 文件超出個數限制時的鈎子
exceedFile(files, fileList) {
this.$notify.warning({
title: '警告',
message: `只能選擇 ${this.limitNum} 個文件,當前共選擇了 ${files.length + fileList.length} 個`
});
},
// 文件狀態改變時的鈎子
fileChange(file, fileList) {
console.log('change')
console.log(file)
this.form.file = file.raw
console.log(this.form.file)
console.log(fileList)
},
// 上傳文件之前的鈎子, 參數為上傳的文件,若返回 false 或者返回 Promise 且被 reject,則停止上傳
beforeUploadFile(file) {
console.log('before upload')
console.log(file)
// let extension = file.name.substring(file.name.lastIndexOf('.')+1); //獲取上傳文件的擴展名
console.log("文件擴展名為:"+extension);
let size = file.size / 1024 / 1024; //設置上傳文件的大小
const isXLSX = file.name.split(".")[1] === 'xlsx'; //獲取上傳文件的擴展名
if(!isXLSX){
this.$notify.warning({
title: '警告',
message: `只能上傳Excel2017(即后綴是.xlsx)的文件`
});
}
if(size > 10) {
this.$notify.warning({
title: '警告',
message: `文件大小不得超過10M`
});
}
},
// 文件上傳成功時的鈎子
handleSuccess(res, file, fileList) {
let self = this;
this.$notify.success({
title: '成功',
message: `文件導入成功`
});
this.getData();
self.ImportdialogVisible = false;
},
// 文件上傳失敗時的鈎子
handleError(err, file, fileList) {
this.$notify.error({
title: '錯誤',
message: `文件導入失敗`
});
},
submitUpload(){
let self = this;
if(self.form.file == ''){
this.$notify.error({
title: '錯誤',
message: `請選擇要導入的文件`
});
return;
}
self.$refs.uploadExcel.submit();
},
exportBook(){
//這里不能用ajax請求,ajax請求無法彈出下載保存對話框
location.href="http://localhost:8080/elementui/export";
},
handleSelectionChange(rows){ //用戶在表格上選擇行時綁定的函數
let self = this;
self.multipleSelection = rows;
}
},
created:function(){
this.getData()
}
})
</script>
二、后台代碼
1、目錄結構:
2、依賴jar包:
<!--POI-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!--上傳-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
3、配置文件:
server:
port: 8080
servlet:
context-path: /elementui
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mvc?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
maxActive: 20
initialSize: 5
minIdle: 3
maxWait: 10000
mybatis:
type-aliases-package: com.vue.elementui.entity
4、實體類:
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Book implements Serializable {
private Integer bookId;
private String bookName;
private String bookAuthor;
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
private Date bookDate;
private Double bookPrice;
}
5、SQL語句生成器類:
public class BookSQLProvider {
public String selectSQL(){ //查詢所有
return new SQL() {
{
SELECT("*");
FROM("ssm_book");
}
}.toString();
}
public String findByIdSQL(Integer id){ //按Id查詢
return new SQL(){
{
SELECT("*");
FROM("ssm_book");
WHERE("book_id=#{id}");
}
}.toString();
}
public String insertSQL(Book book){ //插入
return new SQL(){
{
INSERT_INTO("ssm_book");
VALUES("book_name","#{bookName}");
VALUES("book_author","#{bookAuthor}");
VALUES("book_date","#{bookDate}");
VALUES("book_price","#{bookPrice}");
}
}.toString();
}
public String deleteSQL(Integer id){ //刪除
return new SQL(){
{
DELETE_FROM("ssm_book");
WHERE("book_id=#{id}");
}
}.toString();
}
public String updateSQL(Book book){ //更新
return new SQL(){
{
UPDATE("ssm_book");
SET("book_name=#{bookName}");
SET("book_author=#{bookAuthor}");
SET("book_date=#{bookDate}");
SET("book_price=#{bookPrice}");
WHERE("book_id=#{bookId}");
}
}.toString();
}
public String pageListSQL(Integer start,Integer count){ //分頁查詢
return new SQL(){
{
SELECT("*");
FROM("ssm_book");
LIMIT("#{start},#{count}");
}
}.toString();
}
}
6、代理(Mapper):
@Mapper
@Repository
public interface IBookMapper {
@SelectProvider(type = com.vue.elementui.provider.BookSQLProvider.class,method = "selectSQL")
@Results(id = "bookMap",value = {
@Result(id = true,property = "bookId",column = "book_id"),
@Result(property = "bookName",column = "book_name"),
@Result(property = "bookAuthor",column = "book_author"),
@Result(property = "bookDate",column = "book_date"),
@Result(property = "bookPrice",column = "book_price")
})
public List<Book> findAll();
@SelectProvider(type = com.vue.elementui.provider.BookSQLProvider.class,method = "findByIdSQL")
@ResultMap(value = "bookMap")
public Book findById(Integer id);
@SelectProvider(type = com.vue.elementui.provider.BookSQLProvider.class,method = "pageListSQL")
@ResultMap(value = "bookMap")
public List<Book> findPageBook(Integer start,Integer count);
@InsertProvider(type = com.vue.elementui.provider.BookSQLProvider.class,method ="insertSQL" )
public int insert(Book book);
@DeleteProvider(type = com.vue.elementui.provider.BookSQLProvider.class,method ="deleteSQL")
public int delete(Integer id);
@UpdateProvider(type = com.vue.elementui.provider.BookSQLProvider.class,method = "updateSQL")
public int update(Book book);
}
7、服務層接口:
public interface IBookService {
public int addBook(Book book); //插入
public int removeBook(Integer id);//刪除
public int modifyBook(Book book);//更新
public List<Book> getBooks();//查詢所有
public Book findBookById(Integer id);//按Id查詢
public List<Book> getPageBook(Integer start,Integer count);//分頁查詢
public boolean batchImport(String fileName, MultipartFile file) throws Exception;//導入Excel
public HSSFWorkbook exportToExcel(List<Book> books);//導出到Excel
}
8、服務層實現類:
@Service
@Transactional
public class BookService implements IBookService {
@Autowired
private IBookMapper bookMapper;
@Override
public int addBook(Book book) {
return bookMapper.insert(book);
}
@Override
public int removeBook(Integer id) {
return bookMapper.delete(id);
}
@Override
public int modifyBook(Book book) {
return bookMapper.update(book);
}
@Override
public List<Book> getBooks() {
return bookMapper.findAll();
}
@Override
public Book findBookById(Integer id) {
return bookMapper.findById(id);
}
@Override
public List<Book> getPageBook(Integer start, Integer count) {
return bookMapper.findPageBook(start,count);
}
@Override
public boolean batchImport(String fileName, MultipartFile file) throws Exception {
boolean notnull = false;
List<Book> bookList = new ArrayList<>();
if(!fileName.matches("^.+\\.(?i)(xlsx)$")){ //
throw new Exception("上傳文件格式不正確");
}
boolean isExcel2007 = true;
if(fileName.matches("^.+\\.(?i)(xls)$")){
isExcel2007 = false;
}
InputStream is = file.getInputStream();
Workbook wb = null;
if (isExcel2007) {
wb = new HSSFWorkbook(is);
}else{
wb = new XSSFWorkbook(is);
}
Sheet sheet = wb.getSheetAt(0);
if(sheet != null){
notnull = true;
}
Book book = null;
for(int r=2;r<=sheet.getLastRowNum();r++){ //r = 2 表示從第三行開始循環 如果你的第三行開始是數據
Row row = sheet.getRow(r);//通過sheet表單對象得到行對象
if(row == null){
continue;
}
//sheet.getLastRowNum() 的值是 10,所以Excel表中的數據至少是10條;不然報錯 NullPointerException
book = new Book();
row.getCell(0).setCellType(CellType.STRING);//將每一行第一個單元格設置為字符串類型
String bId =row.getCell(0).getStringCellValue();//得到每一行第一個單元格的值
if(bId == null || bId.isEmpty()){
throw new Exception("導入失敗(第\"+(r+1)+\"行,圖書編號未填寫)");
}
Integer bookId = Integer.parseInt(bId);
String bookName = row.getCell(1).getStringCellValue();//得到每一行的第二個單元格的值
if(bookName == null || bookName.isEmpty()){
throw new Exception("導入失敗(第\"+(r+1)+\"行,圖書名稱未填寫)");
}
String bookAuthor = row.getCell(2).getStringCellValue();//得到每一行的第二個單元格的值
if(bookAuthor == null || bookAuthor.isEmpty()){
throw new Exception("導入失敗(第\"+(r+1)+\"行,圖書作者未填寫)");
}
row.getCell(3).setCellType(CellType.STRING);
Date bookDate =DateConvert.StringToDate(row.getCell(3).getStringCellValue());//得到每一行的第三個單元格的值(日期型)
if(bookDate == null){
throw new Exception("導入失敗(第\"+(r+1)+\"行,圖書發行日期未填寫)");
}
row.getCell(4).setCellType(CellType.STRING);//將每一行第四個單元格設置為字符串類型
String bPrice =row.getCell(4).getStringCellValue();//得到每一行的第四個單元格的值(日期型)
if(bPrice == null || bPrice.isEmpty()){
throw new Exception("導入失敗(第\"+(r+1)+\"行,圖書價格未填寫)");
}
Double bookPrice = Double.parseDouble(bPrice);
//完整的循環一次 就組成了一個對象
book.setBookId(bookId);
book.setBookName(bookName);
book.setBookAuthor(bookAuthor);
book.setBookDate(bookDate);
book.setBookPrice(bookPrice);
bookList.add(book);
}
for(Book bookResord : bookList){
int id = bookResord.getBookId();
Book b1 = bookMapper.findById(id);
if(b1 == null){
bookMapper.insert(bookResord);
System.out.println("==>插入:"+bookResord);
}else{
bookMapper.update(bookResord);
System.out.println("==>更新:"+bookResord);
}
}
return notnull;
}
@Override
public HSSFWorkbook exportToExcel(List<Book> books) { //將集合中的數據存儲到Execl工作簿中
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("book_info");
HSSFRow row = null;
row = sheet.createRow(0);//創建第一行
row.setHeight((short) 800);// 設置行高
HSSFCell c00 = row.createCell(0); //創建第一個單元格
c00.setCellValue("圖書列表");//設置單元格內容
//設置標題樣式
c00.setCellStyle(ExcelImportUtils.createTitleCellStyle(wb));
//合並單元格(firstRow:起始行,lastRow:結束行,firstCol:起始列,lastCol:結束列)
CellRangeAddress rowRegion = new CellRangeAddress(0,0,0,4);
sheet.addMergedRegion(rowRegion);
//創建表頭行,並設置樣式
row = sheet.createRow(1); //創建第二行
row.setHeight((short)500);//設置行高
String[] row_head = {"圖書編號","圖書名稱","圖書作者","發行時間","圖書單價"};
for(int i=0;i<row_head.length;i++){ //創建表頭
HSSFCell tempCell = row.createCell(i);
tempCell.setCellValue(row_head[i]); //設置單元格內容
//設置表頭樣式
tempCell.setCellStyle(ExcelImportUtils.createHeadCellStyle(wb));
}
//定義表格內容(每行數據)
//集合(books)中有多少個元素就生成多少行
for(int i=0;i<books.size();i++) {
row = sheet.createRow(i + 2);
Book book = books.get(i);
for (int j = 0; j < 5; j++) { //每行有5列
HSSFCell tempCell = row.createCell(j); //設置單元格內容
//設置內容樣式
tempCell.setCellStyle(ExcelImportUtils.createContentCellStyle(wb));
if (j == 0) {
tempCell.setCellValue(book.getBookId());
} else if (j == 1) {
tempCell.setCellValue(book.getBookName());
} else if (j == 2) {
tempCell.setCellValue(book.getBookAuthor());
} else if (j == 3) {
tempCell.setCellValue(DateConvert.DateToString(book.getBookDate()));
} else if (j == 4) {
tempCell.setCellValue(book.getBookPrice());
}
}
}
// sheet.setDefaultRowHeight((short)(16.5*20)); 設置默認行高
//列寬自適應
for(int i=0;i<5;i++){
sheet.autoSizeColumn(i);
}
return wb;
}
}
9、工具類:
(1)日期轉換:
public class DateConvert {
public static Date StringToDate(String str){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static String DateToString(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
}
(2)分頁類:
public class Pagination<T> {
public Integer currentPage; //當前頁
public Integer pageSize; //每頁顯示的記錄數
public Integer recordTotal;//記錄總數
public Integer pageCount;//總頁數
public List<T> pageList;//分頁數據
public Pagination() {
this.currentPage = 1;
this.pageSize = 5;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getRecordTotal() {
return recordTotal;
}
public void setRecordTotal(Integer recordTotal) {
this.recordTotal = recordTotal;
if(this.recordTotal % this.pageSize ==0){
this.pageCount = this.recordTotal/this.pageSize;
}else{
this.pageCount = this.recordTotal/this.pageSize + 1;
}
}
public List<T> getPageList() {
return pageList;
}
public void setPageList(List<T> pageList) {
this.pageList = pageList;
}
}
(3)Excel導入導出工具類:
public class ExcelImportUtils {
//@描述:判斷是否是2003版的excel,返回true是2003
public static boolean isExcel2003(String filePath){
return filePath.matches("^.+\\.(?i)(xls)$");
}
//@描述:判斷是否是2007版的Excel,返回true是2007
public static boolean isExcel2007(String filePath){
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
/*
@描述:驗證excel文件
@param:filePath
@return
*/
public static boolean validateExcel(String filePath){
if(filePath == null ||!(isExcel2003(filePath))||!(isExcel2007(filePath))){
return false;
}else{
return true;
}
}
/**
* 創建標題樣式
* @param wb
* @return
*/
public static HSSFCellStyle createTitleCellStyle(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直對齊
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//背景顏色 cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 創建字體樣式
headerFont1.setBold(true); //字體加粗
headerFont1.setFontName("黑體"); // 設置字體類型
headerFont1.setFontHeightInPoints((short) 15); // 設置字體大小
cellStyle.setFont(headerFont1); // 為標題樣式設置字體樣式
return cellStyle;
}
/**
* 創建表頭樣式
* @param wb
* @return
*/
public static HSSFCellStyle createHeadCellStyle(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setWrapText(true);// 設置自動換行
//背景顏色 cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直對齊
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
cellStyle.setBorderTop(BorderStyle.THIN); //上邊框
HSSFFont headerFont = (HSSFFont) wb.createFont(); // 創建字體樣式
headerFont.setBold(true); //字體加粗
headerFont.setFontName("黑體"); // 設置字體類型
headerFont.setFontHeightInPoints((short) 12); // 設置字體大小
cellStyle.setFont(headerFont); // 為標題樣式設置字體樣式
return cellStyle;
}
/**
* 創建內容樣式
* @param wb
* @return
*/
public static HSSFCellStyle createContentCellStyle(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
cellStyle.setWrapText(true);// 設置自動換行
cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
cellStyle.setBorderTop(BorderStyle.THIN); //上邊框
// 生成12號字體
HSSFFont font = wb.createFont();
font.setColor((short)8);
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
return cellStyle;
}
}
10、控制器類:
@CrossOrigin
@RestController
public class BookController {
@Autowired
private IBookService bookService;
private Pagination<Book> pagination = new Pagination<>();
//private Integer records = bookService.getBooks().size();
@GetMapping("/books")
public String getBooks(String CurrentPage,String PageSize){
pagination.setRecordTotal(bookService.getBooks().size());
int current_page = Integer.parseInt(CurrentPage);
int page_size = Integer.parseInt(PageSize);
pagination.setCurrentPage(current_page);
pagination.setPageSize(page_size);
int start = pagination.getCurrentPage()*pagination.getPageSize()-pagination.getPageSize();
pagination.setPageList(bookService.getPageBook(start,pagination.getPageSize()));
ObjectMapper objectMapper = new ObjectMapper();
String pagebooks = null;
try {
pagebooks = objectMapper.writeValueAsString(pagination);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return pagebooks;
}
@GetMapping("/insert")
public boolean insertBook(HttpServletRequest request){
String name = request.getParameter("bookName");
String author = request.getParameter("bookAuthor");
Date date = DateConvert.StringToDate(request.getParameter("bookDate"));
Double price = Double.parseDouble(request.getParameter("bookPrice"));
Book book = new Book(null,name,author,date,price);
int flag = bookService.addBook(book);
return flag>0;
}
@GetMapping("/delete")
public boolean deleteBook(Integer bookId){
int flag = bookService.removeBook(bookId);
return flag>0;
}
@GetMapping("/batchDelete")
public boolean batchDeleteBook(String ids){
String[] str = ids.split(",");
int flag = 0;
for(int i=0;i<str.length;i++){
int id = Integer.parseInt(str[i]);
flag = bookService.removeBook(id);
}
return flag>0;
}
@GetMapping("/update")
public boolean updateBook(HttpServletRequest request){
Integer id = Integer.parseInt(request.getParameter("bookId"));
String name = request.getParameter("bookName");
String author = request.getParameter("bookAuthor");
Date date = DateConvert.StringToDate(request.getParameter("bookDate"));
Double price = Double.parseDouble(request.getParameter("bookPrice"));
Book book = new Book(id,name,author,date,price);
int flag = bookService.modifyBook(book);
return flag>0;
}
@PostMapping("/import")
public boolean exImport(@RequestParam("file") MultipartFile file){
boolean a = false;
String fileName = file.getOriginalFilename();
System.out.println(fileName);
try {
a = bookService.batchImport(fileName,file);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(a);
return a;
}
@GetMapping("/export")
public void export(HttpServletResponse response) throws IOException {
List<Book> books = bookService.getBooks();
HSSFWorkbook wb = bookService.exportToExcel(books);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=book.xlsx"); //默認Excel名稱
wb.write(os);
os.flush();
os.close();
}
}