Entity
@Entity
public class Card {
@Id
private Long id;
private String full_name;
private String login;
private int balance;
public Card() {
}
public Card(String full_name, String login, int balance) {
this.full_name = full_name;
this.login = login;
this.balance = balance;
}
public Card(Long id, String full_name, String login, int balance) {
this.id = id;
this.full_name = full_name;
this.login = login;
this.balance = balance;
}
public String getFull_name() {
return full_name;
}
public String getLogin() {
return login;
}
public int getBalance() {
return balance;
}
@Override
public String toString() {
......... }
}
Repository
@Repository
public interface CardRepository extends JpaRepository<Card, Long>{
@Query(value = "SELECT us.id, ...", nativeQuery = true)
List<Card> findAll();
}
Service
public interface CardService {
List<Card> findAll();
}
Impl
@Service
public class CardServiceImpl implements CardService {
@Autowired
private CardRepository repository;
@Override
public List<Card> findAll() {
List<ECard> list = new ArrayList<>();
return repository.findAll();
}
}
Controller
@RestController
@RequestMapping("/card")
public class CardController {
@Autowired
private CardService cardService;
public @ResponseBody List<Card> getAllCards() {
return cardService.findAll();
}
@RequestMapping( value = "/card", method = RequestMethod.GET)
public List<Card> cardList() {
return cardService.findAll();
}
}
I built a service like this. It works great. But I would like to save the data it receives to an xlsx file. This file should be saved when the application starts. I wanted to add the Workbook library instead of the existing Controller class, or rather its contents.
Something like that:
public class SaveCards {
public static void saveCards() throws IOException {
CardServiceImpl cardService = new CardServiceImpl();
saveExcel(cardService.findAll(), "fileName.xlsx");
}
private static void saveExcel(List<Card> list, String fileName) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Cards");
sheet.setColumnWidth(0, 6000);
sheet.setColumnWidth(1, 4000);
Row header = sheet.createRow(0);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
XSSFFont font = ((XSSFWorkbook) workbook).createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 16);
font.setBold(true);
headerStyle.setFont(font);
Cell headerCell = header.createCell(0);
headerCell.setCellValue("full_name");
headerCell.setCellStyle(headerStyle);
headerCell = header.createCell(1);
headerCell.setCellValue("login");
headerCell.setCellStyle(headerStyle);
headerCell = header.createCell(2);
headerCell.setCellValue("balance");
headerCell.setCellStyle(headerStyle);
CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
int ix_row=2;
for (Card card : list) {
Row row = sheet.createRow(ix_row);
Cell cell = row.createCell(0);
cell.setCellValue(eCard.getFull_name());
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(eCard.getLogin());
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue(eCard.getBalance());
cell.setCellStyle(style);
ix_row ;
}
FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
workbook.close();
}
}
However, if I changed the Controller class to the SaveCards class, I got an error: Error creating bean with name 'viewResolver' Spring
Now, the following comes out: Started DemonewApplication in 11.253 seconds (JVM running for 11.882). And that's all, no action takes place, the data received by the select is not saved to the xlsx file. What am I doing wrong?
I understand that the saveCards method in the SaveCards class has to be called somewhere, but I can't figure out where?
CodePudding user response:
You can extend org.springframework.boot.ApplicationRunner
to run your code at Spring Boot startup.
@Component
class SaveCardsStartupRunner implements ApplicationRunner
{
@Autowired
private SaveCards saveCards;
@Override
public void run(ApplicationArguments args)
{
saveCards.saveCards();
}
}
And the SaveCards
class has to be changed as follows:
@Component
public class SaveCards {
{
@Autowired
private CardService cardService;
public void saveCards() throws IOException {
saveExcel(cardService.findAll(), "fileName.xlsx");
}
private void saveExcel(List<Card> list, String fileName) throws IOException {...}
}