简体   繁体   English

如何获得Excel工作表的每一行和每一列的值

[英]How to get the excel sheet each row and column value

I have the excel file the code works fine, but how can I get each column and row value differently so that I can store the value in database. 我有一个excel文件,代码工作正常,但是如何以不同的方式获取每个列和行的值,以便可以将值存储在数据库中。 Thank you in advance 先感谢您

public class excel_demo {
public static void main(String[] args) {
    try {
        FileInputStream file = new FileInputStream(new File("C:\\Users\\Admin\\Downloads\\ExcelDemosWithPOI\\howtodoinjava_demo.xlsx"));

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

        //Iterate through each rows one by one
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                //Check the cell type and format accordingly
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                      System.out.print(cell.getNumericCellValue() + "\t");                            
                        break;
                    case Cell.CELL_TYPE_STRING:
                       System.out.print(cell.getStringCellValue() + "\t");
                        break;
                }
            }
            System.out.println("");
        }
        file.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

} }

download JEXCEL api,and use this code, 下载JEXCEL api,并使用此代码,

import jxl.*;//import jxl package.


File excelSheet = null;
Workbook workbook = null;
Workbook wb = Workbook.getWorkbook(new File(destFile));//destFile is excel file
Sheet sheet = wb.getSheet(sheetNo);
columns = sheet.getColumns();
rows = sheet.getRows();
for(int row = 0;row <rows;row++)
{
   for(int col =0;col <columns;col++)
   {
      a[row][col] =Integer.parseInt( sheet.getCell(col,row).getContents());
   }
}

Hope this helps.. 希望这可以帮助..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Apache POI获取java中各行excel表的最后一列值 - how to get last column value of respective row of excel sheet in java using Apache POI 如果第一个和最后一个单元格值为空或空白,如何读取 Excel 工作表行的每个单元格的值? - How to read each and every cell's value of the row of Excel sheet if 1st and last cell value is Null or Blank? 使用POI从Excel工作表的下一行获取价值 - Get value from next row in Excel sheet with POI 如何使用列名称遍历Excel工作表中的当前行? - How to iterate over current row in an excel sheet using column name? 使用Java读取Excel工作表的每一行 - Reading each row of excel sheet using Java 如何使用POI API获取一个Excel工作表的完整行并检查该行是否存在于另一个工作表中? - How to get complete row of one excel sheet using POI API and check the row whether it is present in another sheet? 如何使用Java从Excel工作表中获取特定列名(作为参数传递)的最后一个非空单元格的行索引? - How to get the row index of last non-empty cell for a specific column name(passed as an argument) from an excel sheet using java? 如何使用Java在Excel工作表的列中找到最大值? - How to find maximum value in a column of an Excel sheet using java? 从webtable中获取值并在循环中的每一行中一一写入excel表中 - Fetching value from webtable and write in excel sheet one by one of each row in a loop 如何使用Java POI从Excel获取有关行和列值的特定单元格值 - How to get Specific Cell Value from Excel With Respect To Row And Column Value using Java POI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM