天天看点

利用org.apache.poi简单导出Excel

package com;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFPalette;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;

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;

public class ExportExcel {

public static String outputFile="C:/test1.xls";

public static void main(String[] args) {

try {

HSSFWorkbook wb=new HSSFWorkbook();

FileOutputStream outputStream=new FileOutputStream(outputFile);

HSSFSheet sheet1= wb.createSheet();

HSSFCellStyle cellStyle1=wb.createCellStyle();

//设置前景模式的填充色

//cellStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 一定要加这个填充模式才可以填充背景颜色

//设置前景色

//cellStyle1.setFillForegroundColor(HSSFColor.RED.index);

//HSSFFont font=wb.createFont();

//font.setColor((short)255);

//cellStyle1.setFont(font);

//还可以使用自定义颜色 注意 index的值只能在8~64之间

//设置格子填充

HSSFRow row1=sheet1.createRow(0);

String[] header1={"姓名","性别","地址","联系方式","电话"};

for(int i=0;i<header1.length;i++){

HSSFCell cell=row1.createCell((short)i);

cell.setCellValue(new HSSFRichTextString(header1[i]));

cell.setCellStyle(cellStyle1);

}

wb.write(outputStream);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}