poi导出Excel 属性的设置篇

这篇博客详细介绍了如何使用Apache POI库在Java中创建Excel文件,并设置了包括单元格内容、字体样式、背景颜色、对齐方式、边框以及日期格式在内的各种属性。示例代码展示了创建工作簿、添加工作表、设置行高、合并单元格、调整字体样式以及添加边框等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值