POI導出Excle HSSF

package com.sk.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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;

/**
 * 生成導出Excel文件對象
 * 
 * @author chico
 * 
 */
public class ExportExcel {
	private static HSSFFont fontStyle = null;
	private static HSSFCellStyle cellStyle = null;

	public static HSSFFont getTitleFont(HSSFWorkbook wb) {
		fontStyle = wb.createFont();
		fontStyle.setFontName("宋體");
		fontStyle.setFontHeightInPoints((short) 12);
		fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字體加粗
		return fontStyle;
	}
	
	public static HSSFFont getContentFont(HSSFWorkbook wb) {
		fontStyle = wb.createFont();
//		fontStyle.setFontName("宋體");
//		fontStyle.setFontHeightInPoints((short) 12);
		fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		return fontStyle;
	}
	
	public static HSSFCellStyle getTitleStyle(HSSFWorkbook workbook){
		cellStyle = workbook.createCellStyle(); // 在工做薄的基礎上創建一個樣式
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左邊框
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右邊框
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 頂邊框
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 底邊框
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
//		cellStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); // 填充的背景顏色
		cellStyle.setFont(getTitleFont(workbook));
		return cellStyle;
	}
	
	public static HSSFCellStyle getContentStyle(HSSFWorkbook workbook){
		cellStyle = workbook.createCellStyle(); // 在工做薄的基礎上創建一個樣式
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左邊框
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右邊框
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 頂邊框
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 底邊框
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
//		cellStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); // 填充的背景顏色
//		cellStyle.setFont(getContentFont(workbook));
		return cellStyle;
	}
	
	public static HSSFCellStyle getLeftContentStyle(HSSFWorkbook workbook){
		cellStyle = workbook.createCellStyle(); // 在工做薄的基礎上創建一個樣式
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左邊框
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右邊框
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 頂邊框
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 底邊框
		cellStyle.setVerticalAlignment(HSSFCellStyle.ALIGN_FILL);//垂直居中
//		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
//		cellStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); // 填充的背景顏色
//		cellStyle.setFont(getContentFont(workbook));
		return cellStyle;
	}
	
	public static void createTitleRow(HSSFRow curRow,HSSFWorkbook workbook){
		HSSFCell curCell = curRow.createCell(0);
		curCell.setCellStyle(getTitleStyle(workbook));
		curCell.setCellValue("姓名");
		HSSFCell curCell2 = curRow.createCell(1);
		curCell2.setCellStyle(getTitleStyle(workbook));
		curCell2.setCellValue("項目");
	}
	
	public static void createContentRow(HSSFCell curCell,HSSFWorkbook workbook,String value){
		curCell.setCellStyle(ExportExcel.getContentStyle(workbook));
		curCell.setCellValue(value);
	}
	
	public static void createLeftContentRow(HSSFCell curCell,HSSFWorkbook workbook,String value){
		curCell.setCellStyle(ExportExcel.getLeftContentStyle(workbook));
		curCell.setCellValue(value);
	}

	public static void main(String[] args) {
		HSSFWorkbook workbook = new HSSFWorkbook(); // 創建一個工做薄
		
		HSSFSheet sheet = workbook.createSheet();
		sheet.setDefaultRowHeightInPoints(15);
		sheet.setDefaultColumnWidth(50);
		
		HSSFRow curRow = sheet.createRow(0);
		HSSFCell curCell = curRow.createCell(0);
		curCell.setCellStyle(getTitleStyle(workbook));
		curCell.setCellValue("姓名");
		HSSFCell curCell2 = curRow.createCell(1);
		curCell2.setCellStyle(getTitleStyle(workbook));
		curCell2.setCellValue("項目");
		FileOutputStream fos;
		try {
			fos = new FileOutputStream("D:\\test.xls");
			workbook.write(fos);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 填充圖案
		// 假設什麼定義了一個樣式,想在填充第一個單元格的時候填充紅,第二格單元格填充藍色。
		// 若是:
		// HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立一個樣式
		// cellStyle.setFillForegroundColor(HSSFColor.RED.index); // 設置顏色爲紅色
		// cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// HSSFCell cell1 = row.createCell((short) 1); // 給單元格cell1填充紅色
		// cell1.setCellStyle(cellStyle);
		// 若:
		// cellStyle.setFillForegroundColor(HSSFColor.BLUE.index); // 設置顏色爲藍色
		// HSSFCell cell2 = row.createCell((short) 2); // 給單元格cell2填充藍色
		// cell2.setCellStyle(cellStyle);
	}

}
相關文章
相關標籤/搜索