利用poi导出word excel

多方借鉴 然后成为如下了

1.导入资源包

2.加上如下代码



package com.tcl.Utils;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExportWordUtil {
	
	public static void main(String[] args) {
		try {
			String content = "<html>";// 拼接注意加上<html>
			for (int i = 0; i < 10; i++) {
				String title = "hello,all" + i;
				// html拼接出word内容
				content += "<div style=\"text-align: center\"><span style=\"font-size: 24px\"><span style=\"font-family: 黑体\">"
						+ title + "<br /> <br /> </span></span></div>";
				content += "<div style=\"text-align: left\"><span >" + title
						+ "----------<br /> <br /> </span></span></div>";
				// 插入分页符
				content += "<span lang=EN-US style='font-size:12.0pt;line-height:150%;mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;mso-ansi-language:EN-US;mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'><br clear=all style='page-break-before:always'></span>";
				content += "<p class=MsoNormal style='line-height:150%'><span lang=EN-US style='font-size:12.0pt;line-height:150%'><o:p> </o:p></span></p>";

			}
			content += "</html>";
			byte b[] = content.getBytes();
			ByteArrayInputStream bais = new ByteArrayInputStream(b);
			POIFSFileSystem poifs = new POIFSFileSystem();
			DirectoryEntry directory = poifs.getRoot();
			DocumentEntry documentEntry = directory.createDocument(
					"WordDocument", bais);
			
			
			FileOutputStream fops = new FileOutputStream("f:/w.doc");
			
			poifs.writeFilesystem(fops);
			
//			wb.write(fops);
			fops.close();bais.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

}



excel

package com.tcl.Utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExportExcelUtil {
	@SuppressWarnings("deprecation")

	// 测试提供数据
	private static List<ExportInfo> getExportInfo() {
		List<ExportInfo> list = new ArrayList<ExportInfo>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
		try {
			ExportInfo expInfo1 = new ExportInfo(1, "Rambo蓝波", 25,
					df.parse("1988-12-03"));
			ExportInfo expInfo2 = new ExportInfo(2, "Amy艾米", 23,
					df.parse("1991-03-17"));
			ExportInfo expInfo3 = new ExportInfo(3, "Eva伊娃", 24,
					df.parse("1991-08-12"));
			ExportInfo expInfo4 = new ExportInfo(4, "Chery开瑞思", 25,
					df.parse("1991-08-16"));
			list.add(expInfo1);
			list.add(expInfo2);
			list.add(expInfo3);
			list.add(expInfo4);
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		return list;
	}
	public String isNull(Object ob){
		if(MyStrUtil.isBlank(ob)){
			return "";
		}
		if("null".equals(ob.toString())){
			return "";
		}return ob.toString();
	}
	/**
	 * POI技术导出数据到Excel文件 所使用到的JAR包:poi-3.10-FINAL-20140208.jar
	 * 保存方式:将需要保存的列以BEAN的形式保存在对象中,然后通过以下方式导出到Excel文件
	 * 
	 * @author <a href="mailto:yanglan@yysoft,com">颍源科技_杨兰</a>
	 * @sheetName Excel sheet页的名称
	 * @cellTitle 单元格的表头名称
	 * @savePath Excel文件保存的具体路径
	 * @saveName Excel文件的文件名(不需要填写后缀名)
	 */
	@SuppressWarnings("deprecation")
	public boolean ExportExcelByPOI(String sheetName, String[] cellTitle,
			String savePath, String saveName) {
		// 第一步 , 创建一个webbook , 对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步 , 在webbook中添加一个sheet , 对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet(sheetName);
		// 第三步 , 在Sheet中添加表头第0行 , 注意老版本poi对Excel的行数有限制short
		HSSFRow row = sheet.createRow((int) 0);
		// 第四步 , 创建单元格,并设置表头值,设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 创建一个居中格式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFCell cell = null;

		// 设置sheet表头的标题
		for (int i = 0; i < cellTitle.length; i++) {
			cell = row.createCell((short) i);
			cell.setCellValue(cellTitle[i]);
			cell.setCellStyle(style);
		}
		// 第五步 ,写入实体数据 ,(实际应用中的这些数据从数据库得到)
		List<ExportInfo> list = getExportInfo();
		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow((int) i + 1);
			// 数据bean
			ExportInfo expInfo = (ExportInfo) list.get(i);
			// 第四步 , 创建单元格并设置值
			cell = row.createCell((short) 0);
			cell.setCellValue((int) expInfo.getId());
			cell.setCellStyle(style);

			cell = row.createCell((short) 1);
			cell.setCellValue(expInfo.getName());
			cell.setCellStyle(style);

			cell = row.createCell((short) 2);
			cell.setCellValue((int) expInfo.getAge());
			cell.setCellStyle(style);

			cell = row.createCell((short) 3);
			cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(expInfo
					.getBirthday()));
			cell.setCellStyle(style);
		}
		// 第六步 , 将文件保存到指定位置
		try {
			FileOutputStream fops = new FileOutputStream(savePath + saveName);
			wb.write(fops);
			fops.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	// 测试保存方式
	public static void main(String[] args) {
		String[] cellTitle = { "ID", "Name", "Age", "Birthday" };
		String savePath = "F:/";
		String saveName = "/ExpInfo";
		ExportExcelUtil expExcelUtils = new ExportExcelUtil();
		boolean exportStatus = expExcelUtils.ExportExcelByPOI("问题收集",
				cellTitle, savePath, saveName + ".xls");
		if (exportStatus) {
			System.out.println("Excel文件导出成功!!!");
		} else {
			System.out.println("Excel文件导出失败!!!");
		}
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值