代码说明用eclipse获取excel表格中停车位,以平面图形式展现在页面,同时接收数据填充停车位颜色
时间: 2024-05-07 20:23:01 浏览: 174
停车场 车位模拟 平面图
4星 · 用户满意度95%
以下是代码说明:
1. 首先,需要在 Eclipse 中创建一个 Web 项目,并引入 Apache POI 和 JFreeChart 库。Apache POI 库用于读取 Excel 表格,JFreeChart 库用于绘制平面图形。
2. 在项目中创建一个名为 "upload" 的文件夹,用于存放上传的 Excel 表格。
3. 在项目中创建一个名为 "images" 的文件夹,用于存放生成的停车位平面图。
4. 创建一个名为 "UploadServlet" 的 Servlet,用于处理上传的 Excel 表格。在 doPost() 方法中,使用 Apache POI 库读取 Excel 表格中的数据,并将数据存储在一个 List 中。然后调用 generateParkingLot() 方法生成停车位平面图,并将平面图保存到 "images" 文件夹中。
5. 创建一个名为 "ParkingLotServlet" 的 Servlet,用于展示停车位平面图。在 doGet() 方法中,使用 JFreeChart 库绘制停车位平面图,并将其展示在页面上。同时,根据之前存储的数据,填充停车位的颜色。
6. 在页面上添加一个表单,用于上传 Excel 表格。
7. 在页面上添加一个 Canvas 元素,用于展示停车位平面图。
8. 使用 JavaScript 实现上传表格和展示平面图的交互。在表单提交时,将表格上传到 "upload" 文件夹中,并通过 Ajax 调用 ParkingLotServlet 获取停车位平面图。在获取到平面图后,将其绘制到 Canvas 元素中。
代码示例:(仅供参考)
UploadServlet.java
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileName = request.getParameter("file");
String filePath = getServletContext().getRealPath("") + File.separator + "upload" + File.separator + fileName;
File file = new File(filePath);
FileInputStream inputStream = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);
List<String[]> data = new ArrayList<>();
for (Row row : sheet) {
String[] rowData = new String[row.getLastCellNum()];
for (int i = 0; i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
if (cell != null) {
switch (cell.getCellType()) {
case STRING:
rowData[i] = cell.getStringCellValue();
break;
case NUMERIC:
rowData[i] = String.valueOf(cell.getNumericCellValue());
break;
case BOOLEAN:
rowData[i] = String.valueOf(cell.getBooleanCellValue());
break;
default:
rowData[i] = "";
}
} else {
rowData[i] = "";
}
}
data.add(rowData);
}
inputStream.close();
ParkingLotGenerator.generateParkingLot(data);
response.sendRedirect("ParkingLotServlet");
}
}
```
ParkingLotGenerator.java
```java
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
public class ParkingLotGenerator {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int BORDER = 50;
private static final int ROWS = 6;
private static final int COLUMNS = 8;
public static void generateParkingLot(List<String[]> data) throws IOException {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, WIDTH, HEIGHT);
graphics.setColor(Color.BLACK);
graphics.drawRect(BORDER, BORDER, WIDTH - BORDER * 2, HEIGHT - BORDER * 2);
for (int i = 1; i < ROWS; i++) {
int y = BORDER + (HEIGHT - BORDER * 2) / ROWS * i;
graphics.drawLine(BORDER, y, WIDTH - BORDER, y);
}
for (int i = 1; i < COLUMNS; i++) {
int x = BORDER + (WIDTH - BORDER * 2) / COLUMNS * i;
graphics.drawLine(x, BORDER, x, HEIGHT - BORDER);
}
for (int i = 0; i < data.size(); i++) {
int row = Integer.parseInt(data.get(i)[0]);
int column = Integer.parseInt(data.get(i)[1]);
String status = data.get(i)[2];
int x = BORDER + (WIDTH - BORDER * 2) / COLUMNS * column + (WIDTH - BORDER * 2) / COLUMNS / 4;
int y = BORDER + (HEIGHT - BORDER * 2) / ROWS * row + (HEIGHT - BORDER * 2) / ROWS / 4;
int w = (WIDTH - BORDER * 2) / COLUMNS / 2;
int h = (HEIGHT - BORDER * 2) / ROWS / 2;
if (status.equals("空闲")) {
graphics.setColor(Color.GREEN);
} else if (status.equals("已预约")) {
graphics.setColor(Color.YELLOW);
} else if (status.equals("已占用")) {
graphics.setColor(Color.RED);
} else {
graphics.setColor(Color.GRAY);
}
graphics.fillRect(x, y, w, h);
}
ImageIO.write(image, "png", new File(getServletContext().getRealPath("") + File.separator + "images" + File.separator + "parking_lot.png"));
}
}
```
ParkingLotServlet.java
```java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
@WebServlet("/ParkingLotServlet")
public class ParkingLotServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedImage image = ImageIO.read(new File(getServletContext().getRealPath("") + File.separator + "images" + File.separator + "parking_lot.png"));
response.setContentType("image/png");
ImageIO.write(image, "png", response.getOutputStream());
}
}
```
index.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Parking Lot</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$("#uploadForm").submit(function(event) {
event.preventDefault();
var file = $("#file").prop("files")[0];
var formData = new FormData();
formData.append("file", file);
$.ajax({
url: "upload",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var image = new Image();
image.onload = function() {
context.drawImage(image, 0, 0);
};
image.src = "ParkingLotServlet";
},
error: function() {
alert("上传失败");
}
});
});
});
</script>
</head>
<body>
<form id="uploadForm">
<input type="file" id="file" name="file" accept=".xlsx">
<button type="submit">上传</button>
</form>
<canvas id="canvas" width="800" height="600"></canvas>
</body>
</html>
```
在执行代码之前,需要确保已正确引入 Apache POI 和 JFreeChart 库。另外,需要将代码中的路径根据实际情况进行修改。
阅读全文