后端:Spring Boot生成Excel文件添加依赖
- 在pom.xml中添加Apache POI的依赖:
org.apache.poi poi5.1.0 org.apache.poi poi-ooxml5.1.0 - 创建一个用于生成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
- > 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
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
- > 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文件,用于显示下载按钮和提示信息
{{info}} /* 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服务器的域名。
现在,当用户点击下载按钮时,微信小程序会将数据上送到Spring Boot服务器,服务器生成Excel文件并返回给小程序,小程序将文件保存到本地并下载。
还没有评论,来说两句吧...