后端:Spring Boot生成Excel文件添加依赖
- 在
pom.xml
中添加Apache POI的依赖:<groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.1.0</version> </dependency><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.1.0</version> </dependency>
- 创建一个用于生成Excel文件的方法
import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; public class ExcelUtil { public static InputStream generateExcel(List<List<String>> data) throws IOException { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); for (int i = 0; i< data.size(); i++) { Row row = sheet.createRow(i); List<String> rowData = data.get(i); for (int j = 0; j < rowData.size(); j++) { Cell cell = row.createCell(j); cell.setCellValue(rowData.get(j)); } } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); workbook.close(); return new ByteArrayInputStream(outputStream.toByteArray()); } }
4.创建一个用于处理Excel文件的Controller
-
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @RestController public class ExcelController { @PostMapping("/generateExcel") public void generateExcel(@RequestBody List<List<String>> data, HttpServletResponse response) throws IOException { InputStream inputStream = ExcelUtil.generateExcel(data); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment; filename=data.xlsx"); response.setHeader("Cache-Control", "no-cache"); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { response.getOutputStream().write(buffer, 0, bytesRead); } inputStream.close(); } }
前端:微信小程序上送数据并下载Excel文件
- 在
app.json
中添加一个用于下载Excel文件的页面:{ "pages": [ "pages/index/index", "pages/download/download" ] }
-
在
pages/download
目录下创建download.wxml
和download.wxss
文件,用于显示下载按钮和提示信息<!-- download.wxml --> <view class="container"> <button class="download-btn" bindtap="downloadExcel">下载Excel</button> <view class="info">{{info}}</view> </view>
/* download.wxss */ .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; } .download-btn { margin-bottom: 20rpx; } .info { text-align: center; }
- 在
pages/download
目录下创建download.js
文件,用于处理下载Excel文件的逻辑:Page({ data: { info: '' }, downloadExcel: function () { const that = this; const data = [ ['姓名', '年龄', '性别'], ['张三', '25', '男'], ['李四', '30', '女'] ]; wx.request({ url: 'https://your-spring-boot-server.com/generateExcel', method: 'POST', data: data, responseType: 'arraybuffer', success: function (res) { const filePath = wx.env.USER_DATA_PATH + '/data.xlsx'; wx.getFileSystemManager().writeFile({ filePath: filePath, data: res.data, encoding: 'binary', success: function () { wx.downloadFile({ filePath: filePath, success: function (res) { that.setData({ info: '下载成功' }); }, fail: function () { that.setData({ info: '下载失败' }); } }); }, fail: function () { that.setData({ info: '写入文件失败' }); } }); }, fail: function () { that.setData({ info: '请求失败' }); } }); } });
请注意将
url
中的域名替换为您的Spring Boot服务器的域名。文章来源:https://www.toymoban.com/news/detail-848202.html现在,当用户点击下载按钮时,微信小程序会将数据上送到Spring Boot服务器,服务器生成Excel文件并返回给小程序,小程序将文件保存到本地并下载。文章来源地址https://www.toymoban.com/news/detail-848202.html
到了这里,关于后端使用Spring Boot生成Excel文件,前端使用微信小程序上送数据并下载Excel文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!