Я новичок в JPA и Spring MVC. Я делаю проект, и я хотел бы извлечь данные из моей таблицы в объекте Java, который представляет файл xls. Поэтому я создал два метода:
public void createExcel(List<Drug> elenco, ByteArrayOutputStream byteStream) throws IOException, SQLException {
//I want to create and save a file to put on my server
FileInputStream fileDaSalvare = new FileInputStream("PATH_DB/File Drugs.xls");
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Dictionary");
Row headerRow = sheet.createRow(0);
String[] columns = {"Code", "Code 2", "Principle", "Sat-day", "Mixture", "Unity"};
for(int i = 0; i < columns.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columns[i]);
}
int rowNum = 1;
for (Drug drug: elenco) {
try {
if (drug.getMixture().getDsDescrizione().equalsIgnoreCase("attivo")) {
Row row = sheet.createRow(rowNum++);
row.createCell(0)
.setCellValue(drug.getCode());
row.createCell(1)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
row.createCell(2)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
row.createCell(3)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
row.createCell(4)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
row.createCell(5)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
}
}catch(Exception e) {
throw new IOException(e.getMessage(), e);
}
}
workbook.write(byteStream);
byte[] bytes = byteStream.toByteArray();
return bytes;
}
}
Это метод для преобразования данных моей таблицы в файл xls (через класс рабочей книги). Затем я создаю метод для сопоставления книги в определенной таблице BLOB-объектов:
public void mappingObject () throws SQLException, IOException {
List<Drug> list = farDrugService.getAll();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
FarTBlob one = new FarTBlob();
one.setXls(createExcel(list, bos));
fbrepository.save(one);
}
Затем я создаю метод для экспорта данных из BLOB-объектов в клиент (но я не делаю, если этот методЭто правда без HttpResponse или HttpRequest). Это верно? :
@GetMapping(path = "/export")
public void export(@RequestBody(required = true) String id) {
FarTBlob b=new FarTBlob();
b.setId(Long.valueOf(id));
FarTBlob blob = fbr.findOne(Long.valueOf(id));
byte[] content = blob.getXls();
log.info(content);
}
Затем я создаю метод для загрузки моего файла xls, хранящегося на моем сервере, если есть новая строка:
public void updateWorkbook(List<Drug> list) throws EprocException {
for (Drug drug: list) {
try {
FileInputStream myxls = new FileInputStream("PATH_DB/Drug.xls");
HSSFWorkbook sheet = new HSSFWorkbook(myxls);
HSSFSheet worksheet = sheet.getSheetAt(0);
int lastRow=worksheet.getLastRowNum();
Row row = worksheet.createRow(++lastRow);
if (drug.getMixture().getDsDescrizione().equalsIgnoreCase("attivo")) {
row.createCell(0)
.setCellValue(drug.getCode());
row.createCell(1)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
row.createCell(2)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
row.createCell(3)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
row.createCell(4)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
row.createCell(5)
.setCellValue(drug.getCode2().getTxDescrizioneCaratteristica());
}
myxls.close();
FileOutputStream fileDefinitivo =new FileOutputStream(new File("PATH_DB/Drugs.xls"));
sheet.write(fileDefinitivo);
fileDefinitivo.close();
} catch(Exception e){
throw new EprocException(e.getMessage(),e);
}
}
Это моя сущность для моей таблицыBlob:
@Entity
@Table(name="BLOB")
public class FarTBlob {
@Id
@NotNull
@SequenceGenerator(name="FARTBLOB_ID_GENERATOR", sequenceName="SEQ_BLOB")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="FARTBLOB_ID_GENERATOR")
@Column(name="ID", unique=true, nullable=false, precision=14)
private long id;
@NotNull
@Lob
@Column(name="XLS")
private byte[] xls;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public byte[] getXls() {
return xls;
}
public void setXls(byte[] xls) {
this.xls = xls;
}
}
Но я не знаю, как продолжить, потому что я не знаю, как обновить мою рабочую книгу, если клиент внес изменения в мой стол. Препарат, и я не знаю, если мойМетод export () правильный (вы увидите все данные xls из моего блоба?).