读取excel到list中

package demo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
 * 读取excel将它添加到list中,后续直接将list中的数据添加到数据库中
 * @author gr
 *
 */
public class ExcelExportDemo {
	static int count = 0;
	static String fileName = "text.xls";
	static List<List>alldata=new ArrayList<List>();//所有行的数据存入alldata中

	public static void main(String[] args) throws BiffException, IOException {
		readexcel();
	}

	static void readexcel() throws BiffException, IOException {
		Workbook book = Workbook.getWorkbook(new File(fileName));
		Sheet[] sheets = book.getSheets();
		for (int i = 0; i < sheets.length; i++) {// 进入sheet

			int rows = sheets[i].getRows();
			int cols = sheets[i].getColumns();
			// System.out.println(rows+","+cols);

			for (int row = 0; row < rows; row++) {
				if (row != 0) {// 只记录字段值,不记录字段名
					Cell[] cells = sheets[i].getRow(row);
					List<String>datalist=new ArrayList<String>();//将每一行的数据添加到list中
					for (Cell cell : cells) {
						datalist.add(cell.getContents());
					}
					alldata.add(datalist);
					count++;
				}
			}

		}

		System.out.println(alldata.size());
		book.close();
	}
}

  

猜你喜欢

转载自www.cnblogs.com/imggr/p/8949969.html
今日推荐