用POI生成Excel文件的典型例子【基于poi3.0 附源码】_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 用POI生成Excel文件的典型例子【基于poi3.0 附源码】

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

 2011/12/2 7:30:27  xp9802  http://xp9802.iteye.com  我要评论(0)
  • 摘要:用POI生成Excel文件的典型例子,基于poi3.0ExportExcleDemo.java:packagecom;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf
  • 标签:excel 源码 文件 例子

用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

  • ExportExcleDemo.rar (1.5 MB)
  • 下载次数: 274
发表评论
用户名: 匿名