POI 키트를 사용하여 Excel 파일로 데이터 내보내기

4294 단어 poiExcel 내보내기
package cn.foxeye.demo.excel;

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;
import org.apache.poi.ss.usermodel.CellStyle;

import abysscode.commons.utils.StringUtility;

public class ExprotDataToExcel {

	public static void main(String[] args) {
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(" ");
		sheet.setDefaultColumnWidth(10);
		HSSFRow zeroRow = sheet.createRow(0);
		zeroRow.setHeight((short) 300);

		//  
		for (int i = 0; i < 10; i++) {
			HSSFCell cell = zeroRow.createCell(i);
			cell.setCellValue(" " + i);

			//  
			setHeaderStyle(workbook, cell);
		}

		//  
		for (int i = 1; i <= 30; i++) {
			HSSFRow row = sheet.createRow(i);
			for (int j = 0; j < 10; j++) {
				HSSFCell cell = row.createCell(j);
				cell.setCellValue(" " + i + j);
				
				//  
				setContentStyle(workbook, cell);
			}
		}

		//  
		exprotExcel(workbook, null);
	}

	/**
	 *  
	 * @param workbook
	 * @param cell
	 */
	public static void setHeaderStyle(HSSFWorkbook workbook, HSSFCell cell) {
		HSSFFont font = workbook.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName(" ");
		font.setFontHeight((short) 220);

		CellStyle cs = workbook.createCellStyle();
		cs.setAlignment(CellStyle.ALIGN_CENTER);
		cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		cs.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
		cs.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
		cs.setFont(font);
		cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cs.setWrapText(true);
		cell.setCellStyle(cs);
	}

	/**
	 *  
	 * @param workbook
	 * @param cell
	 */
	public static void setContentStyle(HSSFWorkbook workbook, HSSFCell cell) {

		HSSFFont cellFont = workbook.createFont();
		cellFont.setFontName(" ");
		cellFont.setFontHeight((short) 210);

		CellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		cellStyle.setFont(cellFont);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cellStyle.setWrapText(true);
		cell.setCellStyle(cellStyle);
	}

	/**
	 *  
	 * @param workbook
	 * @param fileName
	 */
	public static void exprotExcel(HSSFWorkbook workbook, String fileName) {
		FileOutputStream outputStream = null;
		fileName = StringUtility.isNotBlank(fileName) ? fileName : workbook.getSheetName(0);
		try {
			outputStream = new FileOutputStream("c:\\" + fileName + ".xls");
			workbook.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

}

좋은 웹페이지 즐겨찾기