Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Locale;
- import java.util.Scanner;
- public class SmallShop {
- public static void main(String[] args) throws IOException {
- Scanner myScan = new Scanner(System.in);
- String product = myScan.nextLine();
- String town = myScan.nextLine();
- double quan = Double.parseDouble(myScan.nextLine());
- Locale.setDefault(Locale.ROOT);
- InputStream excelFileToRead = new FileInputStream
- ("D:\\Documents\\IdeaProjects\\1Steps\\src\\SmallShopInfo.xlsx");
- HSSFWorkbook wb = new HSSFWorkbook(excelFileToRead);
- Sheet sheet = wb.getSheetAt(0);
- int indexR = getIndex(sheet,town);
- int indexC = getIndex(sheet,product);
- if (!(indexR == 0 || indexC == 0)) {
- Cell cell = sheet.getRow(indexR).getCell(indexC);
- double price = cell.getNumericCellValue();
- double sum = quan * price;
- System.out.println(sum);
- }
- }
- private static int getIndex(Sheet sheet, String text) {
- for (Row row : sheet) {
- for (Cell cell : row) {
- if (cell.getCellType() == Cell.CELL_TYPE_STRING
- &&cell.getStringCellValue().equals(text)
- &&(row.getRowNum()==0||cell.getColumnIndex()==0)){
- return row.getRowNum()+cell.getColumnIndex();
- }
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment