是否可以使用POITransformer在像POI这样的单元格中写入?

时间:2016-01-22 07:26:13

标签: java apache-poi

这是我需要做的。

1) Accept an xlsx/xls file from client.
2) Backend will receive it in the form of multipart file
3) The file will be processed and if the format of the data is invalid, that same file will be updated and the error message will be written in the side of the input of the client.
4) this modified file will be sent back to the user.

但经过多次尝试,我无法使我的代码正常工作。

def generateErrorReport(ServletResponse response, Map messageCollections, MultipartFile file, String ext){

    FileInputStream fileIn = file.getInputStream()
    Workbook workbook = (ext.equalsIgnoreCase("xls")) ? new HSSFWorkbook(fileIn) : new XSSFWorkbook(fileIn)

    workbook = this.getWorkbook((MultipartFile) file, ext.equalsIgnoreCase("xls"));
    try {
        Sheet sheet = workbook.getSheetAt(0)
        Long lastCellNum = sheet.getRow(0).getLastCellNum();

        for(int i=1; i<sheet.getLastRowNum(); i++){
            if(messageCollections[i]!=null && messageCollections[i]!=[] ) {
                Cell cell = sheet.getRow(i).getCell(lastCellNum + 1)
                cell.setCellValue(messageCollections[i]);
            }
        }

        fileIn.close()

        FileOutputStream fileOut = new FileOutputStream((File) file)
        workbook.write(fileOut);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        response.setHeader("Content-Disposition", "Attachment;Filename=error.xlsx")
        response.outputStream << fileOut
        response.outputStream.flush()

        fileOut.close()
    }catch(Exception ex){
        println ex
    }
}

现在,我认为找到了一种方法,即使用POITransformer。问题是当你有一个模板时使用它。我的想法是使用客户端发送的文件作为模板,然后只需在客户端的输入旁边写入错误消息。但是我找不到写它的方法,因为我找不到setCellData或任何相同的方法。

问题是,是否可以使用POITransformer写入所需的单元数据?如果是的话,我该怎么做?

到目前为止,这就是我所做的。但它仍然没有写作。你能告诉我什么错了吗?

private void bla(ServletResponse response, Map messageCollections, MultipartFile file, String ext){
        InputStream is = file.getInputStream();
        OutputStream os = response.outputStream;

        String fileName = "error";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "Attachment;Filename=${fileName}");
        try {

            PoiTransformer transformer = PoiTransformer.createTransformer(is, os);
            org.apache.poi.ss.usermodel.Workbook workbook = transformer.getWorkbook()
            Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex())
            int lastColNum = sheet.getRow(0).getLastCellNum()

            Cell cellData;
            (0..sheet.getLastRowNum()) {
                if (messageCollections[it]!=null && messageCollections[it]!=[]) {
                    cellData = sheet.getRow(it).getCell(lastColNum+1);
                    cellData.setCellValue(messageCollections[it].toString())
                }
            }

            transformer.write();
        } catch (IOException ex) {
          println ex
//            Logger.getLogger(ExcelFileHandler.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeStream(is);
            closeStream(os);
        }

    }

1 个答案:

答案 0 :(得分:0)

希望这个答案对其他人有帮助。

    InputStream is = file.getInputStream();
    OutputStream os = response.outputStream;

    String fileName = "desiredFilename." + ext

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "Attachment;Filename=${fileName}");
    try {

        PoiTransformer transformer = PoiTransformer.createTransformer(is, os);
        org.apache.poi.ss.usermodel.Workbook workbook = transformer.getWorkbook()
        Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex())
        int lastColNum = sheet.getRow(0).getLastCellNum()

        Cell cell;

        cell = sheet.getRow(0).getCell(lastColNum);
        if(cell==null){
            cell = sheet.getRow(0).createCell(lastColNum);
        }
        cell.setCellType(1)
        cell.setCellValue("Message")
        cell.setCellStyle(getStyle(workbook, 2))

        for(int it=1; it<sheet.getLastRowNum(); it++) {
            if (message.get(new Long(it))!=null && message.get(new Long(it))!=[]) {
                cell = sheet.getRow(it).getCell(lastColNum);
                if(cell==null){
                    cell = sheet.getRow(it).createCell(lastColNum);
                }
                cell.setCellType(1)
                cell.setCellValue(message.get(new Long(it)).join(', '))
                cell.setCellStyle(getStyle(workbook, 1))
            }
        }

        sheet.autoSizeColumn(lastColNum);
        transformer.write();