poi 操作excel的样式

需要导入poi-3.9-20121203.jar

import java.io.FileOutputStream;


import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
public class Poi5 {
    
public static void main(String[] args) throws Exception {
//定义一个新的工作簿
Workbook wb=new HSSFWorkbook(); 
//创建sheet1
Sheet sheet = wb.createSheet("sheet1"); 
//创建行
Row row = sheet.createRow(5);
//设置行高
row.setHeightInPoints(30);
Cell cell = row.createCell(1);
Cell cell2 = row.createCell(2);
cell.setCellValue(new HSSFRichTextString("合并单元格"));
//单元格样式类
CellStyle cellStyle=wb.createCellStyle(); 
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框虚线
cellStyle.setTopBorderColor(IndexedColors.GREEN.getIndex()); //颜色
cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
cell.setCellStyle(cellStyle);

CellStyle cellStyle2=wb.createCellStyle(); 
cellStyle2.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框虚线
cellStyle2.setTopBorderColor(IndexedColors.GREEN.getIndex()); //颜色
cell2.setCellStyle(cellStyle2);
//合并单元格
sheet.addMergedRegion(new CellRangeAddress(
5, // 起始行
5, // 结束行
1, // 其实列
2  // 结束列
));

   //创建一个输入流
FileOutputStream fileOutputStream = new FileOutputStream("d:\\简单的工作簿.xls");
//写入
wb.write(fileOutputStream);
}
}

效果图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值