java hssf 写 excle

在 poi-2.5.1.jar 下

package com.club.community.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.club.exception.BusinessAccessException;


public class CreateExcelUtil {
	private static final Logger log = Logger.getLogger(CreateExcelUtil.class);
	
	private static final String SEPARATOR = File.separator;
	
	public static void crateExcel(String path, String fileName, String sheetName, List<String> titleList, List<String[]> contentList ) {//, List<String> titleList, List<String>
		log.info("生成excel开始,路径: " + path + SEPARATOR + fileName);
		
		HSSFWorkbook workbook = new HSSFWorkbook(); //创建新的Excel工作薄 
		HSSFSheet sheet;
		try {
			sheet = workbook.createSheet(new String(sheetName.getBytes(),"iso8859-1") );//在Excel工作薄中建工作表
			HSSFRow row = sheet.createRow((short) 0); //在索引0的位置建行(最顶端的行) 
			HSSFCell cell;
			String title = "";
			for (int i = 0; i < titleList.size(); i++) {
				title = titleList.get(i);
				cell = row.createCell((short) i);//在索引0的位置建单元格
				cell.setEncoding(HSSFCell.ENCODING_UTF_16); //定义单元格为字符串类型
				cell.setCellValue(title);
			}
			
			String[] contents = new String[titleList.size()];
			for (int i = 0; i < contentList.size(); i++) {
				row = sheet.createRow((short) i + 1); //在索引1的位置创建行(最顶端的行) 
				contents = contentList.get(i);
				for (int j = 0; j < contents.length; j++) {
					cell = row.createCell((short) j); //在索引0的位置创建单元格(左上端) 
					cell.setEncoding(HSSFCell.ENCODING_UTF_16); //定义单元格为字符串类型  
					cell.setCellValue(contents[j]); //在单元格输入一些内容 
				}
			}
		} catch (UnsupportedEncodingException e) {
			log.error(e);
			throw new BusinessAccessException("生成excel失败");
		}
		String fullPathName = path + SEPARATOR + fileName;
		File f = new File(fullPathName);
		if(!f.exists()) {
			f.getParentFile().mkdirs();
		}
		FileOutputStream fOut;//新建输出文件流 
		try {
			fOut = new FileOutputStream(fullPathName);
			workbook.write(fOut); //把相应的Excel工作薄存盘 
			fOut.flush();
			fOut.close(); //操作结束,关闭文件 
			log.info("生成excel结束,路径: " + path + SEPARATOR + fileName);
			System.out.println("excel文件已经生成,存放在" + fullPathName);
		} catch (FileNotFoundException e) {
			log.error(e);
			log.info("生成excel结束,路径: " + path + SEPARATOR + fileName);
			throw new BusinessAccessException("生成excel失败");
		} catch (IOException e) {
			log.error(e);
			log.info("生成excel结束,路径: " + path + SEPARATOR + fileName);
			throw new BusinessAccessException("生成excel失败");
		}
	}
	
	public static void main(String[] args) {
		List<String> titleList = new ArrayList<String>();
		titleList.add("作者");
		titleList.add("编辑");
		
		List<String[]> contentList = new ArrayList<String[]>();
		String[] a1 = {"张三", "李四"};
		String[] a2 = {"王五", "徐六"};
		String[] a3 = {"2222", "3333"};
		String[] a4 = {"4444", "5555"};
		contentList.add(a1);
		contentList.add(a2);
		contentList.add(a3);
		contentList.add(a4);
		
		crateExcel("c:\\abcdefg", "aa.xls", "bb", titleList, contentList);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值