使用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("導出信息失敗!");
	                    
	                    }
	                 }   
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM