Read the Excel file readerexcel (fileutil. Java is used) (Excel is read in jxl Mode)

Source: Internet
Author: User
import com.hao.util.FileUtil;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffException;public class ReaderExcel {public static List<String[]> readExcel(String fileName) {return readExcel(FileUtil.getInputStream(fileName));}public static List<String[]> readExcel(File file) {return readExcel(FileUtil.getInputStream(file));}public static List<String[]> readExcel(InputStream in) {List lt = new ArrayList();Workbook wb = null;try {wb = Workbook.getWorkbook(in);Sheet[] sheets = wb.getSheets();for (int i = 0; i < sheets.length; ++i) {Sheet sheet = sheets[i];for (int j = 0; j < sheet.getRows(); ++j) {Cell[] cells = sheet.getRow(j);if ((cells != null) && (cells.length > 0)) {String[] dataCells = new String[cells.length];for (int k = 0; k < cells.length; ++k) {dataCells[k] = cells[k].getContents();}lt.add(dataCells);}}}} catch (BiffException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (wb != null) {wb.close();}if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}return lt;}public static List<String[]> readExcelByFile(File file) {List lt = new ArrayList();Workbook wb = null;try {wb = Workbook.getWorkbook(file);Sheet[] sheets = wb.getSheets();for (int i = 0; i < sheets.length; ++i) {Sheet sheet = sheets[i];for (int j = 0; j < sheet.getRows(); ++j) {Cell[] cells = sheet.getRow(j);if ((cells != null) && (cells.length > 0)) {String[] dataCells = new String[cells.length];for (int k = 0; k < cells.length; ++k) {dataCells[k] = cells[k].getContents();}lt.add(dataCells);}}}} catch (BiffException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (wb != null) {wb.close();}}return lt;}public static List<String[]> readExcelOneSheet(File file) {return readExcelOneSheet(FileUtil.getInputStream(file));}public static List<String[]> readExcelOneSheet(InputStream in) {List lt = new ArrayList();Workbook wb = null;try {wb = Workbook.getWorkbook(in);Sheet[] sheets = wb.getSheets();Sheet sheet = sheets[0];for (int j = 0; j < sheet.getRows(); ++j) {Cell[] cells = sheet.getRow(j);if ((cells != null) && (cells.length > 0)) {String[] dataCells = new String[cells.length];for (int k = 0; k < cells.length; ++k) {dataCells[k] = cells[k].getContents();}lt.add(dataCells);}}} catch (BiffException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (wb != null) {wb.close();}if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}return lt;}}

 

Note: jxl does not support excel2007

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.