您的位置:首页 > 运维架构 > Apache

使用Apache POI,实现导出Excel的功能

2014-06-12 10:18 711 查看
Apache POI是用Java编写的免费开源的Java API,它提供了对Microsoft Office格式档案读和写的功能。

Apache POI源码和库(JAR包)的官方下载地址是:http://poi.apache.org/download.html

我也提供了一个下载地址:http://download.csdn.net/detail/gaojinshan/7487253

它是最新的版本(POI-3.10-FINAL-20140208),包含源码和库(tar.gz格式)。
参考代码如下:

package test;

import java.io.FileOutputStream;
import java.io.IOException;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.alibaba.fastjson.JSON;

class DataInfo {
	private String countDate; // 统计日期
	private String channelId; // 渠道号

	public String getCountDate() {
		return countDate;
	}

	public void setCountDate(String countDate) {
		this.countDate = countDate;
	}

	public String getChannelId() {
		return channelId;
	}

	public void setChannelId(String channelId) {
		this.channelId = channelId;
	}
};

public class ExportExcelTest {
	public static void main(String[] args) throws IOException {
		// 创建
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet();
		// 创建单元格样式
		HSSFCellStyle titleCellStyle = wb.createCellStyle();
		// 指定单元格居中对齐,边框为细
		titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		titleCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		titleCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		titleCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		titleCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		titleCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		// 设置填充色
		titleCellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 指定当单元格内容显示不下时自动换行
		titleCellStyle.setWrapText(true);
		// 设置单元格字体
		HSSFFont titleFont = wb.createFont();
		titleFont.setFontHeightInPoints((short) 12);
		titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		titleCellStyle.setFont(titleFont);
		HSSFRow headerRow = sheet.createRow(0);
		HSSFCell headerCell = null;
		String[] titles = { "统计日期", "渠道号" };
		for (int c = 0; c < titles.length; c++) {
			headerCell = headerRow.createCell(c);
			headerCell.setCellStyle(titleCellStyle);
			headerCell.setCellValue(titles[c]);
			sheet.setColumnWidth(c, (30 * 160));
		}
		// ------------------------------------------------------------------
		// 创建单元格样式
		HSSFCellStyle cellStyle = wb.createCellStyle();
		// 指定单元格居中对齐,边框为细
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		// 设置单元格字体
		HSSFFont font = wb.createFont();
		titleFont.setFontHeightInPoints((short) 11);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		cellStyle.setFont(font);
		String infoStr = "[{\"channelId\":\"bodao\",\"countDate\":\"2014-06-11\"},"
				+ "{\"channelId\":\"dingzhi\",\"countDate\":\"2014-06-12\"},"
				+ "{\"channelId\":\"ruiwei\",\"countDate\":\"2014-06-13\"}]";
		List<DataInfo> list = JSON.parseArray(infoStr, DataInfo.class);
		for (int r = 0; r < list.size(); r++) {
			DataInfo item = list.get(r);
			HSSFRow row = sheet.createRow(r + 1);
			HSSFCell cell = null;
			int c = 0;
			cell = row.createCell(c++);
			cell.setCellStyle(cellStyle);
			cell.setCellValue(item.getCountDate());
			cell = row.createCell(c++);
			cell.setCellStyle(cellStyle);
			cell.setCellValue(item.getChannelId());
		}

		FileOutputStream fileOut = new FileOutputStream("test.xls");
		wb.write(fileOut);
		fileOut.close();
		System.out.println("Done");
	}
}


效果图如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