Java使用poi生成Excel

package test.cc;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

public class poi {
	
	public static String[] headers={"第一個","第二個","第三個"};
	public static String[] data1={"數據1","數據11","數據111"};
	public static String[] data2={"數據2","數據22","數據222"};
	public static List<String[]> dataset=new ArrayList<String[]>(){{
		add(data1);
		add(data2);
	}		
	};
	
	
	public static void main(String[] args) throws IOException {
		OutputStream os=new FileOutputStream("test.xlsx");
		// 聲明一個工做薄
				HSSFWorkbook workbook = new HSSFWorkbook();
				try {
					// 生成一個表格
					HSSFSheet sheet = workbook.createSheet("testtest");
					// 生成一個樣式
					HSSFCellStyle headerStyle = workbook.createCellStyle();
					// 設置這些樣式
					headerStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
					headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
					headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
					headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
					headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
					headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
					headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
					// 生成一個字體
					HSSFFont headerFont = workbook.createFont();
					headerFont.setColor(HSSFColor.VIOLET.index);
					headerFont.setFontHeightInPoints((short) 12);
					headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
					// 把字體應用到當前的樣式
					headerStyle.setFont(headerFont);
					// 生成並設置另外一個樣式
					HSSFCellStyle dataStyle = workbook.createCellStyle();
					dataStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
					dataStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
					dataStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
					dataStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
					dataStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
					dataStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
					dataStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
					dataStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
					// 生成另外一個字體
					HSSFFont dataFont = workbook.createFont();
					dataFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
					// 把字體應用到當前的樣式
					dataStyle.setFont(dataFont);
					// 產生表格標題行
					HSSFRow row = sheet.createRow(0);
					HSSFCell noCell = row.createCell(0);
					noCell.setCellStyle(headerStyle);
					noCell.setCellValue("編號");
					for (int headerIndex = 0; headerIndex < headers.length; headerIndex++) {
						HSSFCell headerCell = row.createCell(headerIndex + 1);
						headerCell.setCellStyle(headerStyle);
						HSSFRichTextString text = new HSSFRichTextString(headers[headerIndex]);
						headerCell.setCellValue(text);
					}
					// 遍歷集合數據,產生數據行
					for (int dataIndex = 0; dataIndex < dataset.size(); dataIndex++) {
						row = sheet.createRow(dataIndex + 1);
						HSSFCell dataNoCell = row.createCell(0);
						dataNoCell.setCellStyle(dataStyle);
						dataNoCell.setCellValue(dataIndex + 1);
						String[] rowData = dataset.get(dataIndex);
						for (int fieldIndex = 0; fieldIndex < rowData.length; fieldIndex++) {
							HSSFCell dataCell = row.createCell(fieldIndex + 1);
							dataCell.setCellStyle(dataStyle);
							dataCell.setCellValue(rowData[fieldIndex]);
						}
					}
					for (int columnIndex = 0; columnIndex < headers.length + 1; columnIndex++) {
						sheet.autoSizeColumn(columnIndex, true);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					// 清理資源
					workbook.write(os);
					workbook.close();
				}
		
		
		
	}

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