java生成excel并可以导出

package com.census.service.daoImpl;

import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.Resource;

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;
import org.springframework.stereotype.Component;

import com.census.bean.Residence;
import com.census.bean.Student;
import com.census.dao.StudentDao;
import com.census.service.dao.StudentServiceDao;

@Component
public class StudentServiceDaoImpl implements StudentServiceDao {

 //这里需要引入poi-2.5.1.jar
 public InputStream getInputStream(String stno)
 {
    //生成HSSFWorkbook对象,他就像excel文件本身
  HSSFWorkbook wb = new HSSFWorkbook();
  //创建excel表格的一个sheet(默认三个),存放表数据
  HSSFSheet sheet = wb.createSheet("sheet1");
     //创建一个行,每一行和每个单元都是从0开始
  HSSFRow row = sheet.createRow(0);
        //创建单元格(第一行为表头)
  HSSFCell cell = row.createCell((short) 0);
  //设定字符编码ENCODING_UTF_16(每个都要设)
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  //设置单元格存放内容
  cell.setCellValue("学号");

  cell = row.createCell((short) 1);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("姓名");


  cell = row.createCell((short) 2);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("性别");

  cell = row.createCell((short) 3);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("院系");
  
  cell = row.createCell((short) 4);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("班级");
  
  cell = row.createCell((short) 5);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("专业");
  
  cell = row.createCell((short) 6);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("入学时间");
  
  cell = row.createCell((short) 7);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("身份证号码");
  
  cell = row.createCell((short) 8);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("民族");
  
  cell = row.createCell((short) 9);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("出生日期");
  
  cell = row.createCell((short) 10);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("政治面貌");
  
  cell = row.createCell((short) 11);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("籍贯");
  
  cell = row.createCell((short) 12);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("文化程度");
  
  cell = row.createCell((short) 13);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue("户口类别");
  
  //以上创建的是表头,下面创建的是表内容,[循环]插入数据库数据
  //[如果多条记录循环每次循环写入一行数据]
   
  Student st=getStudentByStudentNo(stno);
  Residence residence=st.getResidence();
  
   /***************************************************************/
  
   //创建一个行,每一行和每个单元都是从0开始
   HSSFRow row2 = sheet.createRow(1);
     
  HSSFCell cell2= row2.createCell((short) 0);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(st.getStudentNo());

  cell2 = row2.createCell((short) 1);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(st.getStudentName());


  cell2 = row2.createCell((short) 2);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(st.getStudentSex());

  cell2 = row2.createCell((short) 3);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(st.getDepartMent());
  
  cell2 = row2.createCell((short) 4);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(st.getStudentClass());
  
  cell2= row2.createCell((short) 5);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(st.getSpecialty());
  
  cell2 = row2.createCell((short) 6);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(st.getEntertag().toLocaleString());
  
  cell2 = row2.createCell((short) 7);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(residence.getIdNo());
  
  cell2 = row2.createCell((short) 8);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(residence.getNation());
  
  cell2 = row2.createCell((short) 9);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(residence.getBirthDate().toLocaleString());
  
  cell2 = row2.createCell((short) 10);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(residence.getPolitics());
  
  cell2 = row2.createCell((short) 11);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(residence.getNativePlace());
  
  cell2 = row2.createCell((short) 12);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(residence.getEducation());
  
  cell2 = row2.createCell((short) 13);
  cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell2.setCellValue(residence.getResidencetype());
  
  

  ByteArrayOutputStream os = new ByteArrayOutputStream();
  
  try
  {
   wb.write(os);
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }
  
  byte[] content = os.toByteArray();
  
  InputStream is = new ByteArrayInputStream(content);
  
  return is;
  
  
  
 }

 public Student getStudentByIdNo(String idNo) {
  
  return studentDao.getStudentByIdNo(idNo);
 }

 @Resource
 StudentDao studentDao;

 public void deleteStudent(Student student) {

  studentDao.deleteStudent(student);
 }

 public void insertSudent(Student student) {
  studentDao.insertSudent(student);

 }

 public Student getStudentByStudentNo(String id) {

  return studentDao.getStudentByStudentNo(id);
 }

 public void updateStudent(Student student) {
  studentDao.updateStudent(student);

 }

}

posted @ 2013-06-14 19:12  嗨,你的益达~~~  阅读(551)  评论(0编辑  收藏  举报