poi 写 2003,2007 excel

本文介绍了一种利用Apache POI库在Java中创建Excel文件的方法。通过示例代码展示了如何设置字体样式、单元格格式,并填充数据。适用于需要批量生成Excel报表的应用场景。
这段代码是poi官网上给的,不过里面有些变量重复定义,我稍微改了下

注意,此代码是在 poi-3.7-20101029.jar 下

package com.club.community.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class CreateExcel {
	public static void main(String[] args) {
		CreateExcel.crateExcel();
	}
	
	public static void crateExcel() {
		Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() };
		for(int i=0; i<wbs.length; i++) {
		   Workbook wb = wbs[i];
		   CreationHelper createHelper = wb.getCreationHelper();

		   // create a new sheet
		   Sheet s = wb.createSheet();
		   // declare a row object reference
		   Row r = null;
		   // declare a cell object reference
		   Cell c = null;
		   Cell c2 = null;
		   // create 2 cell styles
		   CellStyle cs = wb.createCellStyle();
		   CellStyle cs2 = wb.createCellStyle();
		   DataFormat df = wb.createDataFormat();

		   // create 2 fonts objects
		   Font f = wb.createFont();
		   Font f2 = wb.createFont();

		   // Set font 1 to 12 point type, blue and bold
		   f.setFontHeightInPoints((short) 12);
		   f.setColor( IndexedColors.RED.getIndex() );
		   f.setBoldweight(Font.BOLDWEIGHT_BOLD);

		   // Set font 2 to 10 point type, red and bold
		   f2.setFontHeightInPoints((short) 10);
		   f2.setColor( IndexedColors.RED.getIndex() );
		   f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

		   // Set cell style and formatting
		   cs.setFont(f);
		   cs.setDataFormat(df.getFormat("#,##0.0"));

		   // Set the other cell style and formatting
		   cs2.setBorderBottom(CellStyle.BORDER_THIN);
		   cs2.setDataFormat(df.getFormat("text"));
		   cs2.setFont(f2);


		   // Define a few rows
		   for(int rownum = 0; rownum < 30; rownum++) {
			   r = s.createRow(rownum);
			   for(int cellnum = 0; cellnum < 10; cellnum += 2) {
				   c = r.createCell(cellnum);
				   c2 = r.createCell(cellnum+1);
		   
				   c.setCellValue((double)rownum + (cellnum/10));
				   c2.setCellValue(
						   createHelper.createRichTextString("Hello! " + cellnum + "马宏敏"));
			   }
		   }
		   
		   // Save
		   String filename = "c://2222//workbook.xls";
		   if(wb instanceof XSSFWorkbook) {
		     filename = filename + "x";
		   }
		 
		   FileOutputStream out;
		try {
			out = new FileOutputStream(filename);
			wb.write(out);
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		   
		}

	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值