如果数据没有用引号括起来,你真的不能在这里做很多事情。您实际上可以做的就是检查行长度,如果它大于 7,您就知道额外的列是客户端名称的一部分。
这是我的解决方案:
for (String[] row : rows) {
if (row.length > 7) {
int extraColumns = row.length - 7; //we have extra columns
String[] fixed = new String[7]; // let's create a row in the correct format
//copies all data before name
for (int i = 0, j = 0; i < row.length; i++, j++) {
fixed[j] = row[i]; //keep copying values, until we reach the name
if (i == 3) { //hit first column with a name in it
for (int k = i + 1; k <= i + extraColumns; k++) { //append comma and the value that follows the name
fixed[i] += ", " + row[k];
}
i += extraColumns; //increase variable i and keep assigning values after it to position j
}
}
row = fixed; //replaces the original broken row
}
//prints the resulting row, values in square brackets for clarity.
for (String element : row) {
System.out.print('[' + element + ']' + ",");
}
System.out.println();
}
这将产生输出:
[Date],[Code],[Company],[Client],[Type],[Quantity],[Price],
[03/03/2014],[500103],[BHEL],[PoI THROUGH DoI, Affairs],[S],[114100000],[165.55],
[21/04/2017],[533309],[DALMI],[KKR MAURITIUS CEMENT, LTD.],[S],[106020],[2050.00],
[21/04/2017],[533309],[DALMI],[KKR MAURITIUS CEMENT, LTD.],[P],[141740],[2050.00],
希望能帮助到你。