首页 > 代码库 > JFile的导入xlsx与xls

JFile的导入xlsx与xls

首先需要有JAVA的一些jar包

你可以去这里下载:http://download.csdn.net/detail/qq_35980546/9892511

你要先配置好路由,还有能拿到绝对路径才行

下面直接给源码:

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.piccolo.io.FileFormatException;
import org.apache.poi.ss.usermodel.Sheet;

import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;

public class ExcelController extends Controller {
      private static final String XLS = "xls";
      private static final String XLSX = "xlsx";

 

      public void Excel() throws FileNotFoundException, FileFormatException{//Excel数据导入
          //String Excelion = PathKit.getWebRootPath() + "//upload//"+this.getPara("Excel");//这是找到项目中的路径
          String Excelion=this.getPara("Excel"); //找到的Excel的绝对路径
          // 检查
          preReadCheck(Excelion);
          // 获取workbook对象
          Workbook workbook = null;

          try {
            workbook = getWorkbook(Excelion);
            Integer ing=0;//长度
            Integer lengthing=0;//表头标记
            int lastColumnIndex = 0;//列数
            // 读文件 一个sheet一个sheet地读取
            for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
              Sheet sheet = workbook.getSheetAt(numSheet);
              if (sheet == null) {
                break;
              }
              int firstRowIndex = sheet.getFirstRowNum();
              int lastRowIndex = sheet.getLastRowNum();
              Row firstRow = sheet.getRow(firstRowIndex);
              for (int i = firstRow.getFirstCellNum(); i <= firstRow.getLastCellNum(); i++) {
                Cell cell = firstRow.getCell(i);
                String cellValue = http://www.mamicode.com/getCellValue(cell, true);//表头数据
                System.out.println("表头数据:"+cellValue);
              }
              //System.out.println("");

              // 读取数据行
              for (int rowIndex = firstRowIndex + 1; rowIndex <= lastRowIndex; rowIndex++) {
                Row currentRow = sheet.getRow(rowIndex);// 当前行
                int firstColumnIndex = currentRow.getFirstCellNum(); // 第一列
                lastColumnIndex = currentRow.getLastCellNum();// 总列数据,也是最后一列
                for (int columnIndex = firstColumnIndex; columnIndex <= lastColumnIndex; columnIndex++) {
                  Cell currentCell = currentRow.getCell(columnIndex);// 当前格
                  String currentCellValue = http://www.mamicode.com/getCellValue(currentCell, true);// 当前格的值
                  //System.out.print(currentCellValue + "\t");
                  System.out.println("表头数据:"+currentCellValue);
                }
              }
            }

          } catch (Exception e) {
            e.printStackTrace();
          }

    }


    private static void preReadCheck(String filePath) throws FileNotFoundException, FileFormatException {
      // 常规检查
      File file = new File(filePath);
      if (!file.exists()) {
        throw new FileNotFoundException("传入的文件不存在:" + filePath);
      }

      if (!(filePath.endsWith(XLS) || filePath.endsWith(XLSX))) {
        throw new FileFormatException("传入的文件不是excel");
      }
    }

    private static String getCellValue(Cell cell, boolean treatAsStr) {
    if (cell == null) {
      return "";
    }

    if (treatAsStr) {
      // 虽然excel中设置的都是文本,但是数字文本还被读错,如“1”取成“1.0”
      // 加上下面这句,临时把它当做文本来读取
      cell.setCellType(Cell.CELL_TYPE_STRING);
    }

    if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
      return String.valueOf(cell.getBooleanCellValue());
    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
      return String.valueOf(cell.getNumericCellValue());
    } else {
      return String.valueOf(cell.getStringCellValue());
    }

 

}
   private static Workbook getWorkbook(String filePath){
      Workbook workbook = null;
      InputStream is;
      try {
          is = new FileInputStream(filePath);
          if (filePath.endsWith(XLS)) { //判断Excel为2003还是2007版本
            workbook = new HSSFWorkbook(is);
          } else if (filePath.endsWith(XLSX)) {
            workbook = new XSSFWorkbook(is);
          }
        } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }

      return workbook;
    }

}

这里有一些引用是多余的,你们再删除吧!

这是读到Excel的两个版本,前提是你能拿到绝对的路径。
这里我可以给你们拿到到绝对路径的一种方法,就是把文件上传到Tomcat服务器中,然后就读取Tomcat中的文件路径,网上有一大把上传的资料。
如果找不到上传到Tomcat服务器中的代码可以留言

JFile的导入xlsx与xls