使用poi实现生成excel文件

import java.util.ArrayList;

import javax.servlet.ServletOutputStream;

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 org.apache.poi.ss.usermodel.HorizontalAlignment;

import com.stu.student;

public class ExcleImpl {
	public void export(String[] titles, ServletOutputStream out) throws Exception{
	    try{
	                     // 第一步,创建一个workbook,对应一个Excel文件
	                     HSSFWorkbook workbook = new HSSFWorkbook();
	                     
	                     
	                     // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
	                     HSSFSheet hssfSheet = workbook.createSheet("sheet1");
	                     
	                     // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
	                     
	                     HSSFRow row = hssfSheet.createRow(0);
	                    // 第四步,创建单元格,并设置值表头 设置表头居中
	                     HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
	                     

	         
	                     HSSFCell hssfCell = null;
	                     for (int i = 0; i < titles.length; i++) {
	                         hssfCell = row.createCell(i);//列索引从0开始
	                         hssfCell.setCellValue(titles[i]);//列名1
	                         hssfCell.setCellStyle(hssfCellStyle);//列居中显示                
	                     }
	                     
	                     // 第五步,写入实体数据 
	                      student  person1=new student("1","张三","123", null, null, null, null, null, null, null, null);
	                      student  person2=new student("2","李四","123", null, null, null, null, null, null, null, null);
	                      student  person3=new student("3","王五","123", null, null, null, null, null, null, null, null);
	                      student  person4=new student("4","徐小筱","123", null, null, null, null, null, null, null, null);
	                      
	                      //这里我把list当做数据库啦
	                      ArrayList<student>  list=new ArrayList<student>();
	                      list.add(person1);
	                      list.add(person2);
	                      list.add(person3);
	                      list.add(person4);
	                     
	                         for (int i = 0; i < list.size(); i++) {
	                             row = hssfSheet.createRow(i+1);                
	                             student person = list.get(i);
	                             
	                             // 第六步,创建单元格,并设置值
	                             String  id = null;
	                             if(person.getId() != null){
	                                     id = person.getId();
	                             }
	                            row.createCell(0).setCellValue(id);
	                             String name = "";
	                             if(person.getUsername() != null){
	                                 name = person.getUsername();
	                             }
	                            row.createCell(1).setCellValue(name);
	                             String password = "";
	                             if(person.getPassword() != null){
	                                 password = person.getPassword();
	                             }
	                             row.createCell(2).setCellValue(password);
	                         
	                           
	                          
	                         }
	    
	                     // 第七步,将文件输出到客户端浏览器
	                     try {
	                         workbook.write(out);
	                         out.flush();
	                        out.close();
	         
	                     } catch (Exception e) {
	                         e.printStackTrace();
	                     }
	                 }catch(Exception e){
	                     e.printStackTrace();
	                    throw new Exception("导出信息失败!");
	                    
	                    }
	                 }   
}

  

原文地址:https://www.cnblogs.com/qinyios/p/11064116.html