在java中通过poi对行的列进行迭代
我的c:驱动器(本地计算机)中有一个名为abc.xls的excel文件,现在,在第一张表格中的excel文件中,有一个如下所示的表格,下表可以位于表格中的任何范围内,因此我开发了下面的java程序,该程序将首先逐行扫描整个表格,然后逐列扫描,并找到其中的单元格 TradeRef在那里吗在java中通过poi对行的列进行迭代,java,apache-poi,Java,Apache Poi,我的c:驱动器(本地计算机)中有一个名为abc.xls的excel文件,现在,在第一张表格中的excel文件中,有一个如下所示的表格,下表可以位于表格中的任何范围内,因此我开发了下面的java程序,该程序将首先逐行扫描整个表格,然后逐列扫描,并找到其中的单元格 TradeRef在那里吗 TradeRef TMS Deal Date 12 45 DRT 23/97/2014 23 36 QWE 21
TradeRef TMS Deal Date
12 45 DRT 23/97/2014
23 36 QWE 21/07/2015
现在,我下面的程序中的问题是,它捕获TradeRef所在的单元格,然后在列上迭代,然后以类似的方式捕获下一行并在列上迭代
但我想应用的逻辑是,当它捕获TradeRef单元格并对列进行迭代并到达上表中的最后一列(即上表中的日期)时,它应该进一步扫描同一行中的下20列,如果在下20列中没有具有任何值的单元格,那么它应该移动到下一行,如果在20列中,任何单元格都可以有值,那么在这种情况下,它应该读取该单元格值
那就好像
TradeRef TMS Deal Date <----- scan next 20 columns is there is no value in next 20 cells then move to next row else include that cell value also------->
12 45 DRT 23/97/2014
23 36 QWE 21/07/2015
TradeRef TMS交易日期
12 45 DRT 23/97/2014
23 36 QWE 2015年7月21日
因此,请建议如何实现上述逻辑,即扫描下一行中的下20列,这是我之前的实现,即
public class AAA {
public static void main(String[] args) throws IOException {
FileInputStream file = null ;
try {
file = new FileInputStream(new File("C:\\abc.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
Cell c = findFirstRow(firstSheet);
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
file.close();
}
}
public static Cell findFirstRow(HSSFSheet firstSheet) {
for (Row row : firstSheet) {
for (Cell cell : row) {
cell.setCellType(cell.CELL_TYPE_STRING);
if ("TradeRef".equals(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());
}
return startOfFirstDataRow;
}
}
}
throw new RuntimeException("TradingRef header cell not found!");
}
}
公共级AAA{
公共静态void main(字符串[]args)引发IOException{
FileInputStream文件=null;
试一试{
file=newfileinputstream(新文件(“C:\\abc.xls”);
HSSF工作簿=新的HSSF工作簿(文件);
HSSFSheet firstSheet=工作簿。getSheetAt(0);
迭代器迭代器=firstSheet.Iterator();
单元c=findFirstRow(第一页);
}
catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
file.close();
}
}
公共静态单元findFirstRow(HSSF表第一张){
用于(行:第一页){
用于(单元格:行){
cell.setCellType(cell.cell\u TYPE\u字符串);
如果(“TradeRef.equals(cell.getStringCellValue())){
int row1=cell.getRowIndex()+1;
int col=cell.getColumnIndex();
if(firstSheet.getRow(行1)==null)
抛出新的RuntimeException(“行“+row1+1+”为空!”);
Cell startOfFirstDataRow=firstSheet.getRow(行1.getCell(列);
if(startOfFirstDataRow==null){
CellReference ref=新的CellReference(第1行,第2列);
抛出新的RuntimeException(“在“+ref.formataString()处找不到数据”);
}
返回startOfFirstDataRow;
}
}
}
抛出新的RuntimeException(“未找到TradingRef头单元格!”);
}
}
因此,请告知我如何实现扫描接下来20列的上述逻辑。首先,您可能应该使用
org.apache.poi.ss.usermodel
API,它可以处理所有Excel文件,而HSSF*
类只能处理.xls
文件
org.apache.poi.ss.usermodel.Sheet类有一个方法,可以用来启动搜索。接下来,您要查找包含给定字符串的单元格序列,如:
// This is an array containing the headers
final String[] headers = { "TradeRef", "TMS", "Deal", "Date" };
// 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 < headers.length && c + h < last; h++) {
if (!headers[h].equals(row.getCell(c + h).getStringCellValue())) {
break; // if the cell value differs from our header
}
}
if (h == headers.length) // this means the break was never invoked
return c; // found it
}
return -1; // not found
//这是一个包含头的数组
最终字符串[]头={“TradeRef”,“TMS”,“Deal”,“Date”};
//现在,我们从上面取row作为row对象,在其中查找标题
int last=row.getLastCellNum();
for(int c=row.getFirstCellNum();c
首先,您可能应该使用org.apache.poi.ss.usermodel
API,它可以处理所有Excel文件,而HSSF*
类只能处理.xls
文件
org.apache.poi.ss.usermodel.Sheet类有一个方法,可以用来启动搜索。接下来,您要查找包含给定字符串的单元格序列,如:
// This is an array containing the headers
final String[] headers = { "TradeRef", "TMS", "Deal", "Date" };
// 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 < headers.length && c + h < last; h++) {
if (!headers[h].equals(row.getCell(c + h).getStringCellValue())) {
break; // if the cell value differs from our header
}
}
if (h == headers.length) // this means the break was never invoked
return c; // found it
}
return -1; // not found
//这是一个包含头的数组
最终字符串[]头={“TradeRef”,“TMS”,“Deal”,“Date”};
//现在,我们从上面取row作为row对象,在其中查找标题
int last=row.getLastCellNum();
for(int c=row.getFirstCellNum();c
row.cellIterator非常了解单元格的数量,可以进一步扫描(row.getCell),但完全是无意义的@ammoQ可能的副本感谢您的建议,请请求您展示我们如何扫描接下来的20列。row.cellIterator非常了解单元格的数量,可以进一步扫描(row.getCell)但是是完全无意义的@ammoQ可能的副本谢谢你的建议请要求你展示我们如何扫描接下来的20列。cellIterator非常清楚单元格的数量,可以进一步扫描(row.getCell),但完全是点