poi生成Excel

news/2024/7/21 7:40:10 标签: poi, excel, 生成

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


http://www.niftyadmin.cn/n/1262605.html

相关文章

海量数据库解决方案2011030201

【摘抄】&#xfeff; 反向键索引其最大的特点就是对于原有相连比较紧密的值&#xff0c;强制使其分散到相距比较远的位置上。这种情况下使用等值运算符""所构成的查询条件。(从应用角度来说&#xff0c;它受了太多的限制&#xff0c;但是一个思路。)位图索引(Bitmap…

用计算机对圆周率估计的方法,用计算机模拟的方法估计圆周率的值.doc

用计算机模拟的方法估计圆周率的值【摘要】本文用计算机Excel软件产生随机数来模拟向下图所示的正方形中撒芝麻的试验来估计圆周率π的值。分别从估计原理和操作方法方面加以说明。【关键词】Excel&#xff1b;随机数&#xff1b;估计&#xff1b;π北师大版数学必修三中多次用…

poi读取Excel

本文为poi生成Excel的序章。 示例代码如下所示&#xff1a; /*** */ package com.geloin.poi.main;import java.io.FileInputStream; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.I…

html思维导图word版,[精选]思维导图(完美排版word).doc

[精选]思维导图(完美排版word)前 言托尼&#xff1a;大学二年级那年&#xff0c;有一天我昂首阔步&#xff0c;兴冲冲地来到图书馆&#xff0c;问图书管理员说&#xff0c;在哪儿可以找到一本谈人的大脑和如何使用大脑的书。她立即指点我去医学图书部&#xff01;我跟她解释说&…

javascript动态创建VML

VML是The Vector Markup Language(矢量可标记语言)的缩写。VML用于将图形数据矢量化的标记语言。这是一种基于 XML 语法的语言&#xff0c;由 AutoDesk 、 Macromedia 和 Microsoft 和 HP 公司向 W3C 提出的方案&#xff0c;于1999年9月附带IE5.0发布的。使用VML可以在IE中绘制…

jxls操作excel文件

JXLS是基于Jakarta POI API的Excel报表生成工具&#xff0c;可以生成精美的Excel格式报表。它采用标签的方式&#xff0c;类似JSP标签&#xff0c;写一个Excel模板&#xff0c;然后生成报表&#xff0c;非常灵活&#xff0c;简单&#xff01; JXLS软件首页&#xff1a;http://j…

JXLS生成Excel中循环的两种用法

在jxls操作excel文件一文中&#xff0c;提到可以使用<jx:forEach>进行迭代处理&#xff0c;JXLS中还有另一种循环方式&#xff0c;且看如下示例。 Excel模板如下所示&#xff1a; 现有类Staff&#xff0c;内容如下所示&#xff1a; /*** */ package com.geloin.jxls.bean…

计算机网络 学习指南,计算机网络工程师学习路线指南

随着计算机网络在社会生活各个领域的广泛应用,网络安全问题越来越成为人们关注的焦点。下面是学习啦小编收集整理的计算机网络工程师学习路线指南&#xff0c;希望对大家有帮助~~计算机网络工程师学习路线指南工具/原料坚持方法/步骤网络工程师考试的题目通常难度都不高&#x…