gpt4 book ai didi

java - 无法使用 apache poi 从 excel 中检测删除线数据

转载 作者:行者123 更新时间:2023-11-29 08:33:07 24 4
gpt4 key购买 nike

我正在为我的项目使用 Java 8、excel 和 apache poi。我有兴趣使用 java 从 excel 中提取某些单元格值。我正在尝试检测 Excel 单元格中删除线的文本,但文本格式略有不同,这就是我遇到一些问题的原因。

以下是我的 Excel 工作表中数据的布局方式:

enter image description here enter image description here

从 excel 中提取此数据后,我总是将其保存为字符串数组列表格式,如 a = [text 1, text 2, text 3]。如果您想查看我如何在此数组列表中存储数据,请在下面提到代码。

我想要的:

我想忽略所有那些被删除的文本,所以在上面的例子中,我希望第一张图片和第二张图片的输出像这样 [text 2, text 3]

我尝试了什么:

为了检测删除线值,我首先尝试了以下代码:

XSSFRichTextString text = new XSSFRichTextString(a.get(0));                             
XSSFFont font = text.getFontAtIndex(0);
Boolean font_striked = font.getStrikeout();

但上面的代码无法正常工作,因为 font_striked 返回 null,它必须返回 true 或 false

在我的案例中,部分适用于单行单元格值的代码是:

boolean striked_out = sheet.getRow(row_index).getCell(column_index).getCellStyle().
getFont().getStrikeout();

此代码仅在单元格中有单行值且带有如上所示的项目符号列表时才有效。它失败了,因为它不是为这种文本制作的。

附言我相信,如果我能够以某种方式从 arraylist 的项目符号点中检测到一个删除线字符串,我就可以让它适用于所有数据。

根据下面的答案,我更新了我的问题,添加了以下代码来展示我如何制作我的字符串数组列表

我如何将excel中的数据转换成Arraylist:

String value_header = cell.getStringCellValue();
String[] newline_split = value_header.split("-");

for (int i = 0; i < newline_split.length; i++){
final_values = newline_split[i].
replace("\n"," ").replaceAll("\\s{2,}", " ").trim();
XSSFRichTextString text = new XSSFRichTextString(final_values);
XSSFFont font = text.getFontAtIndex(0);
Boolean font_striked = font.getStrikeout();
} // for ends here

最佳答案

您需要首先获取 RichTextString,然后遍历所有 FormattingRun,检查它是否被划掉,如果没有,然后获取适当的子字符串和将其放入 List:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.CellType.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;

import java.util.List;
import java.util.ArrayList;

class ReadExcelRichTextCells {

public static void main(String[] args) throws Exception {

Workbook wb = WorkbookFactory.create(new FileInputStream("ExcelRichTextCells.xlsx"));

Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {

switch (cell.getCellTypeEnum()) {
case STRING:
XSSFRichTextString richtextstring = (XSSFRichTextString)cell.getRichStringCellValue();
String textstring = richtextstring.getString();

List<String> textparts = new ArrayList<String>();

if (richtextstring.hasFormatting()) {
for (int i = 0; i < richtextstring.numFormattingRuns(); i++) {

if (richtextstring.getFontOfFormattingRun(i)==null || !richtextstring.getFontOfFormattingRun(i).getStrikeout()) {

int indexofformattingrun = richtextstring.getIndexOfFormattingRun(i);
String textpart = textstring.substring(indexofformattingrun,
indexofformattingrun + richtextstring.getLengthOfFormattingRun(i));
String[] textpart_split = textpart.split("-");
for (int j = 0; j < textpart_split.length; j++){
String text = textpart_split[j].replace("\n", "").trim();
if (!"".equals(text)) textparts.add(text);
}
}
}
} else {
textparts.add(textstring);
}

System.out.println(textparts);
break;

//...
default:
System.out.println("default cell"); //should never occur
}
}
}

wb.close();

}
}

关于java - 无法使用 apache poi 从 excel 中检测删除线数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46296239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com