From 043c1b71a1172c4dda28208f9e1954805668d010 Mon Sep 17 00:00:00 2001 From: jsh <1209700525@qq.com> Date: Sat, 11 Oct 2025 21:51:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BE=93=E5=87=BAExcel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 16 ++- src/main/java/com/dota/Main.java | 132 ++++++++++++++++++---- src/main/java/com/dota/domain/Doctor.java | 7 ++ 3 files changed, 134 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 9ffaa54..3bb60fb 100644 --- a/pom.xml +++ b/pom.xml @@ -9,9 +9,21 @@ 1.0-SNAPSHOT - 21 - 21 + 17 + 17 UTF-8 + + + org.apache.poi + poi + 5.2.3 + + org.apache.poi + poi-ooxml + 5.2.3 + + + \ No newline at end of file diff --git a/src/main/java/com/dota/Main.java b/src/main/java/com/dota/Main.java index bfa1da2..83fbfe9 100644 --- a/src/main/java/com/dota/Main.java +++ b/src/main/java/com/dota/Main.java @@ -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(); +// 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(); + 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 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 schedule(Doctor[] doctors, Doctor[] outputOrder) { + var res = new ArrayList(); 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 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(" "); } } \ No newline at end of file diff --git a/src/main/java/com/dota/domain/Doctor.java b/src/main/java/com/dota/domain/Doctor.java index 3dca8b4..c277df1 100644 --- a/src/main/java/com/dota/domain/Doctor.java +++ b/src/main/java/com/dota/domain/Doctor.java @@ -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++) {