Je veux sortir le contenu du programme au format CSV
CSV affiche le contenu de la table de planification affichée sur l'écran supérieur en appuyant sur le bouton résultat DL
Ajout de super-csv à build.gradle
build.gradle
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'javax.validation:validation-api:2.0.1.Final'
// https://mvnrepository.com/artifact/javax.validation/validation-api
implementation 'javax.validation:validation-api:2.0.1.Final'
// https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator
runtimeOnly 'org.hibernate.validator:hibernate-validator:6.0.17.Final'
// https://mvnrepository.com/artifact/org.glassfish/javax.el
runtimeOnly 'org.glassfish:javax.el:3.0.1-b11'
// https://mvnrepository.com/artifact/org.webjars/fullcalendar
compile group: 'org.webjars.bower', name: 'fullcalendar', version: '3.5.1'
// https://mvnrepository.com/artifact/org.webjars.bower/moment
compile group: 'org.webjars.bower', name: 'moment', version: '2.19.1'
// https://mvnrepository.com/artifact/org.webjars/jquery
compile group: 'org.webjars', name: 'jquery', version: '2.0.3'
// https://mvnrepository.com/artifact/com.github.mygreen/super-csv-annotation
compile group: 'com.github.mygreen', name: 'super-csv-annotation', version: '2.2'
}
test {
useJUnitPlatform()
}
3.Controller Définir les informations d'en-tête dans HttpServletResponse et appeler la méthode de traitement de sortie CSV dans Controller Puisqu'une exception liée à l'écriture d'un fichier peut être déclenchée dans le processus de service, déclarez IOException comme un throw.
TopController.java
...Omission
//Accepter la demande de sortie CSV
@RequestMapping(value = "/top/csv", method = RequestMethod.GET)
public String csvDownload(HttpServletResponse response) throws IOException {
String header = String.format("attachment; filename=\"%s\";", UriUtils.encode("result.csv", StandardCharsets.UTF_8.name()));
response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, header);
topService.csvDownload(response);
return "/top";
}
4.Service Créer une instance de la classe org.supercsv.io.CsvMapWriter avec OutputStreamWriter et un constructeur formatable Excel Écrire avec la méthode writeHeader / writeComment lors du formatage du bean qui a obtenu les informations de planification de la base de données
TopService.java
package com.example.alhproject.service;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.prefs.CsvPreference;
import com.example.alhproject.entity.Schedule;
import com.example.alhproject.mapper.ScheduleMapper;
@Service
public class TopService {
@Autowired
private ScheduleMapper scheduleMapper;
private static final String OUTPUT_SCHEDULE_FORMAT = "%s,%s,%s,%s,%s,%s";
private static final String SJIS = "SJIS";
private static final String TITLE = "title";
private static final String CONTEXT = "context";
private static final String USER_ID= "user_id";
private static final String CREATED_DATE = "created_date";
private static final String SCHEDULE_START_TIME = "schedule_start_time";
private static final String SCHEDULE_END_TIME = "schedule_end_time";
//planification de l'acquisition du contenu de la table
public List<Schedule> getAllSchedule() {
return scheduleMapper.selectAll();
}
//Traitement de sortie CSV
public void csvDownload(HttpServletResponse response) throws IOException {
try (OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream(), Charset.forName(SJIS));
CsvMapWriter wr = new CsvMapWriter(osw, CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE)) {
wr.writeHeader(String.format(OUTPUT_SCHEDULE_FORMAT,
TITLE,
CONTEXT,
USER_ID,
CREATED_DATE,
SCHEDULE_START_TIME,
SCHEDULE_END_TIME
));
getAllSchedule().forEach(dbsc -> {
String scheduleResult = String.format(OUTPUT_SCHEDULE_FORMAT,
dbsc.getTitle(),
dbsc.getContext(),
dbsc.getUserId().toString(),
dbsc.getCreatedDate().toString(),
dbsc.getScheduleStartTime().toString(),
dbsc.getScheduleEndTime().toString());
try {
wr.writeComment(scheduleResult);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
}
(Référence) LazyCsvAnnotationBeanWriter semble être facile à utiliser s'il correspond exactement au nom de l'enregistrement https://mygreen.github.io/super-csv-annotation/sphinx/labelledcolumn.html
Sample.java
//Lors de l'écriture de tous les enregistrements à la fois
public void sampleWriteAll() {
...
LazyCsvAnnotationBeanWriter<UserCsv> csvWriter = new LazyCsvAnnotationBeanWriter<>(
SampleCsv.class,
Files.newBufferedWriter(new File("sample.csv").toPath(), Charset.forName("Windows-31j")),
CsvPreference.STANDARD_PREFERENCE);
...
Recommended Posts