博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java生成excel并可以导出
阅读量:6812 次
发布时间:2019-06-26

本文共 5307 字,大约阅读时间需要 17 分钟。

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);

 }

}

转载地址:http://aswzl.baihongyu.com/

你可能感兴趣的文章
中国互联网创业,最好的城市是哪里?
查看>>
xml.modify() 实例演示
查看>>
端口被占用了,使用netstat找到占用端口的进程
查看>>
我的vim colorscheme - 白色之夜 - 博客园
查看>>
ECSHOP 商品页详情页 添加同类随机商品
查看>>
Select函数
查看>>
【译】UNIVERSAL IMAGE LOADER. PART 3---ImageLoader详解
查看>>
再探迭代器(插入迭代器、流迭代器、反向迭代器、移动迭代器)
查看>>
hdu1181 (变形课)简单地dfs
查看>>
75. Sort Colors
查看>>
WorldWind源码剖析系列:视景体类Frustum
查看>>
(转)完整java开发中JDBC连接数据库代码和步骤
查看>>
Redis Lua脚本原理
查看>>
有时间测试dism
查看>>
/Users/alamps/AndroidStudioProjects/Demo10ScrollView
查看>>
【Swift】iOS UICollectionView 计算 Cell 大小的陷阱
查看>>
为什么我刚发表的文章变成了“待审核”,csdn有没有官方解释啊
查看>>
Matplotlib 工具包 使用教程索引
查看>>
封装bt轮播图淡入淡出效果样式
查看>>
2016第29周三
查看>>