输出Excel

This commit is contained in:
jsh
2025-10-11 21:51:46 +08:00
parent cc687b9536
commit 043c1b71a1
3 changed files with 134 additions and 21 deletions

16
pom.xml
View File

@@ -9,9 +9,21 @@
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<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>
</dependencies>
</project>

View File

@@ -3,10 +3,15 @@ package com.dota;
import com.dota.domain.Doctor;
import com.dota.domain.Week;
import com.dota.domain.WorkEnum;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.util.*;
public class Main {
static String filePath = "C:\\Users\\yangz\\Documents\\医生排班.xlsx";
static Doctor gl;
static Doctor zl;
static Doctor ttt;
@@ -20,6 +25,9 @@ public class Main {
// 医生下标
static int idx = 0;
//开始日期
static LocalDate day;
public static void main(String[] args) {
init();
var doctorOrder = new Doctor[]{
@@ -29,8 +37,89 @@ public class Main {
ln, zy, ttt, zl, zh, gl, tgy
};
for (int i = 0; i < 2; i++) {
schedule(doctorOrder, outputOrder);
// var s = new ArrayList<Doctor>();
// for (int i = -1; i < 2; i++) {
// s.addAll(schedule(doctorOrder, outputOrder));
// }
// print(s);
write("2025-10-13", 4, "顾磊", doctorOrder, outputOrder);
}
/**
* @param startDay 开始第一天
* @param weekNum 输出周数
* @param name 第一周第一天值班的医生名字
*/
static void write(String startDay, int weekNum, String name, Doctor[] doctors, Doctor[] outputDoctors) {
day = LocalDate.parse(startDay);
for (int i = 0; i < doctors.length; i++) {
if (doctors[i].name.equals(name)) {
idx = i;
break;
}
}
var list = new ArrayList<Doctor>();
for (int i = 0; i < weekNum; i++) {
list.addAll(schedule(doctors, outputDoctors));
}
try(var workbook = new XSSFWorkbook()) {
var sheet = workbook.createSheet();
int rowNum = 0;
for (int i = 0; i < weekNum; i++) {
writeHead(sheet, rowNum, day);
day = day.plusDays(7);
rowNum += 2;
writeSchedult(sheet, rowNum, list.subList(i * 7, (i+1) * 7));
rowNum += 8;
}
for (int i = 0; i < 8; i++) {
sheet.autoSizeColumn(i);
}
try(var output = new FileOutputStream(filePath)) {
workbook.write(output);
}
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 写入表头,周几和日期
*/
static void writeHead(Sheet sheet, int rowNum, LocalDate day){
int i = 1;
var row = sheet.createRow(rowNum++);
var r2 = sheet.createRow(rowNum);
for (Week w:Week.values()) {
var cell = row.createCell(i);
cell.setCellValue(w.getValue());
cell = r2.createCell(i++);
cell.setCellValue(day.toString());
day = day.plusDays(1);
}
}
/**
* 写入医生排班
*/
static void writeSchedult(Sheet sheet, int rowNum, List<Doctor> doctors){
for (Doctor doctor : doctors) {
var row = sheet.createRow(rowNum++);
int colNum = 0;
var cell = row.createCell(colNum++);
cell.setCellValue(doctor.name);
for (WorkEnum work : doctor.works) {
cell = row.createCell(colNum++);
cell.setCellValue(work.getName());
}
}
}
@@ -60,8 +149,9 @@ public class Main {
ttt.swap = zl;
}
static void schedule(Doctor[] doctors, Doctor[] outputOrder) {
for (int k = 0; k < 2; k++) {
//生成一周的排班
static List<Doctor> schedule(Doctor[] doctors, Doctor[] outputOrder) {
var res = new ArrayList<Doctor>();
for (int i = 0; i < 7; i++) {
var d = doctors[idx];
if (d.firstRest) {
@@ -93,14 +183,13 @@ public class Main {
days = (days + 1) % 7;
idx = (idx + 1) % 6;
}
fix(doctors);
rest(doctors);
print(outputOrder);
for (Doctor d : doctors) {
for (Doctor d : outputOrder) {
res.add(d.clone());
d.reset();
}
}
return res;
}
static class Rest {
@@ -284,20 +373,25 @@ public class Main {
return false;
}
static void print(Doctor[] outputOrder) {
System.out.printf("%10s", " ");
for (Week value : Week.values()) {
System.out.printf("%10s", value.getValue());
}
static void print(List<Doctor> outputOrder) {
var size = outputOrder.size() / 7;
int idx = 0;
for (int i = 0; i < size; i++) {
System.out.printf("%10s", " ");
for (Week value : Week.values()) {
System.out.printf("%10s", value.getValue());
}
System.out.println(" ");
for (Doctor doctor : outputOrder) {
System.out.printf("%10s", doctor.name);
for (WorkEnum work : doctor.works) {
System.out.printf("%10s", work.getName());
System.out.println(" ");
for (int j = 0;j<7;j++) {
var doctor = outputOrder.get(idx++);
System.out.printf("%10s", doctor.name);
for (WorkEnum work : doctor.works) {
System.out.printf("%10s", work.getName());
}
System.out.println(" ");
}
System.out.println(" ");
}
System.out.println(" ");
}
}

View File

@@ -31,6 +31,13 @@ public class Doctor {
this.name = name;
}
@Override
public Doctor clone() {
var d = new Doctor(this.name);
System.arraycopy(this.works, 0, d.works, 0, this.works.length);
return d;
}
public void reset() {
this.restCount = 0;
for (int i = 0; i < 5; i++) {