简体   繁体   English

使用Apache POI保留excel单元格值的单引号前缀

[英]Preserve Single Quote Prefix of Cell Value of excel using Apache POI

Currently i am reading excel value from my sheet But its neglecting " ' " in cell value目前我正在从我的工作表中读取 excel 值但它忽略了单元格值中的“'”

Expected output- 'Testing
Actual output  -  Testing

How to display the single qoute while reading from excel从excel读取时如何显示单个qoute

package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelDataConfig {

    XSSFWorkbook wb;
    XSSFSheet sheet1;

    public ExcelDataConfig(String excelPath) throws IOException {
        File src = new File(excelPath);
        FileInputStream fis = new FileInputStream(src);
        wb = new XSSFWorkbook(fis);
        
        
    }

    public String getData(int sheetNumber, int row, int column) {
        sheet1 = wb.getSheetAt(sheetNumber);
        String data = sheet1.getRow(row).getCell(column).getStringCellValue();
        return data;

    }
}

My test class我的测试班

ExcelDataConfig excel = new ExcelDataConfig(System.getProperty("user.dir") + "/src/test/resources/testData/TestData.xlsx");
System.out.println("String with double quotes=" +excel.getData(0, 0, 0));

    

A leading single quote character ' is a special character in Excel, which tells it to treat it's contents verbatim.前导单引号字符'是 Excel 中的一个特殊字符,它告诉它逐字处理其内容。 The most common use I know of is to tell it not to format numerical values by removing leading zeroes.我所知道的最常见的用法是告诉它不要通过删除前导零来格式化数值。

When you enter a value into Excel that is prefixed with ' , note how the quote only appears in the editing field, not in the cell as it is displayed in the sheet.当您在 Excel 中输入以'为前缀的值时,请注意引号如何仅出现在编辑字段中,而不是出现在工作表中显示的单元格中。

Behind the scenes the quotePrefix value is stored separately from the value itself.在幕后, quotePrefix值与值本身分开存储。 For example, setting 'Testing will result in the value being just Testing , with quotePrefix=true in the OOXML.例如,设置'Testing将导致只是Testing ,在 OOXML 中使用quotePrefix=true

So technical, so good, POI reports the value correctly, by giving you exactly what Excel shows as the value, namely just Testing .太技术了,太好了,POI 正确地报告了,通过准确地为您提供 Excel 显示的值,即Testing To make the single quotation mark appear in POI, access the property CellStyle.getQuotePrefixed() and add it manually.要使单引号出现在 POI 中,请访问属性CellStyle.getQuotePrefixed()并手动添加它。

So in your example you could try:因此,在您的示例中,您可以尝试:

Cell cell = sheet1.getRow(row).getCell(column);
String data = cell.getStringCellValue();
if(cell.getCellStyle().getQuotePrefixed()) {
    data = '''+data;
}
return data;

With this you might (depending on use-case) run into issues where sometimes you want the prefix and in other situations you don't, especially if you're not in control of the input Excel.有了这个,您可能(取决于用例)遇到问题,有时您需要前缀,而在其他情况下您不需要,尤其是当您无法控制输入 Excel 时。 Arguably it might be 'more correct' to escape the single quotation mark in Excel itself, by adding a second.可以说,通过添加第二个来转义 Excel 本身中的单引号可能“更正确”。 So in Excel you would enter ''Testing .因此,在 Excel 中,您将输入''Testing This would ensure that both Excel and POI report the desired value of 'Testing , without using the above code.这将确保 Excel 和 POI 报告所需的'Testing值,而无需使用上述代码。

Yes it will neglect single quote as single quote in xls cell is actually a shortcut to accept data as it is.是的,它会忽略单引号,因为 xls 单元格中的单引号实际上是按原样接受数据的快捷方式。 So if you are storing 'Hello Superman' then please store it as ''Hello Superman' and it will be read correctly as 'Hello Superman'因此,如果您要存储“Hello Superman”,请将其存储为“Hello Superman”,它将被正确读取为“Hello Superman”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM