POI编辑存在的EXCEL文件_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > POI编辑存在的EXCEL文件

POI编辑存在的EXCEL文件

 2018/7/6 12:20:53  一边天  程序员俱乐部  我要评论(0)
  • 摘要:今天刚好在做对于一个存在的excel文件的编辑,使用到了POI中的XSSFWorkBooktry(InputStreaminputStream=newFileInputStream(bioCloudProperties.getSampleInformationConfig().getSampleImportExcelPath());FileOutputStreamout=newFileOutputStream(filePath);){Files.copy(inputStream,Paths
  • 标签:excel 文件

今天刚好在做对于一个存在的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);

    
上一篇: java, policy, 权限, 安全,沙箱 下一篇: 没有下一篇了!
发表评论
用户名: 匿名