码迷,mamicode.com
首页 > 编程语言 > 详细

java读取excel

时间:2021-06-02 12:07:16      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:util   not   类型   host   bre   work   tac   att   break   

java读取excel

使用 jxl读取

依赖
    
     <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>
    
注意: 读取的excel为 2003版本 ,新版本会报错 Unable to recognize OLE stream
 public static void main(String[] args) {
        File file = new File("C:" + File.separator+ "Users" + File.separator+ "fangbin" + File.separator + "Desktop" + File.separator + "20210527开发需求" + File.separator + "allhosts2003.xls");
        try {
            Workbook workbook = Workbook.getWorkbook(file);
            Sheet sheet = workbook.getSheet(0);
            for (int i = 0; i < sheet.getRows(); i++) {
                System.out.println(i +" "+ sheet.getRow(i)[0].getContents() + " " + sheet.getRow(i)[1].getContents() );
                if (StringUtils.isBlank(sheet.getRow(i)[0].getContents())) {
                    break;
                }
                /*for (int j = 0 ; j < sheet.getColumns(); j++){
                    Cell cell = sheet.getCell(j,i);
                    System.out.println(cell.getContents() + );
                }*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

HSSFWorkbook

package com.ij34.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;

public class Test02 {
 public static void main(String[] args) throws FileNotFoundException, IOException {
 File excelFile = new File("table01.xls");
 HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
 HSSFSheet sheet = wb.getSheetAt(0);
 
 for (Row row : sheet) {
  for (Cell cell : row) {
  switch (cell.getCellType()) {
  case Cell.CELL_TYPE_STRING://字符串
   System.out.print(cell.getRichStringCellValue().getString());
   System.out.print(" ");
   break;
  case Cell.CELL_TYPE_NUMERIC://数值与日期
   if (DateUtil.isCellDateFormatted(cell)) {
   System.out.print(String.valueOf(cell.getDateCellValue()));
   } else {
   System.out.print(cell.getNumericCellValue());
   }
   System.out.print(" ");
   break;
  case Cell.CELL_TYPE_BOOLEAN://boolean类型
   System.out.print(cell.getBooleanCellValue());
   System.out.print(" ");
   break;
  default:
  }
  }
  System.out.println();
 }
}
}

java读取excel

标签:util   not   类型   host   bre   work   tac   att   break   

原文地址:https://www.cnblogs.com/fb010001/p/14814971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!