我有以下java程序,试图通过java poi读取excel表,首先看下面的excel表格式如下图所示
abcRef 21
ABC 20
ERT 60
FGT 57
abcRef SMS Seal Total
12 45 DRT 3000
23 36 QWE 2000
subtotal 5000
abcRef SMS Seal Total
18 25 hRT 1000
29 16 tWE 2100
subtotal 3100
RTY 57
TDZ 21
YUI 98
所以你可以看到上面我需要捕获从abcRef开始并在Total列结束的所有表格范围,所以为此我添加了首先捕获此组合的标题单元格的条件,换句话说首先找到标题细胞
abcRef+SMS+Seal+Total
我能够实现,所以我的下面的程序首先遍历所有行,然后尝试找到组合的标题单元格,然后打印它正在做的所有值
但是我很少需要你提出建议的挑战
我只想在控制台上打印表格,所以我想在
时打破我的程序所以请告知我如何自定义下面的代码以便我可以在我的控制台上打印以下内容
abcRef SMS Seal Total
12 45 DRT 3000
23 36 QWE 2000
abcRef SMS Seal Total
18 25 hRT 1000
29 16 tWE 2100
下面是我在下面的程序仪式中的代码片段,其中只打印标题行,而不是如上所示我想要
public class abc{
public static void main(String[] args) throws Exception {
FileInputStream file = null ;
try {
HSSFRow r =findgetRowNo();
getAllColumnFromRow(r);
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
file.close();
}
}
public static void getAllColumnFromRow(HSSFRow RowObject){
Iterator<Cell> itr = RowObject.iterator();
HSSFRow headerRow = RowObject.getSheet().getRow(0);
String cellValue = "";
String information = "";
int headerCellCount = 0;
//to avoid first column
while(itr.hasNext()){
Cell cell = itr.next();
Cell headerValue = headerRow.getCell(headerCellCount++);
switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue = cell.getBooleanCellValue() +"";
information = information + " " +headerValue+" - "+cellValue+ "; ";
break;
case HSSFCell.CELL_TYPE_NUMERIC:
cellValue = cell.getNumericCellValue() + "";
information = information + " " +headerValue+" - "+cellValue+ "; ";
break;
case HSSFCell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
System.out.println(cellValue);
information = information + " " +headerValue+" - "+cellValue+ "; ";
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
}
}
System.out.println("@@@@");
}
public static int findHeaderCell(HSSFSheet firstSheet) {
final String[] headers1 = { "abcRef", "SMS", "Seal", "Total "};
List<String> listHeader=Arrays.asList(headers1);
for (Row row : firstSheet) {
for (Cell cell : row) {
if(listHeader.contains(cell.getStringCellValue())) {
int row1 = cell.getRowIndex() + 1;
int col = cell.getColumnIndex();
if (firstSheet.getRow(row1) == null)
throw new RuntimeException("Row " + row1 + 1 + " is empty!");
Cell startOfFirstDataRow = firstSheet.getRow(row1).getCell(col);
if (startOfFirstDataRow == null) {
CellReference ref = new CellReference(row1, col);
throw new RuntimeException("Data not found at " + ref.formatAsString());
}
// now we take row from above to be the Row object where we seek our headers
int last = row.getLastCellNum();
for (int c = row.getFirstCellNum(); c < last; c++) {
int h = 0;
// check if the cell at (c + h) has the required value
for (; h < listHeader.size() && c + h < last; h++) {
if (!listHeader.get(h).equals(row.getCell(c + h).getStringCellValue())) {
System.out.println("headers not match as Expected");
break; // if the cell value differs from our header
}
}
int t;
if (h == listHeader.size()) // this means the break was never invoked
System.out.println("headers Matched as Expected");
return row.getRowNum();
// return c; // found it
}
// not found
return -1;
}
}
}
throw new RuntimeException("TradingRef header cell not found!");
}
public static HSSFRow findgetRowNo() throws Exception {
FileInputStream file = null ;
file = new FileInputStream(new File("C:\\abc.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet firstSheet1 = workbook.getSheetAt(0);
Iterator<Row> rowItr = firstSheet1.iterator();
final String[] headers1 = { "abcRef", "SMS", "Seal", "Total "}
List<String> listHeader=Arrays.asList(headers1);
for (Row row : firstSheet1) {
for (Cell cell : row) {
if(listHeader.contains(cell.getStringCellValue())) {
int row1 = cell.getRowIndex() + 1;
int col = cell.getColumnIndex();
if (firstSheet1.getRow(row1) == null)
throw new RuntimeException("Row " + row1 + 1 + " is empty!");
Cell startOfFirstDataRow = firstSheet1.getRow(row1).getCell(col);
if (startOfFirstDataRow == null) {
CellReference ref = new CellReference(row1, col);
throw new RuntimeException("Data not found at " + ref.formatAsString());
}
// now we take row from above to be the Row object where we seek our headers
int last = row.getLastCellNum();
for (int c = row.getFirstCellNum(); c < last; c++) {
int h = 0;
// check if the cell at (c + h) has the required value
for (; h < listHeader.size() && c + h < last; h++) {
if (!listHeader.get(h).equals(row.getCell(c + h).getStringCellValue())) {
System.out.println("headers not match as Expected");
break; // if the cell value differs from our header
}
}
int t;
if (h == listHeader.size()) // this means the break was never invoked
System.out.println("headers Matched as Expected");
row.getPhysicalNumberOfCells();
return (HSSFRow) row;
// return c; // found it
}
// not found
return null;
}
}
}
throw new RuntimeException("abcRef header cell not found!");
}
}
有些人可以就此提出建议吗