乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > java使用<em>poi</em>导出<em>excel</em>表格

java使用<em>poi</em>导出<em>excel</em>表格

作者:乔山办公网日期:

返回目录:excel表格制作


项目结构:

xls:

\\\

XlsMain .java 类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)

*

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
*
* @author Hongten</br>
*
* 参考地址:http://hao0610.iteye.com/blog/1160678
*
*/
public class XlsMain {

public static void main(String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();
XlsDto xls = null;
List<XlsDto> list = xlsMain.readXls();

try {
XlsDto2Excel.xlsDto2Excel(list);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
xls = (XlsDto) list.get(i);
System.out.println(xls.getXh() + " " + xls.getXm() + " "
+ xls.getYxsmc() + " " + xls.getKcm() + " "
+ xls.getCj());
}

}

/**
* 读取xls文件内容
*
* @return List<XlsDto>对象
* @throws IOException
* 输入/输出(i/o)异常
*/
private List<XlsDto> readXls() throws IOException {
InputStream is = new FileInputStream("pldrxkxxmb.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
XlsDto xlsDto = null;
List<XlsDto> list = new ArrayList<XlsDto>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
xlsDto = new XlsDto();
// 循环列Cell
// 0学号 1姓名 2学院 3课程名 4 成绩
// for (int cellNum = 0; cellNum <=4; cellNum++) {
HSSFCell xh = hssfRow.getCell(0);
if (xh == null) {
continue;
}
xlsDto.setXh(getValue(xh));
HSSFCell xm = hssfRow.getCell(1);
if (xm == null) {
continue;
}
xlsDto.setXm(getValue(xm));
HSSFCell yxsmc = hssfRow.getCell(2);
if (yxsmc == null) {
continue;
}
xlsDto.setYxsmc(getValue(yxsmc));
HSSFCell kcm = hssfRow.getCell(3);
if (kcm == null) {
continue;
}
xlsDto.setKcm(getValue(kcm));
HSSFCell cj = hssfRow.getCell(4);
if (cj == null) {
continue;
}
xlsDto.setCj(Float.parseFloat(getValue(cj)));
list.add(xlsDto);
}
}
return list;
}

/**
* 得到Excel表中的值
*
* @param hssfCell
* Excel中的每一个格子
* @return Excel中每一个格子中的值
*/
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}

}

XlsDto2Excel.java类
//该类主要负责向Excel(2003版)中插入数据

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class XlsDto2Excel {

/**
*
* @param xls
* XlsDto实体类的一个对象
* @throws Exception
* 在导入Excel的过程中抛出异常
*/
public static void xlsDto2Excel(List<XlsDto> xls) throws Exception {
// 获取总列数
int CountColumnNum = xls.size();
// 创建Excel文档
HSSFWorkbook hwb = new HSSFWorkbook();
XlsDto xlsDto = null;
// sheet 对应一个工作页
HSSFSheet sheet = hwb.createSheet("pldrxkxxmb");
HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "学号";
names[1] = "姓名";
names[2] = "学院";
names[3] = "课程名";
names[4] = "成绩";
for (int j = 0; j < CountColumnNum; j++) {
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
}
for (int i = 0; i < xls.size(); i++) {
// 创建一行
HSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一条记录
xlsDto = xls.get(i);
for (int colu = 0; colu <= 4; colu++) {
// 在一行内循环
HSSFCell xh = row.createCell(0);
xh.setCellValue(xlsDto.getXh());
HSSFCell xm = row.createCell(1);
xm.setCellValue(xlsDto.getXm());
HSSFCell yxsmc = row.createCell(2);
yxsmc.setCellValue(xlsDto.getYxsmc());
HSSFCell kcm = row.createCell(3);
kcm.setCellValue(xlsDto.getKcm());
HSSFCell cj = row.createCell(4);
cj.setCellValue(xlsDto.getCj());
(xlsDto.getMessage());
}
}
// 创建文件输出流,准备输出电子表格
OutputStream out = new FileOutputStream("POI2Excel/pldrxkxxmb.xls");
hwb.write(out);
out.close();
System.out.println("数据库导出7a686964616fe59b9ee7ad94331成功");
}

}

XlsDto .java类
//该类是一个实体类

