`

利用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之间
/*HSSFPalette palette=wb.getCustomPalette();
palette.setColorAtIndex((short)9, (byte)HSSFColor.RED.index, (byte)HSSFColor.GREEN.index, (byte)HSSFColor.BLUE.index);
palette.setColorAtIndex((short)10, (byte)HSSFColor.RED.index, (byte)HSSFColor.GREEN.index, (byte)HSSFColor.BLUE.index);
palette.setColorAtIndex((short)11, (byte)HSSFColor.RED.index, (byte)HSSFColor.GREEN.index, (byte)HSSFColor.BLUE.index);

cellStyle1.setFillForegroundColor((short)9);
*/

//设置格子填充
/*cellStyle1.setFillPattern(HSSFCellStyle.BIG_SPOTS);
cellStyle1.setFillForegroundColor(HSSFColor.RED.index);
cellStyle1.setFillBackgroundColor(HSSFColor.LIGHT_BLUE.index);*/


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();
}

}
}





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics