poi生成Excel

 需要用到的jar包:poi-3.9-20121203.jar。

        源代码示例:

[java]  view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.geloin.poi.bean;  
  5.   
  6. import java.util.Date;  
  7.   
  8. /** 
  9.  * @author Geloin 
  10.  *  
  11.  */  
  12. public class Person {  
  13.   
  14.     /** 
  15.      * 姓名 
  16.      */  
  17.     private String name;  
  18.   
  19.     /** 
  20.      * 年龄 
  21.      */  
  22.     private Integer age;  
  23.   
  24.     /** 
  25.      * 生日 
  26.      */  
  27.     private Date birthday;  
  28.   
  29.     /** 
  30.      * 是否学生 
  31.      */  
  32.     private boolean isStudent;  
  33.   
  34.     /** 
  35.      * 身高 
  36.      */  
  37.     private double height;  
  38.   
  39.     public String getName() {  
  40.         return name;  
  41.     }  
  42.   
  43.     public void setName(String name) {  
  44.         this.name = name;  
  45.     }  
  46.   
  47.     public Integer getAge() {  
  48.         return age;  
  49.     }  
  50.   
  51.     public void setAge(Integer age) {  
  52.         this.age = age;  
  53.     }  
  54.   
  55.     public Date getBirthday() {  
  56.         return birthday;  
  57.     }  
  58.   
  59.     public void setBirthday(Date birthday) {  
  60.         this.birthday = birthday;  
  61.     }  
  62.   
  63.     public boolean isStudent() {  
  64.         return isStudent;  
  65.     }  
  66.   
  67.     public void setStudent(boolean isStudent) {  
  68.         this.isStudent = isStudent;  
  69.     }  
  70.   
  71.     public double getHeight() {  
  72.         return height;  
  73.     }  
  74.   
  75.     public void setHeight(double height) {  
  76.         this.height = height;  
  77.     }  
  78.   
  79. }  

[java]  view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.geloin.poi.main;  
  5.   
  6. import java.io.File;  
  7. import java.io.FileOutputStream;  
  8. import java.io.OutputStream;  
  9. import java.text.SimpleDateFormat;  
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12.   
  13. import org.apache.poi.hssf.usermodel.HSSFCell;  
  14. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  15. import org.apache.poi.hssf.usermodel.HSSFFont;  
  16. import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
  17. import org.apache.poi.hssf.usermodel.HSSFRow;  
  18. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  20. import org.apache.poi.ss.util.CellRangeAddress;  
  21.   
  22. import com.geloin.poi.bean.Person;  
  23.   
  24. /** 
  25.  * @author Geloin 
  26.  *  
  27.  */  
  28. public class PoiTest {  
  29.   
  30.     /** 
  31.      *  
  32.      * @param args 
  33.      * @throws Exception 
  34.      */  
  35.     public static void main(String[] args) throws Exception {  
  36.   
  37.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  38.   
  39.         List<Person> data = new ArrayList<Person>();  
  40.         Person person1 = new Person();  
  41.         person1.setName("张三");  
  42.         person1.setAge(20);  
  43.         person1.setBirthday(format.parse("1989-11-12"));  
  44.         person1.setStudent(true);  
  45.         person1.setHeight(168.8);  
  46.         data.add(person1);  
  47.         Person person2 = new Person();  
  48.         person2.setName("李四");  
  49.         person2.setAge(21);  
  50.         person2.setBirthday(format.parse("1988-11-12"));  
  51.         person2.setStudent(false);  
  52.         person2.setHeight(169.8);  
  53.         data.add(person2);  
  54.   
  55.         String exportPath = "d:/work/proTmp/geloin/poi/export.xls";  
  56.         OutputStream out = new FileOutputStream(new File(exportPath));  
  57.   
  58.         // 声明一个工作薄  
  59.         HSSFWorkbook workbook = new HSSFWorkbook();  
  60.         // 生成一个表格  
  61.         HSSFSheet sheet = workbook.createSheet("sheet的名称");  
  62.         // 设置表格默认列宽度为15个字节  
  63.         sheet.setDefaultColumnWidth(15);  
  64.   
  65.         // 设置标题  
  66.         HSSFCellStyle titleStyle = workbook.createCellStyle();  
  67.         // 居中显示  
  68.         titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  69.         // 标题字体  
  70.         HSSFFont titleFont = workbook.createFont();  
  71.         // 字体大小  
  72.         titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
  73.         titleStyle.setFont(titleFont);  
  74.   
  75.         HSSFCellStyle contentStyle = workbook.createCellStyle();  
  76.         contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  77.         HSSFFont contentFont = workbook.createFont();  
  78.         contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
  79.         contentStyle.setFont(contentFont);  
  80.   
  81.         // 产生表格标题行  
  82.         HSSFRow row = sheet.createRow(0);  
  83.         String[] headers = new String[] { "序号""姓名""年龄""出生年月""是否学生",  
  84.                 "身高" };  
  85.         for (int i = 0; i < headers.length; i++) {  
  86.             HSSFCell cell = row.createCell(i);  
  87.             HSSFRichTextString text = new HSSFRichTextString(headers[i]);  
  88.             cell.setCellValue(text);  
  89.             cell.setCellStyle(titleStyle);  
  90.         }  
  91.   
  92.         int rowCount = 1;  
  93.         for (int i = 0; i < data.size(); i++, rowCount++) {  
  94.             HSSFRow dataRow = sheet.createRow(rowCount);  
  95.             Person person = data.get(i);  
  96.   
  97.             // 序号  
  98.             HSSFCell cell0 = dataRow.createCell(0);  
  99.             cell0.setCellValue((i + 1));  
  100.             cell0.setCellStyle(contentStyle);  
  101.   
  102.             // 姓名  
  103.             HSSFCell cell1 = dataRow.createCell(1);  
  104.             cell1.setCellValue(person.getName());  
  105.             cell1.setCellStyle(contentStyle);  
  106.   
  107.             // 年龄,转化为String后放到cell里面  
  108.             HSSFCell cell2 = dataRow.createCell(2);  
  109.             cell2.setCellValue(person.getAge().toString());  
  110.             cell2.setCellStyle(contentStyle);  
  111.   
  112.             // 出生年月,转化为String后放到cell里面  
  113.             HSSFCell cell3 = dataRow.createCell(3);  
  114.             cell3.setCellValue(format.format(person.getBirthday()));  
  115.             cell3.setCellStyle(contentStyle);  
  116.   
  117.             // 是否学生,转化为String后放到cell里面  
  118.             HSSFCell cell4 = dataRow.createCell(4);  
  119.             String isStudent = person.isStudent() ? "是" : "否";  
  120.             cell4.setCellValue(isStudent);  
  121.             cell4.setCellStyle(contentStyle);  
  122.   
  123.             // 身高,转化为String后放到cell里面  
  124.             HSSFCell cell5 = dataRow.createCell(5);  
  125.             cell5.setCellValue(String.valueOf(person.getHeight()));  
  126.             cell5.setCellStyle(contentStyle);  
  127.         }  
  128.   
  129.         // 合并,从第一行到最后一行,从第七列到第七列  
  130.         sheet.addMergedRegion(new CellRangeAddress(0, rowCount - 166));  
  131.         // 合并单元格的内容,合并单元格后,仅会保留第一行,第七列的内容,所以设置第一行第七列的内容  
  132.         HSSFCell cell6 = row.createCell(6);  
  133.         cell6.setCellStyle(contentStyle);  
  134.         cell6.setCellValue("合并单元格的内容");  
  135.   
  136.         workbook.write(out);  
  137.     }  
  138. }  

        简略过程:

        1. 通过new HSSFWorkBook生成一个workBook;

        2. 通过workBook的createSheet生成一个sheet,即工作表,同时可为工作表命名;

        3. 通过sheet的createRow生成一行,sheet中的行数从0开始,表示第一行;

        4. 通过row的createCell生成一列,sheet中的列数从0开始,表示第一列;

        5. 通过workBook.write,将内容输出到一个excel文件中。


        主要说明:

        1. HSSFCellStyle用于设定单元格的style;

        2. HSSFFont用于设定单元格的字体;

        3. 通过sheet.addMergedRegion(开始行号,结束行号,开始列号,结束列号)方法,可合并单元格,当需要合并多行的某列时,设置开始列号等于结束列号即可;当需要合并多列的某行时,设置开始行号等于结束行号即可;

        4. Excel有一特性——合并多行时,合并后的内容为合并中的第一行的内容;合并多列时,合并后的内容为合并中的多列的最左上角一列的内容——所以在合并时,只需要设置指定的单元格的内容,即可设置合并后的单元格的内容;

        5. 行号和列号均是从0开始的,表示第一行或第一列。


文章转载:http://blog.csdn.net/geloin/article/details/17219885

你可能感兴趣的:(poi生成Excel)