Poi生成Excel文件

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

發佈了34 篇原創文章 · 獲贊 15 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章