`
xp9802
  • 浏览: 1182766 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

用POI生成Excel文件的典型例子【基于poi3.0 附源码】

阅读更多

用POI生成Excel文件的典型例子,基于poi3.0

ExportExcleDemo.java:

 

package com;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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;


public class ExportExcleDemo {

	/**
	 * 
	 * 功能描述:用POI生成Excel文件的典型例子
	 * 基于poi3.0
	 * @author http://xp9802.iteye.com
	 * create on: 2011-11-29
	 *
	 */
	public static void main(String[] args) {
			HSSFWorkbook workbook = new HSSFWorkbook();

			int begin = 1;
			int end = 50;

			HSSFSheet sheet = workbook.createSheet();

			HSSFCellStyle cellStyleHead = workbook.createCellStyle();
			HSSFCellStyle cellStyleContent = workbook.createCellStyle();

			HSSFFont font1 = workbook.createFont();
			HSSFFont font2 = workbook.createFont();

			font1.setFontHeightInPoints((short) 12);
			font1.setColor((short)2);
			font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

			font2.setFontHeightInPoints((short) 10);
			font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

			cellStyleHead.setFont(font1);
			cellStyleContent.setFont(font2);

			int rowIndex = 0;
			int colIndex = 0;
			HSSFRow row = sheet.createRow(rowIndex++);
			HSSFCell cell = row.createCell((short) colIndex++);
			String[] headTitles = { "第1列头部", "第2列头部", "第3列头部" };
			for (int i = 0; i < headTitles.length; i++) {
				cell = row.createCell((short) i);
				cell.setCellStyle(cellStyleHead);
				cell.setCellValue(new HSSFRichTextString(headTitles[i]));
			}

			for (int i = begin; i < end; i++) {
				row = sheet.createRow(rowIndex++);
				int index = 0;
				HSSFCell cell4 = row.createCell((short) index++);
				HSSFCell cell5 = row.createCell((short) index++);
				HSSFCell cell6 = row.createCell((short) index++);
				cell4.setCellStyle(cellStyleContent);
				cell5.setCellStyle(cellStyleContent);
				cell6.setCellStyle(cellStyleContent);
				cell4.setCellValue(new HSSFRichTextString("第1列数据"));
				cell5.setCellValue(new HSSFRichTextString("第2列数据"));
				cell6.setCellValue(new HSSFRichTextString("第3列数据"));
			}
			for (int i = 0; i < 3; i++) {
				sheet.autoSizeColumn((short) i);
			}
			File f=new File("D:\\exampleExcle.xls");
			FileOutputStream outputStream;
			try {
				outputStream = new FileOutputStream(f);
				try {
					workbook.write(outputStream);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

	}

  

源码解压密码为:xp9802.iteye.com

7
4
分享到:
评论
3 楼 18335864773 2017-06-08  
尝试用一下pageoffice插件去生成excel把。代码调用简单清晰高效。支持Office2003、2007、2010、2013、2016.
POI
较大的Word、Excel文档时,JVM很容易内存溢出。用PageOffice提取Word、Excel文档中的内容,40MB大小的文档没问题,100MB大小的文档也无压力。
2 楼 xp9802 2011-12-02  
celsword 写道
少东西了……

少了啥??POI包有了哦
1 楼 celsword 2011-12-02  
少东西了……

相关推荐

Global site tag (gtag.js) - Google Analytics