---
title: "jxt Excel 导入数据"
author: "rainyear"
date: "2011-12-02T17:47:41Z"
tags: []
categories: ["编程语言"]
slug: "jxt-excel"
draft: false
lastmod: "2011-12-02T17:47:41Z"
id: 1290168
url: "https://rainyear.iteye.com/blog/1290168"
---

package com.xiu.commerce.attributeDict.beans;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import net.sf.jxls.parser.Cell;

public class Test {
	private String filePath;
	private String tableName;// 备用,table's name in SQLServer
	private jxl.Workbook book;
	private jxl.Sheet booksheet;
	private List<String> topic;
	private List<List> lineData;

	public Test() {
		topic = new ArrayList<String>();
		lineData = new ArrayList<List>();
	}

	public void setFilePath(String filepath) {
		this.filePath = filepath;
	}

	public String getFilePath() {
		return this.filePath;
	}

	public void setTableName(String tablename) {
		this.tableName = tablename;
	}

	public String getTableName() {
		return this.tableName;
	}

	public static void main(String args) throws Exception {
		Test test = new Test();
		test.setFilePath("e:\\油画_20111129150826.xls");
		List<String> topString = test.getTopic();
		for (String str : topString) {
			System.out.println("topName>>>>>>>" + str);
		}
		List<String> lineDate = test.getLineData();
		test.closeWorkBook();
	}

	public jxl.Sheet read() {
		try {
			book = Workbook.getWorkbook(new File(filePath));
		} catch (BiffException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		booksheet = book.getSheet(0);
		return booksheet;
	}

	public void closeWorkBook() {
		book.close();
	}

	public List<String> getTopic()// 这个方法读取表格的第一行做为数据库的字段内容
	{
		List<String> topic_ = new ArrayList<String>();
		jxl.Sheet sheet = read();
		// setTableName(sheet.getName());//获取表名称并将其值赋
		if (sheet.getRows() >= 1) {
			for (int i = 0; i < sheet.getColumns(); i++) {
				jxl.Cell cell = sheet.getCell(i, 0);
				topic_.add(cell.getContents());
			}
		}
		return topic_;
	}

	public List<String> getLineData() // 这个方法读取excle文件的第二行以下的数据用于数据库表中的数据内容
	{
		jxl.Sheet sheet = read();
		List<String> lineData_ = new ArrayList<String>();
		System.out.print("row" + sheet.getRows());
		System.out.print("column" + sheet.getColumns());
		if (sheet.getRows() > 1) {
			for (int i = 1; i <= sheet.getRows(); i++) 
			{
				for (int j = 0; j < sheet.getColumns(); j++) {

					jxl.Cell cell = sheet.getCell(j, i);
					System.out.println(cell.getContents());

				}
			}
		}
		return lineData_;
	}
 
}