今天刚好在做对于一个存在的excel文件的编辑,使用到了POI中的XSSFWorkBook
class="java" name="code">
try (
InputStream inputStream = new FileInputStream(bioCloudProperties.getSampleInformationConfig().getSampleImportExcelPath());
FileOutputStream out = new FileOutputStream(filePath);
) {
Files.copy(inputStream, Paths.get(filePath));
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(filePath));
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
xssfSheet.getRow(0).createCell(1).setCellValue(simpleInformation.getCourierNumber());
xssfSheet.getRow(1).createCell(1).setCellValue(simpleInformation.getContractName());
xssfSheet.getRow(2).createCell(1).setCellValue(simpleInformation.getContractNum());
ContractInformation contractInformation = (ContractInformation) JSONObject.toBean(JSONObject.fromObject(simpleInformation.getContractInformation()), ContractInformation.class);
xssfSheet.getRow(3).createCell(1).setCellValue(contractInformation.getProductType().get(0));
xssfSheet.getRow(4).createCell(1).setCellValue(contractInformation.getClientName());
AttachmentFile attachmentFile = getAttrachFileJson(simpleInformation);
if (TissueSample.name().equalsIgnoreCase(simpleInformation.getSampleType())) {
SampleInformationOverView sampleInformationOverView = (SampleInformationOverView) JSONObject.toBean(JSONObject.fromObject(simpleInformation.getSampleInformationOverview()), SampleInformationOverView.class);
//运输条件
xssfSheet.getRow(5).createCell(1).setCellValue(sampleInformationOverView.getTransportation());
xssfSheet.getRow(6).createCell(1).setCellValue(sampleInformationOverView.getAdviceSave());
xssfSheet.getRow(7).createCell(1).setCellValue(sampleInformationOverView.getExtractBeforeDo());
xssfSheet.getRow(8).createCell(1).setCellValue("否");
JSONArray array = JSONArray.fromObject(simpleInformation.getDetailedSampleInformation());
//设置内容
int lineSize = array.size();
for (int i = 11; i < lineSize+11; i++) {
DetailSampleInformation detailSampleInformation = (DetailSampleInformation) JSONObject.toBean(JSONObject.fromObject(array.get(i-11)), DetailSampleInformation.class);
if (detailSampleInformation.getIsMinSample().equalsIgnoreCase("是")) {
xssfSheet.getRow(8).createCell(1).setCellValue("是");
}
XSSFRow contentRow = xssfSheet.createRow(i);
SampleStaticContent.setTissueSampleImportTableValue(sampleInformationOverView, detailSampleInformation, TissueSample, contentRow);
}
xssfWorkbook.write(out);
LOG.info("write sample import file end");
}
} catch (Exception e) {
LOG.info("信息单号:{}将样品内容转换为文件出现异常:{}", simpleInformation.getId(), e.getMessage());
}
重点代码段:
InputStream inputStream = new FileInputStream(bioCloudProperties.getSampleInformationConfig().getSampleImportExcelPath());
FileOutputStream out = new FileOutputStream(filePath);
Files.copy(inputStream, Paths.get(filePath));
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(filePath));
xssfWorkbook.write(out);