poi生成Excel分Excel2003和Excel2007(Excel2010)

以下測試可行:jar包 poi-3.7


//實體類

package com.tuyao.domain;
import java.util.Date;  
  
/** 
 * @author Geloin 
 *  
 */  
public class Person {  
  
    /** 
     * 姓名 
     */  
    private String name;  
  
    /** 
     * 年齡 
     */  
    private int age;  
  
    /** 
     * 生日 
     */  
    private Date birthday;  
  
    /** 
     * 是否學生 
     */  
    private boolean isStudent;  
  
    /** 
     * 身高 
     */  
    private double height;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int i) {  
        this.age = i;  
    }  
  
    public Date getBirthday() {  
        return birthday;  
    }  
  
    public void setBirthday(Date birthday) {  
        this.birthday = birthday;  
    }  
  
    public boolean isStudent() {  
        return isStudent;  
    }  
  
    public void setStudent(boolean isStudent) {  
        this.isStudent = isStudent;  
    }  
  
    public double getHeight() {  
        return height;  
    }  
  
    public void setHeight(double height) {  
        this.height = height;  
    }  
  
}  



//poi處理類

package com.poi.excel.handle;  
  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.OutputStream;  
import java.text.SimpleDateFormat;  
import java.util.ArrayList;  
import java.util.List;  
  
import org.apache.poi.hssf.usermodel.HSSFCell;  
import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
import org.apache.poi.hssf.usermodel.HSSFFont;  
import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
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.util.CellRangeAddress;  
  
import com.tuyao.domain.Person;  
  
/** 
 * @author Geloin 
 *  
 */  
public class PoiTest {  
  
    /** 
     *  
     * @param args 
     * @throws Exception 
     */  
    public static void main(String[] args) throws Exception {  
  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  
        List<Person> data = new ArrayList<Person>();  
        Person person1 = new Person();  
        person1.setName("張三");  
        person1.setAge(20);  
        person1.setBirthday(format.parse("1989-11-12"));  
        person1.setStudent(true);  
        person1.setHeight(168.8);  
        data.add(person1);  
        Person person2 = new Person();  
        person2.setName("李四");  
        person2.setAge(21);  
        person2.setBirthday(format.parse("1988-11-12"));  
        person2.setStudent(false);  
        person2.setHeight(169.8);  
        data.add(person2);  
  
        String exportPath = "d:/export.xls";  
        OutputStream out = new FileOutputStream(new File(exportPath));  
  
        // 聲明一個工作薄  
        HSSFWorkbook workbook = new HSSFWorkbook();  
        // 生成一個表格  
        HSSFSheet sheet = workbook.createSheet("sheet的名稱");  
        // 設置表格默認列寬度爲15個字節  
        sheet.setDefaultColumnWidth(15);  
  
        // 設置標題  
        HSSFCellStyle titleStyle = workbook.createCellStyle();  
        // 居中顯示  
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
        // 標題字體  
        HSSFFont titleFont = workbook.createFont();  
        // 字體大小  
        titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
        titleStyle.setFont(titleFont);  
  
        HSSFCellStyle contentStyle = workbook.createCellStyle();  
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
        HSSFFont contentFont = workbook.createFont();  
        contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
        contentStyle.setFont(contentFont);  
  
        // 產生表格標題行  
        HSSFRow row = sheet.createRow(0);  
        String[] headers = new String[] { "序號", "姓名", "年齡", "出生年月", "是否學生",  
                "身高" };  
        for (int i = 0; i < headers.length; i++) {  
            HSSFCell cell = row.createCell(i);  
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);  
            cell.setCellValue(text);  
            cell.setCellStyle(titleStyle);  
        }  
  
        int rowCount = 1;  
        for (int i = 0; i < data.size(); i++, rowCount++) {  
            HSSFRow dataRow = sheet.createRow(rowCount);  
            Person person = data.get(i);  
  
            // 序號  
            HSSFCell cell0 = dataRow.createCell(0);  
            cell0.setCellValue((i + 1));  
            cell0.setCellStyle(contentStyle);  
  
            // 姓名  
            HSSFCell cell1 = dataRow.createCell(1);  
            cell1.setCellValue(person.getName());  
            cell1.setCellStyle(contentStyle);  
  
            // 年齡,轉化爲String後放到cell裏面  
            HSSFCell cell2 = dataRow.createCell(2);  
            cell2.setCellValue(person.getAge());  
            cell2.setCellStyle(contentStyle);  
  
            // 出生年月,轉化爲String後放到cell裏面  
            HSSFCell cell3 = dataRow.createCell(3);  
            cell3.setCellValue(format.format(person.getBirthday()));  
            cell3.setCellStyle(contentStyle);  
  
            // 是否學生,轉化爲String後放到cell裏面  
            HSSFCell cell4 = dataRow.createCell(4);  
            String isStudent = person.isStudent() ? "是" : "否";  
            cell4.setCellValue(isStudent);  
            cell4.setCellStyle(contentStyle);  
  
            // 身高,轉化爲String後放到cell裏面  
            HSSFCell cell5 = dataRow.createCell(5);  
            cell5.setCellValue(String.valueOf(person.getHeight()));  
            cell5.setCellStyle(contentStyle);  
        }  
  
        // 合併,從第一行到最後一行,從第七列到第七列  
        sheet.addMergedRegion(new CellRangeAddress(0, rowCount - 1, 6, 6));  
        // 合併單元格的內容,合併單元格後,僅會保留第一行,第七列的內容,所以設置第一行第七列的內容  
        HSSFCell cell6 = row.createCell(6);  
        cell6.setCellStyle(contentStyle);  
        cell6.setCellValue("合併單元格的內容");  
  
        workbook.write(out);  
    }  
}  


發佈了38 篇原創文章 · 獲贊 10 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章