话不多说
直接上代码
其中,单元格填充背景色还没搞定
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AMes.Code;
using AMes.Data;
using AMes.Domain.Entity.BasicDataManage;
using AMes.Domain.Entity.ProcessInspectionManage;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
namespace AMes.Application.ExcelImport
{
public class DeBugExcelExport
{
IRepositoryBase service = new RepositoryBase();
public string Export(string[] VersionIds)
{
string path = AppDomain.CurrentDomain.BaseDirectory + Configs.GetValue("ExportPath") + "\\调试记录导出模板.xls";
//开启文件流
FileStream fs = null;
//创造一个
IWorkbook workBook = null;
try
{
fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
workBook = new HSSFWorkbook();
//创建一个样式
ICellStyle style1 = workBook.CreateCellStyle();
//设置单元格字体居中
style1.Alignment = HorizontalAlignment.Center;
style1.VerticalAlignment = VerticalAlignment.Center;//上下居中
//设置边框
style1.BorderBottom = BorderStyle.Thin;
style1.BorderLeft = BorderStyle.Thin;
style1.BorderRight = BorderStyle.Thin;
style1.BorderTop = BorderStyle.Thin;
style1.BottomBorderColor = HSSFColor.Black.Index;
style1.LeftBorderColor = HSSFColor.Black.Index;
style1.RightBorderColor = HSSFColor.Black.Index;
style1.TopBorderColor = HSSFColor.Black.Index;
//设置字体
IFont font = workBook.CreateFont();
font.FontName = "宋体";
font.FontHeightInPoints = 11;
style1.SetFont(font);
//设置背景色
style1.FillBackgroundColor = HSSFColor.Blue.Index;
style1.FillPattern = FillPattern.SolidForeground;
ICellStyle style = workBook.CreateCellStyle();
style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
style.FillPattern = FillPattern