使用Java在Excel中迭代给定的占位符。

huangapple go评论81阅读模式
英文:

Use Java to iterate between given placeholders in Excel

问题

这里是我可以提供的示例内容翻译:

以下是我想要做的示例:
使用Java在Excel中迭代给定的占位符。

是否有一种方法可以输出从startprint到end_print之间的所有单元格?我在Excel中有3个工作表,都有相同的占位符,我只想查看startprint和endprint之间的数据。目前我只能打印出所有3个工作表的所有内容。以下是我的代码:

public class ReadInvoices {
	
	private static final String NAME = "C:\\Users\\........\\excelfile.xlsx";

	public static void main(String[] args) {
		try {
			FileInputStream file = new FileInputStream(new File(NAME));
			Workbook workbook = new XSSFWorkbook(file);
			DataFormatter dataFormatter = new DataFormatter();
			Iterator<Sheet> sheets = workbook.sheetIterator();
			while(sheets.hasNext()) {
				Sheet sh = sheets.next();
				System.out.println("工作表名称为:"+sh.getSheetName());
				System.out.println("---------");
				Iterator<Row> iterator = sh.iterator();
				while(iterator.hasNext()) {
					Row row = iterator.next();
					Iterator<Cell> cellIterator = row.iterator();
					while (cellIterator.hasNext()) {
						Cell cell = cellIterator.next();
						String cellValue = dataFormatter.formatCellValue(cell);

						System.out.print(cellValue+"\t");
					}
					System.out.println();
				}
			}
			workbook.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}

	}

}
英文:

Here is an example of what i could like to do:
使用Java在Excel中迭代给定的占位符。

Is there a way to output all the cells between startprint and end_print, i've got like 3 sheet in excel with same place holder, and i want to view only the data between startprint and endprint, for now all i can do is print out all the 3 sheet with all the content, here is my code:

public class ReadInvoices {
	
	private static final String NAME = &quot;C:\\Users\\........\\excelfile.xlsx&quot;;

	public static void main(String[] args) {
		try {
			FileInputStream file = new FileInputStream(new File(NAME));
			Workbook workbook = new XSSFWorkbook(file);
			DataFormatter dataFormatter = new DataFormatter();
			Iterator&lt;Sheet&gt; sheets = workbook.sheetIterator();
			while(sheets.hasNext()) {
				Sheet sh = sheets.next();
				System.out.println(&quot;Sheet name is &quot;+sh.getSheetName());
				System.out.println(&quot;---------&quot;);
				Iterator&lt;Row&gt; iterator = sh.iterator();
				while(iterator.hasNext()) {
					Row row = iterator.next();
					Iterator&lt;Cell&gt; cellIterator = row.iterator();
					while (cellIterator.hasNext()) {
						Cell cell = cellIterator.next();
						String cellValue = dataFormatter.formatCellValue(cell);

						System.out.print(cellValue+&quot;\t&quot;);
					}
					System.out.println();
				}
			}
			workbook.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}

	}

}

答案1

得分: 1

以下是翻译好的内容:

我没有电子表格来测试这段代码。

processLines 方法测试起始打印行和结束打印行。当我们在起始打印行和结束打印行之间时,我们打印行。

我不确定这行是否正确。我正在尝试从 A 列获取单元格。

String text = row.getColumnObject(0);

编辑:也许这会起作用。

Iterator<Cell> cellIterator = row.iterator();
Cell cell = cellIterator.next();
String text = dataFormatter.formatCellValue(cell);

以下是示例代码。

public class ReadInvoices {

    private static final String NAME = "C:\\Users\\.......\excelfilename.xlsx";

    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream(new File(NAME));
            Workbook workbook = new XSSFWorkbook(file);
            DataFormatter dataFormatter = new DataFormatter();
            Iterator<Sheet> sheets = workbook.sheetIterator();
            while (sheets.hasNext()) {
                Sheet sh = sheets.next();
                System.out.println("Sheet name is " + sh.getSheetName());
                System.out.println("---------");
                processLines(dataFormatter, sh);
            }
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void processLines(DataFormatter dataFormatter, Sheet sh) {
        boolean printLine = false;
        Iterator<Row> iterator = sh.iterator();
        while (iterator.hasNext()) {
            Row row = iterator.next();
            
            Iterator<Cell> cellIterator = row.iterator();
            Cell cell = cellIterator.next();
            String text = dataFormatter.formatCellValue(cell);

            if (printLine && text.equals("End_print")) {
                printLine = false;
            }

            if (printLine) {
                printRow(dataFormatter, row);
            } 

            if (!printLine && text.equals("StartPrint")) {
                printLine = true;
            }
        }
    }

    private static void printRow(DataFormatter dataFormatter, Row row) {
        Iterator<Cell> cellIterator = row.iterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            String cellValue = dataFormatter.formatCellValue(cell);

            System.out.print(cellValue + "\t");
        }
        System.out.println();
    }

}
英文:

I don't have a spreadsheet to test this code.

The processLines method tests for the start print row and the end print row. When we're in between the start print and end print rows, we print the rows.

I'm not sure if this line is correct. I'm trying to get the cell from column A.

String text = row.getColumnObject(0);

Edit: Maybe this will work.

Iterator&lt;Cell&gt; cellIterator = row.iterator();
Cell cell = cellIterator.next();
String text = dataFormatter.formatCellValue(cell);

Here's the example code.

public class ReadInvoices {
private static final String NAME = &quot;C:\\Users\\.......\excelfilename.xlsx&quot;;
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File(NAME));
Workbook workbook = new XSSFWorkbook(file);
DataFormatter dataFormatter = new DataFormatter();
Iterator&lt;Sheet&gt; sheets = workbook.sheetIterator();
while (sheets.hasNext()) {
Sheet sh = sheets.next();
System.out.println(&quot;Sheet name is &quot; + sh.getSheetName());
System.out.println(&quot;---------&quot;);
processLines(dataFormatter, sh);
}
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void processLines(DataFormatter dataFormatter, Sheet sh) {
boolean printLine = false;
Iterator&lt;Row&gt; iterator = sh.iterator();
while (iterator.hasNext()) {
Row row = iterator.next();
Iterator&lt;Cell&gt; cellIterator = row.iterator();
Cell cell = cellIterator.next();
String text = dataFormatter.formatCellValue(cell);
if (printLine &amp;&amp; text.equals(&quot;End_print&quot;)) {
printLine = false;
}
if (printLine) {
printRow(dataFormatter, row);
} 
if (!printLine &amp;&amp; text.equals(&quot;StartPrint&quot;)) {
printLine = true;
}
}
}
private static void printRow(DataFormatter dataFormatter, Row row) {
Iterator&lt;Cell&gt; cellIterator = row.iterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + &quot;\t&quot;);
}
System.out.println();
}
}

huangapple
  • 本文由 发表于 2020年10月16日 16:08:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64385292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定