Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ”€ :: (#275) Excel ์ถ”๊ฐ€ #276

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions yapaghetti-infrastructure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ dependencies {

//cool sms
implementation 'net.nurigo:javaSDK:2.2'

// excel poi
implementation 'org.apache.poi:poi:3.16'
implementation 'org.apache.poi:poi-ooxml:3.16'
osangu marked this conversation as resolved.
Show resolved Hide resolved

}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.hs.entrydsm.yapaghetti.domain.teacher.excel;

import javax.servlet.http.HttpServletResponse;

public interface CreateExcelInterface {
void execute(HttpServletResponse response);
}
osangu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package kr.hs.entrydsm.yapaghetti.domain.teacher.excel;

import kr.hs.entrydsm.yapaghetti.domain.student.persistence.StudentPersistenceAdapter;
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.StudentElementByGradeClassNum;
import kr.hs.entrydsm.yapaghetti.domain.teacher.excel.exception.ExcelOutPutException;
import lombok.AccessLevel;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RequiredArgsConstructor
@Component
public class StudentXSSFExcelCreator implements CreateExcelInterface {

private final StudentPersistenceAdapter studentPersistenceAdapter;

public static final String DATE_TIME_FORMAT_PATTERN = "yyyy๋…„MM์›”dd์ผ_HH์‹œmm๋ถ„_";
public static final String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
public static final String HEADER_NAME = "\"Content-Disposition\"";
public static final String FILE_EXTENSION = ".xlsx";
public static final String CHARSET_NAME = "8859_1";
public static final String BITE_CHARSET_NAME = "KSC5601";
public static final String HEADER_VALUE_BEFORE_NAME = "attachment; fileName=\"";
public static final String HEADER_VALUE_AFTER_NAME = "\"";

@Override
public void execute(HttpServletResponse response) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("ํ•™์ƒ ์ •๋ณด");

int rowNum = 0;
String fileName = "์ „๊ต์ƒ ํ˜„ํ™ฉ";

Row headerRow = sheet.createRow(rowNum++);
headerRow.createCell(0).setCellValue("ํ•™๋ฒˆ");
headerRow.createCell(1).setCellValue("์ด๋ฆ„");
headerRow.createCell(2).setCellValue("ํ”ผ๋“œ๋ฐฑ ์ƒํƒœ");
headerRow.createCell(3).setCellValue("๋Œ€๊ธฐ ๋ฌธ์„œ ์—ฌ๋ถ€");
headerRow.createCell(4).setCellValue("๊ณต๊ฐœ ๋ฌธ์„œ ์—ฌ๋ถ€");

List<StudentElementByGradeClassNum> excelElement = studentPersistenceAdapter
.queryStudentListByGradeAndClassNumAndDocStatus(null, null, null);

for (StudentElementByGradeClassNum element : excelElement) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(element.getGcn());
row.createCell(1).setCellValue(element.getName());
row.createCell(2).setCellValue(element.isFeedbackStatus() ? "O" : null);
row.createCell(3).setCellValue(element.isSubmitted() ? "O" : null);
row.createCell(4).setCellValue(element.isPublicStatus() ? "O" : null);
}

try {
String time = LocalDateTime.now()
.format(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT_PATTERN));
String formatFileName = new String((time + fileName + FILE_EXTENSION)
.getBytes(BITE_CHARSET_NAME), CHARSET_NAME);

response.setContentType(CONTENT_TYPE);
response.setHeader(HEADER_NAME, HEADER_VALUE_BEFORE_NAME + formatFileName + HEADER_VALUE_AFTER_NAME);

workbook.write(response.getOutputStream());
workbook.close();
} catch (IOException e) {
throw ExcelOutPutException.EXCEPTION;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kr.hs.entrydsm.yapaghetti.domain.teacher.excel.exception;

import kr.hs.entrydsm.yapaghetti.error.ErrorProperty;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum ExcelErrorCode implements ErrorProperty {

EXCEL_OUTPUT_EXCEPTION(500, "์—‘์…€์„ ๋‹ค์šด๋กœ๋“œ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");

private final int status;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.hs.entrydsm.yapaghetti.domain.teacher.excel.exception;

import kr.hs.entrydsm.yapaghetti.error.YapaghettiException;

public class ExcelOutPutException extends YapaghettiException {

public static final YapaghettiException EXCEPTION = new ExcelOutPutException();

private ExcelOutPutException() {
super(ExcelErrorCode.EXCEL_OUTPUT_EXCEPTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.CompanyListResponse;
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.NewCompanyResponse;
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.StudentDetailResponse;
import kr.hs.entrydsm.yapaghetti.domain.teacher.excel.CreateExcelInterface;
import kr.hs.entrydsm.yapaghetti.domain.teacher.presentation.dto.request.WebCreateCompanyRequest;
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.StudentListResponse;
import kr.hs.entrydsm.yapaghetti.domain.teacher.presentation.dto.request.WebCreateFeedbackRequest;
Expand All @@ -38,6 +39,8 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.UUID;
Expand All @@ -58,6 +61,7 @@ public class TeacherWebAdapter {
private final QueryStudentDetailPort queryStudentDetailPort;
private final QueryCompanyListPort queryCompanyListPort;
private final DeleteFeedbackPort deleteFeedbackPort;
private final CreateExcelInterface createExcelInterface;

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/feedback/{student-id}")
Expand Down Expand Up @@ -162,4 +166,8 @@ public void deleteFeedback(@RequestBody @Valid WebDeleteFeedbackRequest request)
);
}

@GetMapping("/excel")
public void getExcel(HttpServletResponse response) {
createExcelInterface.execute(response);
}
Comment on lines +169 to +172
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5; ๋” ์ข‹์€ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”...

Copy link
Member

@LeagueLugas LeagueLugas Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OutputStream์„ ๊ทธ๋Œ€๋กœ return ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋ ค๋‚˜?
์˜ˆ์ „์— ๋น„์Šทํ•˜๊ฒŒ ํ–ˆ๋˜๊ฑฐ ๊ฐ™์€๋””, ํ•จ ์‚ฝ์งˆ ํ•ด๋ด๋ฐ”
https://github.com/EntryDSM/Husky/blob/master/src/main/java/kr/hs/entrydsm/husky/domain/pdf/controller/PDFExportController.java

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.antMatchers(HttpMethod.DELETE, "/teachers/company/{company-id}").hasRole(TEACHER)
.antMatchers(HttpMethod.DELETE, "/teachers/student/{student-id}").hasRole(TEACHER)
.antMatchers(HttpMethod.DELETE, "/teachers/feedback").hasRole(TEACHER)
.antMatchers(HttpMethod.GET, "/teachers/excel").hasRole(TEACHER)

// tags
.antMatchers(HttpMethod.POST, "/tags").hasRole(TEACHER)
Expand Down