java获取excle中的内容测试源码

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;


import org.apache.poi.hssf.usermodel.HSSFCell;
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 org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
 * 读取excel中的内容
 * @author colbor
 *
 */
public class DuruExcel {
 public static void main(String[] args) {  
//        readXml("E:/student.xlsx");  
//        System.out.println("-------------");  
       readXml("E:/student.xls");  
   }  
   public static void readXml(String fileName){  
       boolean isE2007 = false;    //判断是否是excel2007格式  
       if(fileName.endsWith("xlsx"))  
           isE2007 = true;  
       try {  
           InputStream input = new FileInputStream(fileName);  //建立输入流  
           Workbook wb  = null;  
           //根据文件格式(2003或者2007)来初始化  
           if(isE2007)  
               wb = new XSSFWorkbook(input);  
           else  
               wb = new HSSFWorkbook(input);  
           Sheet sheet = wb.getSheetAt(0);     //获得第一个表单  
           Iterator<Row> rows = sheet.rowIterator(); //获得第一个表单的迭代器  
           while (rows.hasNext()) {  
               Row row = rows.next();  //获得行数据  
//                System.out.print(row.getRowNum()+" ");  //获得行号从0开始  
               Iterator<Cell> cells = row.cellIterator();    //获得第一行的迭代器  
               while (cells.hasNext()) {  
                   Cell cell = cells.next();  
//                    System.out.print( cell.getColumnIndex()+" ");  
                   switch (cell.getCellType()) {   //根据cell中的类型来输出数据  
                   case HSSFCell.CELL_TYPE_NUMERIC:  
                       System.out.print(cell.getNumericCellValue()+" ");  
                       break;  
                   case HSSFCell.CELL_TYPE_STRING:  
                       System.out.print(cell.getStringCellValue()+" ");  
                       break;  
                   case HSSFCell.CELL_TYPE_BOOLEAN:  
                       System.out.print(cell.getBooleanCellValue()+" ");  
                       break;  
                   case HSSFCell.CELL_TYPE_FORMULA:  
                       System.out.print(cell.getCellFormula());  
                       break;  
                   default:  
                       System.out.print("unsuported sell type");  
                   break;  
                   }  
               }  
           }  
       } catch (IOException ex) {  
           ex.printStackTrace();  
       }  
   }  
}
当然,这是一个使用 Apache POI 库读取 Excel 文件数据的简单示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.IOException; public class ExcelReaderDemo { public static void main(String[] args) { String excelFilePath = "path/to/your/excel/file.xlsx"; try (FileInputStream inputStream = new FileInputStream(excelFilePath); Workbook workbook = new XSSFWorkbook(inputStream)) { Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 for (Row row : sheet) { for (Cell cell : row) { CellType cellType = cell.getCellType(); if (cellType == CellType.STRING) { System.out.print(cell.getStringCellValue() + "\t"); } else if (cellType == CellType.NUMERIC) { System.out.print(cell.getNumericCellValue() + "\t"); } else if (cellType == CellType.BOOLEAN) { System.out.print(cell.getBooleanCellValue() + "\t"); } } System.out.println(); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例代码,我们首先通过 FileInputStream 打开 Excel 文件,然后使用 XSSFWorkbook 类创建 Workbook 对象。接下来,我们使用 getSheetAt() 方法获取第一个工作表。然后,我们遍历每一行和每个单元格,并根据单元格的类型打印出相应的值。 请确保将 `path/to/your/excel/file.xlsx` 替换为你要读取的实际 Excel 文件的路径。 希望这个示例对你有所帮助!如有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值