poi生成Excel

来源:互联网 发布:淘宝称呼买家的昵称 编辑:程序博客网 时间:2024/05/19 13:57

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

        源代码示例:

/** *  */package com.geloin.poi.bean;import java.util.Date;/** * @author Geloin *  */public class Person {/** * 姓名 */private String name;/** * 年龄 */private Integer age;/** * 生日 */private Date birthday;/** * 是否学生 */private boolean isStudent;/** * 身高 */private double height;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}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;}}

/** *  */package com.geloin.poi.main;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.geloin.poi.bean.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:/work/proTmp/geloin/poi/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().toString());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);}}

        简略过程:

        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开始的,表示第一行或第一列。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 打架把眼睛打肿怎么办属于什么伤 罗马椅有点高做不了山羊挺身怎么办 节食一周后暴食肚子胀的难受怎么办 健身教练和会员聊天说错肌肉怎么办 两个月宝宝吃奶老是呛到怎么办 怀孕六个月体重一天增加两斤怎么办 备孕同房后一直乳头立起来怎么办 夏天出汉内衣老是湿的怎么办 大腿旁边长了红色的癣怎么办 跑步膝盖疼怎么办能不能再跑了 两周宝宝剧烈运动后咳嗽怎么办 bra的M有点紧L有点宽怎么办 穿吊带总是会露出来左胸罩杯怎么办 生小孩后腰部有一圈黑色勒痕怎么办 新买的饮水机热水口出水小怎么办 新买的饮水机热水口不出水怎么办 平胸没有适合自己的内衣怎么办 售楼小姐穿坏的丝袜都怎么办了 蛋白粉一天喝十克补不起来怎么办 跑步时没有卸妆毛孔堵塞了怎么办 自己做的葡萄酒太甜了怎么办 健身馆碰到一个帅的健身教练怎么办 随着年龄的增长脖子越来越短怎么办 安装软件时解析包出现问题怎么办 鼻子吸进去的气往嘴巴里怎么办 做完瑜伽之后大腿后侧特别紧怎么办 刚下生小狗腿后腿站不起来怎么办 脚被凳子压到了流血了怎么办还很痛 小狗脚被凳子压出血了怎么办 怀孕8个月脐带绕颈一周怎么办 练完瑜伽大腿两侧肌肉麻木怎么办 突然吃了辣火锅肚子烫怎么办 副鼻窦炎鼻头顶痛鼻子臭怎么办 一岁宝宝夜里睡觉不踏实怎么办 分手一个月了还是放不下前任怎么办 当晚上遇到烦心事睡不着该怎么办 为什么白天太累晚上就睡不着怎么办 白天走累的脚痛晚上睡不着怎么办 1岁宝宝感冒咳嗽流泪流鼻涕怎么办 宝宝不含母乳只吃奶瓶怎么办 宝宝习惯奶嘴不咬妈妈的乳头怎么办