public class XlsDto {
/**
* 选课号
*/
private Integer xkh;
/**
* 学号
*/
private String xh;
/**
* 姓名
*/
private String xm;
/**
* 学院
*/
private String yxsmc;
/**
* 课程号
*/
private Integer kch;
/**
* 课程名
*/
private String kcm;
/**
* 成绩
*/
private float cj;
public Integer getXkh() {
return xkh;
}
public void setXkh(Integer xkh) {
this.xkh = xkh;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getYxsmc() {
return yxsmc;
}
public void setYxsmc(String yxsmc) {
this.yxsmc = yxsmc;
}
public Integer getKch() {
return kch;
}
public void setKch(Integer kch) {
this.kch = kch;
}
public String getKcm() {
return kcm;
}
public void setKcm(String kcm) {
this.kcm = kcm;
}
public float getCj() {
return cj;
}
public void setCj(float cj) {
this.cj = cj;
}

}

后台输出:
数据库导出成功
1.0 hongten 信息技术学院 计算机网络应用基础 80.0
2.0 王五 信息技术学院 计算机网络应用基础 81.0
3.0 李胜基 信息技术学院 计算机网络应用基础 82.0
4.0 五班古 信息技术学院 计算机网络应用基础 83.0
5.0 蔡诗芸 信息技术学院 计算机网络应用基础 84.0

让用户指定导出位置这个貌似行不通,这个要根据浏览器的来定了,ie6可以让你自己选择保存的路径,但是对于chorome 和火狐而言他们有自己默认文件保存路径
poi导入一个excel文件,本地运行是可以导入成copy功的,但是部署到测试环境就在报错。然后本地是windows系统,测试是Linux系统。前台用的是“ajaxFileUpload”插件上传,因为要实现的是上传成功之后,在页面上给一个弹框,提示上传成功。

导入POI的jar包
新建一个项目,在根目录在新建一个lib文件夹,将jar包复制粘贴到lib文件夹后,右键将其添加到项目的build path中,最后的结果如图所示:

2
编写java类,新建一个实体类,比如我们要导出数据库的有关电脑的信息,那么就建一个Computer实体类,代码如下:
package com.qiang.poi;
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();
this.id = id;
this.name = name;
this.description = description;
this.price = price;
this.credit = credit;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = 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 = excel.createSheet("computer");
创建第一行标题信息的HSSFRow对象
我们都知道excel是表格,即由一行一行组成的,那么这一行在java类中就是一个HSSFRow对象,我们通过HSSFSheet对象就可以创建HSSFRow对象
如:创建表格中的第一行(我们常用来做标题的行) HSSFRow firstRow = sheet.createRow(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] = firstRow.createCell(i);
cells[0].setCellValue(titles[i]);
}
数据分析
第一行标题栏创建完毕后,就准备填充我们要写e5a48de588b6e799bee5baa6e997aee7ad94335入的数据吧,在java中,面向对象给我们带来的好处在这里正好体现了,没错
把要填写的数据封装在对象中,即一行就是一个对象,n行就是一个对象列表嘛,好的,走起。
创建对象Computer,私有属性id,name,description,price,credit,以及各属性的setter和getter方法,如步骤二所示。
假设我们要写入excel中的数据从数据库查询出来的,最后就生成了一个List<Computer>对象computers
数据写入
具体数据有了,又该让机器帮我们干活了,向excel中写入数据。
for (int i = 0; i < computers.size(); i++) {
HSSFRow row = sheet.createRow(i + 1);
Computer computer = computers.get(i);
HSSFCell cell = row.createCell(0);
cell.setCellValue(computer.getId());
cell = row.createCell(1);
cell.setCellValue(computer.getName());
cell = row.createCell(2);
cell.setCellValue(computer.getDescription());
cell = row.createCell(3);
cell.setCellValue(computer.getPrice());
cell = row.createCell(4);
cell.setCellValue(computer.getCredit());
}
将数据真正的写入excel文件中
做到这里,数据都写好了,最后就是把HSSFWorkbook对象excel写入文件中了。
OutputStream out = null;
try {
out = new FileOutputStream(file);
excel.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("数据已经写入excel"); //温馨提示
看看我的main方法吧
public static void main(String[] args) throws IOException {
File file = new File("test1.xls");
if(!file.exists()){
file.createNewFile();
}
List<Computer> computers = new ArrayList<Computer>();
computers.add(new Computer(1,"宏碁","笔记本电脑",3333,9.0));
computers.add(new Computer(2,"苹果","笔记本电脑,一体机",8888,9.6));
computers.add(new Computer(3,"联想","笔记本电脑,台式机",4444,9.3));
computers.add(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,8.6));
computers.add(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 1.0, 9.9));
write2excel(computers, file);
}
工程目录及执行main方法后的test1.xls数据展示

源码分享,computer就不贴了
package com.qiang.poi;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ReadExcel {
public static void main(String[] args) throws IOException {
File file = new File("test1.xls");
if(!file.exists()){
file.createNewFile();
}
List<Computer> computers = new ArrayList<Computer>();
computers.add(new Computer(1,"宏碁","笔记本电脑",3333,9.0));
computers.add(new Computer(2,"苹果","笔记本电脑,一体机",8888,9.6));
computers.add(new Computer(3,"联想","笔记本电脑,台式机",4444,9.3));
computers.add(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,8.6));
computers.add(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 1.0, 9.9));
write2excel(computers, file);
}

public static void write2excel(List<Computer> computers,File file) {
HSSFWorkbook excel = new HSSFWorkbook();
HSSFSheet sheet = excel.createSheet("computer");
HSSFRow firstRow = sheet.createRow(0);
HSSFCell cells[] = new HSSFCell[5];
String[] titles = new String[] { "id", "name", "description", "price",
"credit" };
for (int i = 0; i < 5; i++) {
cells[0] = firstRow.createCell(i);
cells[0].setCellValue(titles[i]);
}
for (int i = 0; i < computers.size(); i++) {
HSSFRow row = sheet.createRow(i + 1);
Computer computer = computers.get(i);
HSSFCell cell = row.createCell(0);
cell.setCellValue(computer.getId());
cell = row.createCell(1);
cell.setCellValue(computer.getName());
cell = row.createCell(2);
cell.setCellValue(computer.getDescription());
cell = row.createCell(3);
cell.setCellValue(computer.getPrice());
cell = row.createCell(4);
cell.setCellValue(computer.getCredit());
}
OutputStream out = null;
try {
out = new FileOutputStream(file);
excel.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

相关阅读

关键词不能为空
极力推荐
  • Excel公式——取整那些事儿-excel取整函数

  • excel取整函数,这个世界上有种很隐蔽的错误,叫做你看着它是11.1,实际它却是11。11在使用Excel的过程中,有时是为了整齐,有时是为了好看,很多人都有习惯设置一下数字格式,并且只

ppt怎么做_excel表格制作_office365_word文档_365办公网