InputStream is = new FileInputStream(filepath + "/赛程xlsx");
Workbook wb = null;
try {
wb = WorkbookgetWorkbook(is);
} catch (BiffException e) { // TODO Auto-generated catch block
eprintStackTrace();
}
Sheet sheet = wbgetSheet(0);
Cell cell = null;
int line = sheetgetColumns();
int row = sheetgetRows();
try {
ArrayList<ArrayList<String>> arraylist = new ArrayList<ArrayList<String>>();
for (int r = 1; r < row; r++) {
ArrayList<String> array = new ArrayList<String>();
for (int i = 0; i < line; i++) {
cell = sheetgetCell(i, r);
result = ExparceCell(cell, i);
arrayadd(result);
}
arraylistadd(array);
}
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
这是早年我写的一份,你看看能不能用。
还有你问的第二个问题我不太明白,但是以我的程序为例,两个arrayList,外层arrayList已经一个循环,然后new 新的arrayList直接追加在它的后面。
导入POI的jar包
新建一个项目,在根目录在新建一个lib文件夹,将jar包复制粘贴到lib文件夹后,右键将其添加到项目的build path中,最后的结果如图所示:
2
编写java类,新建一个实体类,比如我们要导出数据库的有关电脑的信息,那么就建一个Computer实体类,代码如下:
package comqiangpoi;
public class Computer {
private int id;
private String name;
private String description;
private double price;
private double credit;
public int getId() {
return id;
}
public Computer(int id, String name, String description, double price,
double credit) {
super();
thisid = id;
thisname = name;
thisdescription = description;
thisprice = price;
thiscredit = credit;
}
public void setId(int id) {
thisid = id;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
thisdescription = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
thisprice = price;
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
thiscredit = credit;
}
}
3
新建一个写入excel的方法,如write2excel,参数可以后面边写边决定(站在一个不熟悉POI的角度)
public static void write2Excel(){}
4
创建 *** 作Excel的HSSFWorkbook对象
HSSFWorkbook excel= new HSSFWorkbook();
创建HSSFSheet对象
Excel中的一个sheet(工作表)对应着java中的一个HSSFSheet对象,利用HSSFWorkbook对象可以创建一个HSSFSheet对象
如:创建一个sheet名为computer的excel
HSSFSheet sheet = excelcreateSheet("computer");
创建第一行标题信息的HSSFRow对象
我们都知道excel是表格,即由一行一行组成的,那么这一行在java类中就是一个HSSFRow对象,我们通过HSSFSheet对象就可以创建HSSFRow对象
如:创建表格中的第一行(我们常用来做标题的行) HSSFRow firstRow = sheetcreateRow(0); 注意下标从0开始
创建标题行中的HSSFCell数组
当然,excel中每一行是由若干个单元格,我们常称为cell,它对应着java中的HSSFCell对象
如:创建5个单元格 HSSFCell cells[] = new HSSFCell[5];
//假设我们一行有五列数据
创建标题数据,并通过HSSFCell对象的setCellValue()方法对每个单元格进行赋值
既然单元格都准备好了,那最后是不是该填充数据了呀。对的,没错。填充数据之前,得把数据准备好吧,
数据:String[] titles = new String[]{"id","name","description","price","credit"};
插入一句话: 在这个时代,能让机器做的,尽量不让人来做,记住这句话。
好的,继续。现在就通过for循环来填充第一行标题的数据
for (int i = 0; i < 5; i++) {
cells[0] = firstRowcreateCell(i);
cells[0]setCellValue(titles[i]);
}
数据分析
第一行标题栏创建完毕后,就准备填充我们要写入的数据吧,在java中,面向对象给我们带来的好处在这里正好体现了,没错
把要填写的数据封装在对象中,即一行就是一个对象,n行就是一个对象列表嘛,好的,走起。
创建对象Computer,私有属性id,name,description,price,credit,以及各属性的setter和getter方法,如步骤二所示。
假设我们要写入excel中的数据从数据库查询出来的,最后就生成了一个List<Computer>对象computers
数据写入
具体数据有了,又该让机器帮我们干活了,向excel中写入数据。
for (int i = 0; i < computerssize(); i++) {
HSSFRow row = sheetcreateRow(i + 1);
Computer computer = computersget(i);
HSSFCell cell = rowcreateCell(0);
cellsetCellValue(computergetId());
cell = rowcreateCell(1);
cellsetCellValue(computergetName());
cell = rowcreateCell(2);
cellsetCellValue(computergetDescription());
cell = rowcreateCell(3);
cellsetCellValue(computergetPrice());
cell = rowcreateCell(4);
cellsetCellValue(computergetCredit());
}
将数据真正的写入excel文件中
做到这里,数据都写好了,最后就是把HSSFWorkbook对象excel写入文件中了。
OutputStream out = null;
try {
out = new FileOutputStream(file);
excelwrite(out);
outclose();
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
Systemoutprintln("数据已经写入excel"); //温馨提示
看看我的main方法吧
public static void main(String[] args) throws IOException {
File file = new File("test1xls");
if(!fileexists()){
filecreateNewFile();
}
List<Computer> computers = new ArrayList<Computer>();
computersadd(new Computer(1,"宏碁","笔记本电脑",3333,90));
computersadd(new Computer(2,"苹果","笔记本电脑,一体机",8888,96));
computersadd(new Computer(3,"联想","笔记本电脑,台式机",4444,93));
computersadd(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,86));
computersadd(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 10, 99));
write2excel(computers, file);
}
工程目录及执行main方法后的test1xls数据展示
源码分享,computer就不贴了
package comqiangpoi;
import javaioFile;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import javaioOutputStream;
import javautilArrayList;
import javautilList;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
public class ReadExcel {
public static void main(String[] args) throws IOException {
File file = new File("test1xls");
if(!fileexists()){
filecreateNewFile();
}
List<Computer> computers = new ArrayList<Computer>();
computersadd(new Computer(1,"宏碁","笔记本电脑",3333,90));
computersadd(new Computer(2,"苹果","笔记本电脑,一体机",8888,96));
computersadd(new Computer(3,"联想","笔记本电脑,台式机",4444,93));
computersadd(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,86));
computersadd(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 10, 99));
write2excel(computers, file);
}
public static void write2excel(List<Computer> computers,File file) {
HSSFWorkbook excel = new HSSFWorkbook();
HSSFSheet sheet = excelcreateSheet("computer");
HSSFRow firstRow = sheetcreateRow(0);
HSSFCell cells[] = new HSSFCell[5];
String[] titles = new String[] { "id", "name", "description", "price",
"credit" };
for (int i = 0; i < 5; i++) {
cells[0] = firstRowcreateCell(i);
cells[0]setCellValue(titles[i]);
}
for (int i = 0; i < computerssize(); i++) {
HSSFRow row = sheetcreateRow(i + 1);
Computer computer = computersget(i);
HSSFCell cell = rowcreateCell(0);
cellsetCellValue(computergetId());
cell = rowcreateCell(1);
cellsetCellValue(computergetName());
cell = rowcreateCell(2);
cellsetCellValue(computergetDescription());
cell = rowcreateCell(3);
cellsetCellValue(computergetPrice());
cell = rowcreateCell(4);
cellsetCellValue(computergetCredit());
}
OutputStream out = null;
try {
out = new FileOutputStream(file);
excelwrite(out);
outclose();
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
}
}
package poi;
import javaioFileInputStream;
import javaioIOException;
import javaioInputStream;
import javautilIterator;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFWorkbook;
import orgapachepoissusermodelCell;
import orgapachepoissusermodelRow;
import orgapachepoissusermodelSheet;
import orgapachepoissusermodelWorkbook;
import orgapachepoixssfusermodelXSSFWorkbook;
public class ReadExcel001 {
public static void main(String[] args) {
readXml("D:/testxlsx");
Systemoutprintln("-------------");
readXml("d:/test2xls");
}
public static void readXml(String fileName){
boolean isE2007 = false; //判断是否是excel2007格式
if(fileNameendsWith("xlsx"))
isE2007 = true;
try {
InputStream input = new FileInputStream(fileName); //建立输入流
Workbook wb = null;
//根据文件格式(2003或者2007)来初始化
if(isE2007)
wb = new XSSFWorkbook(input);
else
wb = new HSSFWorkbook(input);
Sheet sheet = wbgetSheetAt(0); //获得第一个表单
Iterator<Row> rows = sheetrowIterator(); //获得第一个表单的迭代器
while (rowshasNext()) {
Row row = rowsnext(); //获得行数据
Systemoutprintln("Row #" + rowgetRowNum()); //获得行号从0开始
Iterator<Cell> cells = rowcellIterator(); //获得第一行的迭代器
while (cellshasNext()) {
Cell cell = cellsnext();
Systemoutprintln("Cell #" + cellgetColumnIndex());
switch (cellgetCellType()) { //根据cell中的类型来输出数据
case HSSFCellCELL_TYPE_NUMERIC:
Systemoutprintln(cellgetNumericCellValue());
break;
case HSSFCellCELL_TYPE_STRING:
Systemoutprintln(cellgetStringCellValue());
break;
case HSSFCellCELL_TYPE_BOOLEAN:
Systemoutprintln(cellgetBooleanCellValue());
break;
case HSSFCellCELL_TYPE_FORMULA:
Systemoutprintln(cellgetCellFormula());
break;
default:
Systemoutprintln("unsuported sell type");
break;
}
}
}
} catch (IOException ex) {
exprintStackTrace();
}
}
}
参考自>
你是要修改POI 源代码呢?还是想利用现成的POI jar包呢?
>
以上就是关于求问java中poi导入execl表到数据库,表的结构是多行多SHEET,且每个sheet结构不同,请问怎么实现。全部的内容,包括:求问java中poi导入execl表到数据库,表的结构是多行多SHEET,且每个sheet结构不同,请问怎么实现。、如何使用POI对Excel表进行导入和导出、java poi怎么导入excel数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)