• 大小: 22.89MB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2023-07-15
  • 语言: 数据库
  • 标签: poi  excel  

资源简介

ssh2整合poi导入导出excel实例,将工程导入myeclipce就可以运行,数据库采用mysql,源码具体使用见工程里面的文档,界面美观,一个很不错的实例,分享给大家

资源截图

代码片段和文件信息

package com.demo.cyd.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Cellstyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.demo.cyd.java.UploadFile;
import com.demo.cyd.model.Student;
import com.demo.cyd.service.StudentService;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings(“serial“)
@Scope(“prototype“)
@Component
public class StudentAction extends ActionSupport {

private InputStream excelFile;
private File uploadFile;
private String uploadFileFileName;
private StudentService studentService;

// 进入页面查询数据
public String listAll() {
HttpServletRequest request = ServletActionContext.getRequest();
List sList = studentService.findAll();
request.setAttribute(“list“ sList);
return “listAll“;
}

// 导出Excel
public String ExcelExport() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String ids = request.getParameter(“ids“);
List list = new ArrayList();
String[] array = ids.split(““);
int[] id = new int[array.length];
for (int i = 0; i < id.length; i++) {
Student student = studentService
.findById(Integer.valueOf(array[i]));
list.add(student);
}
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet(“学生信息“);
Row row = sheet.createRow(0);
row.createCell(0).setCellValue(“学号“);
row.createCell(1).setCellValue(“姓名“);
row.createCell(2).setCellValue(“年龄“);
row.createCell(3).setCellValue(“性别“);
row.createCell(4).setCellValue(“地址“);
Cellstyle cellstyle = workbook.createCellstyle();
cellstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(“m/d/yy“));
for (int i = 1; i <= list.size(); i++) {
Student stu = list.get(i - 1);
row = sheet.createRow(i);
row.createCell(0).setCellValue(stu.getS_id());
row.createCell(1).setCellValue(stu.getS_name());
row.createCell(2).setCellValue(stu.getS_age());
row.createCell(3).setCellValue(stu.getS_sex());
row.createCell(4).setCellValue(stu.getS_address());
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
excelFile = new ByteArrayInputStream(baos.toByteArray());
baos.close();
return “excel“;
}

// 导入Excel
public String ExcelInto() throws Exception {
String di

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-10-24 14:54  Poi导出excel\
     文件        5990  2012-12-13 22:27  Poi导出excel\.classpath
     目录           0  2012-12-13 22:27  Poi导出excel\.myeclipse\
     文件         315  2012-12-13 22:27  Poi导出excel\.mymetadata
     文件        1420  2012-12-13 22:27  Poi导出excel\.project
     目录           0  2012-12-13 22:27  Poi导出excel\.settings\
     文件         500  2012-12-13 22:27  Poi导出excel\.settings\.jsdtscope
     文件         330  2012-12-13 22:27  Poi导出excel\.settings\org.eclipse.jdt.core.prefs
     文件          49  2012-12-13 22:27  Poi导出excel\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2012-12-13 22:27  Poi导出excel\.settings\org.eclipse.wst.jsdt.ui.superType.name
     目录           0  2012-12-13 22:27  Poi导出excel\src\
     文件        3266  2012-12-13 22:27  Poi导出excel\src\beans.xml
     目录           0  2012-12-13 22:27  Poi导出excel\src\com\
     目录           0  2012-12-13 22:27  Poi导出excel\src\com\demo\
     目录           0  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\
     目录           0  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\action\
     文件        5266  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\action\StudentAction.java
     目录           0  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\dao\
     目录           0  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\dao\impl\
     文件        1140  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\dao\impl\StudentDaoImpl.java
     文件         240  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\dao\StudentDao.java
     目录           0  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\java\
     文件         491  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\java\UploadFile.java
     目录           0  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\model\
     文件        1079  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\model\Student.java
     目录           0  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\service\
     文件         738  2012-12-13 22:27  Poi导出excel\src\com\demo\cyd\service\StudentService.java
     文件        1543  2012-12-13 22:27  Poi导出excel\src\log4j.properties
     文件        1222  2012-12-13 22:27  Poi导出excel\src\struts.xml
     目录           0  2012-12-13 22:27  Poi导出excel\WebRoot\
     目录           0  2012-12-13 22:27  Poi导出excel\WebRoot\css\
............此处省略130个文件信息

评论

共有 条评论