Java程序可以通过两种方式读取Excel文件数据:使用Apache POI框架:导入所需库。实例化Workbook对象。遍历工作表和单元格。使用JXL库:导入所需库。实例化Workbook对象。遍历工作表和单元格。
Java读取Excel文件数据
如何读取Excel文件数据?
Java程序可以通过Apache POI框架和JXL库来读取Excel文件数据。
使用Apache POI读取Excel数据
import org.apache.poi.ss.usermodel.*
Workbook workbook = WorkbookFactory.create(new FileInputStream("excel_file.xlsx"));
for (Sheet sheet : workbook) { ... }
String cellValue = sheet.getRow(rowNumber).getCell(columnNumber).toString();
使用JXL读取Excel数据
import jxl.*
Workbook workbook = Workbook.getWorkbook(new FileInputStream("excel_file.xls"));
for (Sheet sheet : workbook.getSheets()) { ... }
String cellValue = sheet.getCell(columnNumber, rowNumber).getContents();
示例代码(使用Apache POI)
<code class="java">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.ss.usermodel.WorkbookFactory; import java.io.FileInputStream; import java.io.IOException; public class ReadExcel { public static void main(String[] args) throws IOException { Workbook workbook = WorkbookFactory.create(new FileInputStream("excel_file.xlsx")); for (Sheet sheet : workbook) { for (Row row : sheet) { for (Cell cell : row) { System.out.println(cell.toString()); } } } } }</code>
以上是java怎么读取excel文件数据的详细内容。更多信息请关注PHP中文网其他相关文章!