Java 如何将2行标题从';xls&x27;文件
我正在尝试从excel文件创建一个标题,该文件的表标题位于两个不同的行上。我必须从这两个标题中划出一行 我所遵循的方法是在第一个头上附加一个@,并尝试用第二个头值替换它 我的excel文件如下所示Java 如何将2行标题从';xls&x27;文件,java,excel,string,if-statement,stringbuffer,Java,Excel,String,If Statement,Stringbuffer,我正在尝试从excel文件创建一个标题,该文件的表标题位于两个不同的行上。我必须从这两个标题中划出一行 我所遵循的方法是在第一个头上附加一个@,并尝试用第二个头值替换它 我的excel文件如下所示 Bank Positive Name of Local Approah Value Customer Civilian Remote //some data 这是我使用的代码 public List<String> processSheet(Sheet
Bank Positive Name of Local Approah
Value Customer Civilian Remote
//some data
这是我使用的代码
public List<String> processSheet(Sheet sheet) {
ActivityLauncher.logConsoleMsg("Processing sheet " + sheet.getSheetName() + " started");
List<String> rows = new ArrayList<String>();
Iterator<Row> rowIterator = sheet.iterator();
List<String>firstHeader = new ArrayList<String>();
List<String>secondHeader = new ArrayList<String>();
List<String>finalHeader = new ArrayList<String>();
while (rowIterator.hasNext()) {
Row currentRow = rowIterator.next();
StringBuilder row = new StringBuilder();
for (int i = 0; i < currentRow.getLastCellNum(); i++) {
if(currentRow.getRowNum()==0 || currentRow.getRowNum()==1 || currentRow.getRowNum()==3 || currentRow.getRowNum()==2) {
continue;
} else {
Cell currentCell = currentRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String cellValue = excelManager.evalCell(currentCell);
//inserting the alternate row into the text file to make a continues header
if(currentCell.getRowIndex()==4 ) {
if(cellValue.equals("Local Risk")) {
row.append(cellValue);
row.append("@");
break;
} else {
row.append(cellValue);
row.append("@");
}
}
if(currentCell.getRowIndex()==5) {
if(cellValue.equals("rating")) {
row.append(cellValue);
row.append("@");
break;
} else {
int pos = row.indexOf("@");
if(pos >=0) {
row.replace(pos, 1, cellValue);
}
}
}
}
}
if (!row.toString().isEmpty()) {
rows.add(row.toString());
}
}
ActivityLauncher.logConsoleMsg("Processing sheet " + sheet.getSheetName() + " completed");
return rows;
}
但我的目标是
Bank Value Positive Customer Name of Civilian Approach Remote
有人能帮我解决这个问题吗?row.toString().replaceAll(“@”,cellValue)
无效,因为调用toString
和后续替换后得到的字符串将被丢弃并被垃圾回收
如果要进行就地替换,请使用indexOf
和replace(int,int,String)
:
此外,当扫描与插入“@”
字符不同的行时,必须进行替换,因此,当进入第5行时,需要确保第4行中的StringBuilder
仍然存在。要使其工作,请在循环外部声明行
,并仅在当前行不等于4时将其替换为新行:
StringBuilder row = new StringBuilder();
while (rowIterator.hasNext()) {
Row currentRow = rowIterator.next();
...
if (currentCell.getRowIndex() == 4) {
continue;
}
if (!row.toString().isEmpty()) {
rows.add(row.toString());
row = new StringBuilder();
}
}
row.toString().replaceAll(“@”,cellValue)
无效,因为调用toString
和后续替换后得到的字符串将被丢弃并被垃圾回收
如果要进行就地替换,请使用indexOf
和replace(int,int,String)
:
此外,当您扫描插入的'@'
字符的不同行时,必须进行替换,因此您需要确保第4行的StringBuilder
在进入第5行时“存活”。要使其工作,请在循环外部声明行
,并仅在当前行不等于4时将其替换为新行:
StringBuilder row = new StringBuilder();
while (rowIterator.hasNext()) {
Row currentRow = rowIterator.next();
...
if (currentCell.getRowIndex() == 4) {
continue;
}
if (!row.toString().isEmpty()) {
rows.add(row.toString());
row = new StringBuilder();
}
}
小提琴的外部链接: 我用硬编码的数据制作了一个与下面的例子几乎相同的算法
对于标题,最好用两行创建一个列表,并在完成后追加字符串 请尝试以下代码:
public List<String> processSheet(Sheet sheet) {
List<String> headers = new ArrayList<>();
Iterator<Row> rowIterator = sheet.iterator();
int rowCnt = 0;
while (rowIterator.hasNext()) {
Row currentRow = rowIterator.next();
for (int i = 0; i < currentRow.getLastCellNum(); ++i) {
Cell currentCell = currentRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String cellValue = excelManager.evalCell(currentCell);
headers.add(rowCnt + (i * (rowCnt+1)), cellValue);
}
++rowCnt;
}
return headers;
}
公共列表流程表(工作表){
列表标题=新建ArrayList();
迭代器rowIterator=sheet.Iterator();
int rowCnt=0;
while(roweiterator.hasNext()){
Row currentRow=rowIterator.next();
对于(int i=0;i
该算法适用于任意数量的行和列,只要每行的列数相同。使用遵循相同算法的硬编码示例数据。完成此操作后,可以将字符串2×2追加(如果标题高达3行,则可以将字符串3×3追加)。类似这样的算法应该适合:
private static List<String> appendStrings(List<String> original, int group) throws Exception {
if (original.size() % group == 0) {
List<String> newList = new ArrayList<>();
for (int i = 0; i < original.size(); i += group) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < group; ++j) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(original.get(i + j));
}
newList.add(sb.toString());
}
return newList;
} else {
throw new Exception("MyClass::appendStrings() ---> the group value must be a factor of the size of the original list");
}
}
private static List appendStrings(List original,int-group)引发异常{
如果(原始.size()%group==0){
List newList=newarraylist();
对于(int i=0;i0){
某人加上(“”);
}
sb.append(原始get(i+j));
}
添加(sb.toString());
}
返回newList;
}否则{
抛出新异常(“MyClass::AppendString()-->组值必须是原始列表大小的一个因子”);
}
}
如果您的行可能包含空单元格,并且不希望添加空字符串,但仍希望遵守顺序,则需要在生成列表后删除数据。像这样:
public List<String> processSheet(Sheet sheet) {
/*... same code as previous example ...*/
// the following removes all "" or null values
while(headers.remove(null) || headers.remove("")){}
return headers;
}
公共列表流程表(工作表){
/*…与上一个示例相同的代码*/
//下面将删除所有“”或空值
while(headers.remove(null)| | headers.remove(“”){
返回标题;
}
小提琴的外部链接:
我用硬编码的数据制作了一个与下面的例子几乎相同的算法
对于标题,最好用两行创建一个列表,并在完成后追加字符串 请尝试以下代码:
public List<String> processSheet(Sheet sheet) {
List<String> headers = new ArrayList<>();
Iterator<Row> rowIterator = sheet.iterator();
int rowCnt = 0;
while (rowIterator.hasNext()) {
Row currentRow = rowIterator.next();
for (int i = 0; i < currentRow.getLastCellNum(); ++i) {
Cell currentCell = currentRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String cellValue = excelManager.evalCell(currentCell);
headers.add(rowCnt + (i * (rowCnt+1)), cellValue);
}
++rowCnt;
}
return headers;
}
公共列表流程表(工作表){
列表标题=新建ArrayList();
迭代器rowIterator=sheet.Iterator();
int rowCnt=0;
while(roweiterator.hasNext()){
Row currentRow=rowIterator.next();
对于(int i=0;i
该算法适用于任意数量的行和列,只要每行的列数相同。使用遵循相同算法的硬编码示例数据。完成此操作后,可以将字符串2×2追加(如果标题高达3行,则可以将字符串3×3追加)。类似这样的算法应该适合:
private static List<String> appendStrings(List<String> original, int group) throws Exception {
if (original.size() % group == 0) {
List<String> newList = new ArrayList<>();
for (int i = 0; i < original.size(); i += group) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < group; ++j) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(original.get(i + j));
}
newList.add(sb.toString());
}
return newList;
} else {
throw new Exception("MyClass::appendStrings() ---> the group value must be a factor of the size of the original list");
}
}
private static List appendStrings(List original,int-group)引发异常{
如果(原始.size()%group==0){
List newList=newarraylist();
对于(int i=0;i0){
某人加上(“”);
}
sb.append(原始get(i+j));