Poi生成Excel文件

本文介绍如何利用Java的Poi库生成Excel文件,提供了一个详细的教程链接供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java poi生成Excel文件:

package com.util.excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import com.data.O_c_t_006;
import com.util.DataTypeChangeUtil;

/**
 *
 *  类描述:list集合中数据写入excel文件工具类
 * 
 */
@SuppressWarnings("deprecation")
@Component
public class InputListToExcelUtil {
	private static final Log logger = LogFactory.getLog(InputListToExcelUtil.class);
	
	@Autowired
	private DataTypeChangeUtil dataTypeChangeUtil;
	/**
	 * @author guoxk  
	 * 方法描述:对象列表写入excel文件
	 * @param ct006List  文件内容对象列表
	 * @param newName    文件标题行:XXXXXX文件列表
	 * @param tempPath   临时路径
	 */
	public void writeObjectListIntoExcel(List<O_c_t_006> ct006List, String fileTitle, String tempPath) {
		logger.info("对象列表写入文件开始》》》》》》》》》》》》》》");
		FileOutputStream fos = null;
		File file = null;
		try {
			//生成文件
			file = new File(tempPath);
			//生成工作薄
			HSSFWorkbook hwb = new HSSFWorkbook();
			//生成工作表
			HSSFSheet sheet = hwb.createSheet();
			
			//冻结窗格
//			sheet.createFreezePane(3, 2);
			
			//设置列宽
			sheet.setColumnWidth(0, 1100);
			sheet.setColumnWidth(1, 1500);
			sheet.autoSizeColumn(2);//自适应列宽
//			sheet.setColumnWidth(2, 5000);
			
			//设置单元格格式1--标题单元格
			HSSFCellStyle headStyle = hwb.createCellStyle();
			headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
			headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
			
			//设置单元格格式2--金额数据单元格
			HSSFCellStyle numberStyle = hwb.createCellStyle();
			numberStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//水平居右
			numberStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
			numberStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
			numberStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
			numberStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
			
			//设置单元格格式3---普通单元格
			HSSFCellStyle generalStyle = hwb.createCellStyle();
			generalStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
			generalStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
			generalStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
			generalStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
			generalStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
			
			//设置单元格格式4---备注单元格
			HSSFCellStyle msgStyle = hwb.createCellStyle();
			msgStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);//水平居左
			msgStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
			msgStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
			msgStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
			msgStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
			
			HSSFRow row = null;// 定义行号
			HSSFCell cell = null;// 定义单元格
			
			//生成标题行--大标题
			row = sheet.createRow(0);
			cell = row.createCell(0);
			cell.setCellValue(fileTitle);
			//应用单元格对齐方式
			cell.setCellStyle(headStyle);
			/*
			 * 合并单元格---参数定义:从0开始
			 * 0:第一个单元格行数
			 * 0:第二个单元格行数
			 * 0:第一个单元格列数
			 * 3:第二个单元格列数
			 */
			@SuppressWarnings("deprecation")
			CellRangeAddress rangeAddress = new CellRangeAddress(0, 0, 0, 3);
			sheet.addMergedRegion(rangeAddress);
			
			//生成标题行--列标题
			row = sheet.createRow(1);
			
			cell = row.createCell(0);
			cell.setCellValue("序号");
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);//设置单元格格式为文本格式
			cell.setCellStyle(generalStyle);//左右居中
			
			cell = row.createCell(1);
			cell.setCellValue("姓名");
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);//设置单元格格式为文本格式
			cell.setCellStyle(generalStyle);//左右居中
			
			cell = row.createCell(2);
			cell.setCellValue("身份证号码");
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);//设置单元格格式为文本格式
			cell.setCellStyle(generalStyle);//左右居中
			
			//生成表格内容
			for (int i = 0; i < ct006List.size(); i++) {
				O_c_t_006 c_t_006 = ct006List.get(i);
				//新增一行
				row = sheet.createRow(i+2);
				//新增单元格,并填值
				cell = row.createCell(0);
				cell.setCellValue(i+1);//序号
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);//设置单元格格式为文本格式
				cell.setCellStyle(generalStyle);//左右居中
				
				cell = row.createCell(1);
				cell.setCellValue(c_t_006.getC_016());//姓名
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);//设置单元格格式为文本格式
				cell.setCellStyle(generalStyle);//左右居中
				
				cell = row.createCell(2);
				cell.setCellValue(c_t_006.getC_013());//身份证号码
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);//设置单元格格式为文本格式
				cell.setCellStyle(generalStyle);//左右居中
			}
			//新增一行---签字行
			row = sheet.createRow(sheet.getLastRowNum()+2);
			cell = row.createCell(0);
			cell.setCellValue("     经   办   人 :   领导审批:      日  期:       ");
			// 合并单元格---参数定义:
			sheet.addMergedRegion(rangeAddress);
			
			fos = new FileOutputStream(file);
			hwb.write(fos);
			
			logger.info("对象列表写入文件完成《《《《《《《《《《《《《《《《《");
		} catch (IOException e) {
			logger.error("对象列表写入文件失败!" + e.getMessage());
		} finally {
			try {
				fos.flush();
				fos.close();
			} catch (IOException e) {
				logger.error("对象列表写入文件失败!" + e.getMessage());
			}
		}
	}
	
	/**
	 * @author guoxk
	 *
	 * 方法描述:测试方法
	 * @param args
	 */
//	public static void main(String[] args) {
//		List<O_c_t_006> ct006List = new ArrayList<O_c_t_006>();
//		O_c_t_006 c_t_006 = new O_c_t_006();
//		c_t_006.setC_013("dddd");
//		c_t_006.setC_014("ffff");
//		c_t_006.setC_012("fhghghgf");
//		c_t_006.setC_016("fffgxxxx");
//		ct006List.add(c_t_006);
//		new InputListToExcelUtil().writeObjectListIntoExcel(ct006List, "D://jg.xls");
//	}
}

同类文章参考:http://www.linuxidc.com/Linux/2014-10/108119.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值