jxt Excel 导入数据

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_;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值