您的位置:首页 > 其它

poi导出Excel 属性的设置篇

2016-07-08 16:42 411 查看
package com.oa.action.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.http.HttpResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
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.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.struts2.ServletActionContext;

import com.itextpdf.text.Font;

public class Test004 {

public static void main(String[] args) {
//1.创建一个webbook工作薄对象,对应的就是一个Excel
HSSFWorkbook wobBook=new HSSFWorkbook();
HSSFSheet sheet=wobBook.createSheet("JavaA");
//3.创建行行的下标从0开始
HSSFRow row1=sheet.createRow(2);
HSSFRow row2=sheet.createRow(5);
//高度的设置
row2.setHeightInPoints(30); // 设置行的高度
//4.通过行创建列
HSSFCell cell_1=row1.createCell(2);
HSSFCell cell_11=row1.createCell(5);
HSSFCell cell_2=row1.createCell(2);
HSSFCell cell_21=row1.createCell(5);
//赋值//创建表名
cell_2.setCellValue("测试");
//合并单元格 new CellRangeAddress(2,5,2,5)合并第三行及第六行,及第三列及第六列
sheet.addMergedRegion(new CellRangeAddress(2, 5, 2, 5));
//设置背景颜色
CellStyle style = wobBook.createCellStyle();
style.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
//居中
// 设置单元格内容水平对其方式
// XSSFCellStyle.ALIGN_CENTER       居中对齐
// XSSFCellStyle.ALIGN_LEFT         左对齐
// XSSFCellStyle.ALIGN_RIGHT        右对齐
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
//style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
// 设置单元格内容垂直对其方式
// XSSFCellStyle.VERTICAL_TOP       上对齐
// XSSFCellStyle.VERTICAL_CENTER    中对齐
// XSSFCellStyle.VERTICAL_BOTTOM    下对齐
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);

cell_2.setCellStyle(style);
//cell_21.setCellStyle(style);
//cell_11.setCellStyle(style);
//设置字体,名称居中 字体颜色红,字体大小,加粗
HSSFFont font  = wobBook.createFont();
font.setItalic(true);//设置字体为斜体字
font.setColor(HSSFColor.RED.index); // 字体颜色
//font.setColor(HSSFColor.RED.index);//将字体设置为“红色”
font.setFontHeightInPoints((short)30);//字号设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
font.setFontName("宋体"); // 将“黑体”字体应用到当前单元格上
font.setUnderline(HSSFFont.U_DOUBLE); // 添加(Font.U_SINGLE单条下划线/Font.U_DOUBLE双条下划线)
font.setStrikeout(true); // 是否添加删除线
style.setFont(font);//将字体应用到样式上面
cell_2.setCellStyle(style);

//border边框的设置
// 设置这些样式
//style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//border边框属性值thin细线 dashed虚框THICK粗边线
// CellStyle.BORDER_THICK       粗边线
// 创建单元格样式对象
// CellStyle.BORDER_DOUBLE      双边线
// CellStyle.BORDER_THIN        细边线
// CellStyle.BORDER_MEDIUM      中等边线
// CellStyle.BORDER_DASHED      虚线边线
// CellStyle.BORDER_HAIR        小圆点虚线边线
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_DASHED);
style.setBorderRight(HSSFCellStyle.BORDER_DASHED);
style.setBorderTop(HSSFCellStyle.BORDER_THICK);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

//创建列表头【id name】按照下标来创建表头的位置
HSSFRow row3=sheet.createRow(7);
row3.createCell(2).setCellValue(1);
row3.createCell(3).setCellValue("aa");
HSSFRow row4=sheet.createRow(8);
row4.createCell(2).setCellValue(2);

// 我们将这第二个单元格改成日期格式,这需要从工作薄创建一个新的单元格格式,这可// 以只影响当前建立的一个单元格。
//set date format
CellStyle cellStyle = wobBook.createCellStyle();
DataFormat format= wobBook.createDataFormat();
Date date=new Date();
System.out.println(date);
row4.createCell(2).setCellValue(date);
cellStyle.setDataFormat(format.getFormat("yyyy-MM-dd"));
row4.createCell(3).setCellStyle(cellStyle);

//输出文件
//防止文件名重复发生覆盖,采用时间戳或uuid
SimpleDateFormat sim=new SimpleDateFormat("yyyyMMdd");
String simple=sim.format(new Date());
RandomStringUtils randomStringUtils=new RandomStringUtils();
String random=randomStringUtils.randomNumeric(6);
String xlsName=simple+random;
try {
FileOutputStream fileOutputStream=new FileOutputStream("g:/"+xlsName+".xls");
wobBook.write(fileOutputStream);
fileOutputStream.close();
fileOutputStream.flush();//刷新
System.out.println("导出成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: