Hi ich benötige Hilfe bei meinem Code.
Alle anderen Methoden außer die findAdressByName funktionieren. Es wird die ganze Zeit der leere String returned, aber wich weiß nicht warum..
[CODE lang="java" title="Main"]package utils;
public class Main {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ExcelUtils eu = new ExcelUtils("./data/Test.xlsx");
eu.findAddressByName("Henry");
eu.getCellData();
}
}[/CODE]
[CODE lang="java" title="ExcelUtils"]package utils;
import java.io.File;
import java.io.IOException;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
static XSSFWorkbook workbook;
static XSSFSheet sheet;
public ExcelUtils(String excelPath) throws IOException{
workbook = new XSSFWorkbook(excelPath);
sheet = workbook.getSheet("Tabelle1");
}
public static void getCellData() throws IOException{
DataFormatter formatter = new DataFormatter();
Object value = formatter.formatCellValue(sheet.getRow(1).getCell(0));
System.out.println(value);
}
public static void getRowCount() throws IOException {
int rowCount = sheet.getPhysicalNumberOfRows();
System.out.println("Number of Row Count" + rowCount);
}
public static String findAddressByName(String nameToSearch) throws IOException {
for (int rowIndex = 0; rowIndex < sheet.getLastRowNum(); rowIndex++) {
XSSFRow row = sheet.getRow(rowIndex);
if (row != null && row.getCell(0).getStringCellValue().equals(nameToSearch)) {
return row.getCell(1).getRawValue();
}
}
return "";
}
}
[/CODE]
Alle anderen Methoden außer die findAdressByName funktionieren. Es wird die ganze Zeit der leere String returned, aber wich weiß nicht warum..
[CODE lang="java" title="Main"]package utils;
public class Main {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ExcelUtils eu = new ExcelUtils("./data/Test.xlsx");
eu.findAddressByName("Henry");
eu.getCellData();
}
}[/CODE]
[CODE lang="java" title="ExcelUtils"]package utils;
import java.io.File;
import java.io.IOException;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
static XSSFWorkbook workbook;
static XSSFSheet sheet;
public ExcelUtils(String excelPath) throws IOException{
workbook = new XSSFWorkbook(excelPath);
sheet = workbook.getSheet("Tabelle1");
}
public static void getCellData() throws IOException{
DataFormatter formatter = new DataFormatter();
Object value = formatter.formatCellValue(sheet.getRow(1).getCell(0));
System.out.println(value);
}
public static void getRowCount() throws IOException {
int rowCount = sheet.getPhysicalNumberOfRows();
System.out.println("Number of Row Count" + rowCount);
}
public static String findAddressByName(String nameToSearch) throws IOException {
for (int rowIndex = 0; rowIndex < sheet.getLastRowNum(); rowIndex++) {
XSSFRow row = sheet.getRow(rowIndex);
if (row != null && row.getCell(0).getStringCellValue().equals(nameToSearch)) {
return row.getCell(1).getRawValue();
}
}
return "";
}
}
[/CODE]