package action;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.opensymphony.xwork2.ActionSupport;
import entity.Student;
public class ExcleAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 2305566757087929807L;
private List<Student> students;
private String na;
public String getNa() {
return na;
}
public void setNa(String na) {
this.na = na;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String execute() throws Exception {
System.out.println("----------------------------------" + students.size());
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = row.createCell(0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("生日");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
for (int i = 0; i < students.size(); i++) {
row = sheet.createRow((int) i + 1);
Student stu = (Student) students.get(i);
// 第四步,创建单元格,并设置值
row.createCell(0).setCellValue((double) stu.getId());
row.createCell(1).setCellValue(stu.getName());
row.createCell(2).setCellValue((double) stu.getAge());
cell = row.createCell(3);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBirth()));
}
// 第六步,将文件存到指定位置
try {
FileOutputStream fout = new FileOutputStream("D:/students.xls");
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("success.....................");
return SUCCESS;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Java操作Excel.rar (24个子文件)
poi-3.6-20091214.jar 1.47MB
ExcleWeb
.project 1KB
src
action
DataSourceAction.java 1020B
ExcleAction.java 3KB
struts.xml 504B
entity
Student.java 780B
WebRoot
WEB-INF
classes
action
DataSourceAction.class 2KB
ExcleAction.class 4KB
struts.xml 504B
entity
Student.class 1KB
lib
poi-3.6-20091214.jar 1.47MB
web.xml 936B
error.jsp 837B
index.jsp 2KB
success.jsp 840B
META-INF
MANIFEST.MF 39B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 464B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 483B
.jsdtscope 522B
.classpath 1KB
jxl.jar 664KB
共 24 条
- 1
资源评论
Sunny浩文
- 粉丝: 2
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功