Adding cell with quotePrefix in POI

后端 未结 1 1250
眼角桃花
眼角桃花 2021-01-15 13:00

I want to add a cell in xlsx workbooks sheet containing the quote prefix, and i am trying to create that sheet using POI library. How do I add this type of cell

I fo

相关标签:
1条回答
  • 2021-01-15 13:49

    The CTXf and also the quotePrefix property is part of the XSSFCellStyle and not the XSSFCell.

    So we must create a XSSFCellStyle, set the quotePrefix there and then apply this XSSFCellStyle to the XSSFCell.

    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.*;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    class WriteQuotePrefix {
    
     public static void main(String[] args) {
      try {
    
       Workbook wb = new XSSFWorkbook();
    
       CellStyle style = wb.createCellStyle();
       ((XSSFCellStyle)style).getCoreXf().setQuotePrefix(true);
    
       Sheet sheet = wb.createSheet();
       Row row = sheet.createRow(0);
       Cell cell = row.createCell(0);
       cell.setCellStyle(style);
       cell.setCellValue("1234");
    
       FileOutputStream fileOut = new FileOutputStream("WriteQuotePrefix.xlsx");
       wb.write(fileOut);
       fileOut.close();
    
      } catch (IOException ioex) {
      }
     }
    }
    
    0 讨论(0)
提交回复
热议问题