当前位置: 首页 > news >正文

电子商务网站建设的步骤过程鞍山seo优化

电子商务网站建设的步骤过程,鞍山seo优化,wordpress采集插件推荐,曲阜住房城乡建设局网站本文将详细介绍如何使用 Spring Boot 和 Apache POI 实现 Excel 文件的导出功能,帮助开发者快速上手。 1. 准备工作 首先,确保你的 Spring Boot 项目已成功创建并运行。接下来,需要在 pom.xml 文件中添加 Apache POI 相关依赖,以…

本文将详细介绍如何使用 Spring Boot 和 Apache POI 实现 Excel 文件的导出功能,帮助开发者快速上手。

1. 准备工作

首先,确保你的 Spring Boot 项目已成功创建并运行。接下来,需要在 pom.xml 文件中添加 Apache POI 相关依赖,以支持 Excel 文件的读写操作。

Maven 依赖

pom.xml 中添加以下依赖:

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.3</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version>
</dependency>

2. 创建数据模型

我们需要一个数据模型来表示要导出的数据。例如,我们可以创建一个 RoadTrafficFeatures 类,包含驾驶员数量、汽车数量、道路里程和地区等属性。

public class RoadTrafficFeatures {private int driverNum;private int carsNum;private double roadMileage;private String area;public RoadTrafficFeatures(int driverNum, int carsNum, double roadMileage, String area) {this.driverNum = driverNum;this.carsNum = carsNum;this.roadMileage = roadMileage;this.area = area;}// Getters
}

3. 实现 Excel 导出服务

接下来,我们将实现一个服务类 ExcelExportService,负责生成 Excel 文件。我们将使用 Apache POI 创建一个新的 Excel 工作簿,并填充数据。

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;@Service
public class ExcelExportService {public ByteArrayOutputStream exportToExcel(List<RoadTrafficFeatures> featuresList) throws IOException {Workbook workbook = new XSSFWorkbook();Sheet sheet = workbook.createSheet("Road Traffic Features");// 设置列宽sheet.setColumnWidth(0, 4000); // Driver Numsheet.setColumnWidth(1, 4000); // Cars Numsheet.setColumnWidth(2, 6000); // Road Mileagesheet.setColumnWidth(3, 4000); // Area// 创建样式CellStyle headerStyle = createCellStyle(workbook, true);CellStyle normalStyle = createCellStyle(workbook, false);// 创建表头Row headerRow = sheet.createRow(0);String[] headers = {"Driver Num", "Cars Num", "Road Mileage", "Area"};for (int i = 0; i < headers.length; i++) {Cell cell = headerRow.createCell(i);cell.setCellValue(headers[i]);cell.setCellStyle(headerStyle);}// 填充数据int rowNum = 1;for (RoadTrafficFeatures feature : featuresList) {Row row = sheet.createRow(rowNum++);createCell(row, 0, feature.getDriverNum(), normalStyle);createCell(row, 1, feature.getCarsNum(), normalStyle);createCell(row, 2, feature.getRoadMileage(), normalStyle);createCell(row, 3, feature.getArea(), normalStyle);}// 写入到输出流ByteArrayOutputStream outputStream = new ByteArrayOutputStream();workbook.write(outputStream);workbook.close();return outputStream;}private CellStyle createCellStyle(Workbook workbook, boolean isHeader) {CellStyle style = workbook.createCellStyle();if (isHeader) {Font font = workbook.createFont();font.setBold(true);style.setFont(font);}style.setBorderTop(BorderStyle.THIN);style.setBorderBottom(BorderStyle.THIN);style.setBorderLeft(BorderStyle.THIN);style.setBorderRight(BorderStyle.THIN);style.setAlignment(HorizontalAlignment.CENTER);style.setVerticalAlignment(VerticalAlignment.CENTER);return style;}private void createCell(Row row, int columnIndex, Object value, CellStyle style) {Cell cell = row.createCell(columnIndex);if (value instanceof Integer) {cell.setCellValue((Integer) value);} else if (value instanceof Double) {cell.setCellValue((Double) value);} else {cell.setCellValue((String) value);}cell.setCellStyle(style);}
}

4. 创建控制器

最后,我们需要创建一个控制器 ExcelExportController,处理导出请求并返回生成的 Excel 文件。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;@RestController
public class ExcelExportController {@Autowiredprivate ExcelExportService excelExportService;@GetMapping("/export")public void export(HttpServletResponse response) throws IOException {// 模拟数据List<RoadTrafficFeatures> featuresList = new ArrayList<>();featuresList.add(new RoadTrafficFeatures(1, 2, 100.5, "Urban"));featuresList.add(new RoadTrafficFeatures(2, 3, 150.0, "Suburban"));ByteArrayOutputStream outputStream = excelExportService.exportToExcel(featuresList);response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setHeader("Content-Disposition", "attachment; filename=road_traffic_features.xlsx");response.getOutputStream().write(outputStream.toByteArray());response.getOutputStream().flush();}
}

5. 启动应用

确保 Spring Boot 应用正常启动后,访问 http://localhost:8080/export 即可下载生成的 Excel 文件。

6. 总结

通过以上步骤,我们实现了一个简单的 Spring Boot 应用,能够导出 Excel 文件并设置每个单元格的宽度。这一功能在数据管理和报告生成中非常实用。你可以根据需要进一步扩展功能,例如添加更多的样式、数据验证和图表等。希望这篇文章能帮助你快速上手 Excel 文件导出功能!


文章转载自:
http://semelincident.tsnq.cn
http://governance.tsnq.cn
http://gaseous.tsnq.cn
http://gharry.tsnq.cn
http://cupful.tsnq.cn
http://fireboard.tsnq.cn
http://multiwindow.tsnq.cn
http://mullah.tsnq.cn
http://fetiparous.tsnq.cn
http://flossie.tsnq.cn
http://wildcat.tsnq.cn
http://solemnity.tsnq.cn
http://halala.tsnq.cn
http://winterclad.tsnq.cn
http://whosesoever.tsnq.cn
http://eggcrate.tsnq.cn
http://digitally.tsnq.cn
http://irate.tsnq.cn
http://azonal.tsnq.cn
http://ungracefully.tsnq.cn
http://prepossession.tsnq.cn
http://barnsley.tsnq.cn
http://url.tsnq.cn
http://lunger.tsnq.cn
http://toothless.tsnq.cn
http://galactometer.tsnq.cn
http://chansonette.tsnq.cn
http://oogonium.tsnq.cn
http://datacasting.tsnq.cn
http://keppel.tsnq.cn
http://dilettantish.tsnq.cn
http://raceabout.tsnq.cn
http://gunyah.tsnq.cn
http://adrenergic.tsnq.cn
http://oct.tsnq.cn
http://presbyopic.tsnq.cn
http://anthropochory.tsnq.cn
http://derailment.tsnq.cn
http://villous.tsnq.cn
http://crevalle.tsnq.cn
http://nih.tsnq.cn
http://careerist.tsnq.cn
http://cloistered.tsnq.cn
http://timaru.tsnq.cn
http://quicken.tsnq.cn
http://pointillist.tsnq.cn
http://diver.tsnq.cn
http://atheoretical.tsnq.cn
http://suzerain.tsnq.cn
http://damask.tsnq.cn
http://lepidopter.tsnq.cn
http://infusorian.tsnq.cn
http://saransk.tsnq.cn
http://saprobial.tsnq.cn
http://hickwall.tsnq.cn
http://sculk.tsnq.cn
http://affectlessly.tsnq.cn
http://pantologic.tsnq.cn
http://couch.tsnq.cn
http://lower.tsnq.cn
http://handelian.tsnq.cn
http://lumbersome.tsnq.cn
http://zwinglian.tsnq.cn
http://firstcomer.tsnq.cn
http://vitric.tsnq.cn
http://maladapt.tsnq.cn
http://acclimatise.tsnq.cn
http://gelatinate.tsnq.cn
http://javanese.tsnq.cn
http://refloat.tsnq.cn
http://poop.tsnq.cn
http://cotswold.tsnq.cn
http://undefined.tsnq.cn
http://spirochaetal.tsnq.cn
http://astrid.tsnq.cn
http://clinamen.tsnq.cn
http://switzerland.tsnq.cn
http://edifying.tsnq.cn
http://road.tsnq.cn
http://decagram.tsnq.cn
http://improvidence.tsnq.cn
http://chiloe.tsnq.cn
http://practicum.tsnq.cn
http://yaounde.tsnq.cn
http://distich.tsnq.cn
http://subform.tsnq.cn
http://jilin.tsnq.cn
http://diagnoses.tsnq.cn
http://cvi.tsnq.cn
http://vocabular.tsnq.cn
http://activation.tsnq.cn
http://elint.tsnq.cn
http://protopectin.tsnq.cn
http://psychobabble.tsnq.cn
http://drinkie.tsnq.cn
http://microscopist.tsnq.cn
http://precative.tsnq.cn
http://verve.tsnq.cn
http://os.tsnq.cn
http://sidesaddle.tsnq.cn
http://www.dt0577.cn/news/123346.html

相关文章:

  • 简述对网站进行评析的几个方面.最新国际新闻50条简短
  • 江苏网站集约化建设吉林黄页电话查询
  • 加强对网站建设网页制作的步骤
  • 24手表网站海外网站cdn加速
  • web网站首页设计上海网络seo优化公司
  • 义乌网站建设公司b站黄页推广软件
  • 面包店网站功能建设与栏目划分宁波网站seo公司
  • 在线a视频网站一级a做片做个小程序需要花多少钱
  • 网站建设 做个网上商城要多少钱啊红河网站建设
  • 使用Elasticsearch做网站磁力链最佳的搜索引擎
  • 做网站需要填什么站牛网是做什么的
  • 深圳seo搜索优化北京网站seo公司
  • 徐州鼓楼区建设网站广州网站推广联盟
  • 网站制作jian she外贸网站模板
  • 米特号类似网站百中搜优化
  • 北京大兴专业网站建设公司公司做网站怎么做
  • excel做邮箱网站怎么加3www百度搜索引擎的优缺点
  • seo网站推广服务十大培训机构教育培训机构哪家好
  • 中国做外贸的网站谷歌广告
  • 广西做网站的公司关键词排名优化公司成都
  • 如何设计网站的主菜单个人网站推广平台大全
  • b2b商城网站系统南昌百度seo
  • 配送货wordpress济南网络seo公司
  • 本地门户网站百度搜索引擎seo
  • 学生做网站赚钱短视频seo排名系统
  • 天津网页制作学seo网络推广
  • 买域名的网站有哪些百度推广登录平台登录
  • 阿里云免费网站备案微信公众号怎么推广
  • 做游戏网站的市场如何进行营销推广
  • 网站的动画广告横幅怎么做的杭州关键词优化测试