You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
7.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util;
namespace Common.Tools
{
/// <summary>
/// DescriptionNpoi之Excel数据导出帮助类创建Excel表格行列设置行高设置字体样式单元格边框样式单元格背景颜色和样式单元格内容对齐方式等常用属性和样式封装
/// </summary>
public class NpoiExcelExportHelper
{
private static NpoiExcelExportHelper _exportHelper;
public static NpoiExcelExportHelper _
{
get => _exportHelper ?? (_exportHelper = new NpoiExcelExportHelper());
set => _exportHelper = value;
}
/// <summary>
/// TODO:先创建行,然后在创建对应的列
/// 创建Excel中指定的行
/// </summary>
/// <param name="sheet">Excel工作表对象</param>
/// <param name="rowNum">创建第几行(从0开始)</param>
/// <param name="rowHeight">行高</param>
public HSSFRow CreateRow(ISheet sheet, int rowNum, float rowHeight)
{
HSSFRow row = (HSSFRow)sheet.CreateRow(rowNum); //创建行
row.HeightInPoints = rowHeight; //设置列头行高
return row;
}
/// <summary>
/// 创建行内指定的单元格
/// </summary>
/// <param name="row">需要创建单元格的行</param>
/// <param name="cellStyle">单元格样式</param>
/// <param name="cellNum">创建第几个单元格(从0开始)</param>
/// <param name="cellValue">给单元格赋值</param>
/// <returns></returns>
public HSSFCell CreateCells(HSSFRow row, HSSFCellStyle cellStyle, int cellNum, string cellValue)
{
HSSFCell cell = (HSSFCell)row.CreateCell(cellNum); //创建单元格
cell.CellStyle = cellStyle; //将样式绑定到单元格
if (!string.IsNullOrWhiteSpace(cellValue))
{
//单元格赋值
cell.SetCellValue(cellValue);
}
return cell;
}
/// <summary>
/// 行内单元格常用样式设置
/// </summary>
/// <param name="workbook">Excel文件对象</param>
/// <param name="hAlignment">水平布局方式</param>
/// <param name="vAlignment">垂直布局方式</param>
/// <param name="fontHeightInPoints">字体大小</param>
/// <param name="isAddBorder">是否需要边框</param>
/// <param name="boldWeight">字体加粗 (None = 0,Normal = 400Bold = 700</param>
/// <param name="fontName">字体(仿宋,楷体,宋体,微软雅黑...与Excel主题字体相对应</param>
/// <param name="isAddBorderColor">是否增加边框颜色</param>
/// <param name="isItalic">是否将文字变为斜体</param>
/// <param name="isLineFeed">是否自动换行</param>
/// <param name="isAddCellBackground">是否增加单元格背景颜色</param>
/// <param name="fillPattern">填充图案样式(FineDots 细点SolidForeground立体前景isAddFillPattern=true时存在)</param>
/// <param name="cellBackgroundColor">单元格背景颜色当isAddCellBackground=true时存在</param>
/// <param name="fontColor">字体颜色</param>
/// <param name="underlineStyle">下划线样式(无下划线[None],单下划线[Single],双下划线[Double],会计用单下划线[SingleAccounting],会计用双下划线[DoubleAccounting]</param>
/// <param name="typeOffset">字体上标下标(普通默认值[None],上标[Sub],下标[Super]),即字体在单元格内的上下偏移量</param>
/// <param name="isStrikeout">是否显示删除线</param>
/// <returns></returns>
public HSSFCellStyle CreateStyle(HSSFWorkbook workbook, HorizontalAlignment hAlignment, VerticalAlignment vAlignment, short fontHeightInPoints, bool isAddBorder, short boldWeight, string fontName = "宋体", bool isAddBorderColor = true, bool isItalic = false, bool isLineFeed = false, bool isAddCellBackground = false, FillPattern fillPattern = FillPattern.NoFill, short cellBackgroundColor = HSSFColor.Yellow.Index, short fontColor = HSSFColor.Black.Index, FontUnderlineType underlineStyle =
FontUnderlineType.None, FontSuperScript typeOffset = FontSuperScript.None, bool isStrikeout = false)
{
HSSFCellStyle cellStyle = (HSSFCellStyle)workbook.CreateCellStyle(); //创建列头单元格实例样式
cellStyle.Alignment = hAlignment; //水平居中
cellStyle.VerticalAlignment = vAlignment; //垂直居中
cellStyle.WrapText = isLineFeed;//自动换行
//背景颜色,边框颜色,字体颜色都是使用 HSSFColor属性中的对应调色板索引关于 HSSFColor 颜色索引对照表详情参考https://www.cnblogs.com/Brainpan/p/5804167.html
//TODO引用了NPOI后可通过ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置FillPattern 为单元格背景色的填充样式
//TODO:十分注意要设置单元格背景色必须是FillForegroundColor和FillPattern两个属性同时设置否则是不会显示背景颜色
if (isAddCellBackground)
{
cellStyle.FillForegroundColor = cellBackgroundColor;//单元格背景颜色
cellStyle.FillPattern = fillPattern;//填充图案样式(FineDots 细点SolidForeground立体前景)
}
//是否增加边框
if (isAddBorder)
{
//常用的边框样式 None(没有),Thin(细边框,瘦的),Medium(中等),Dashed(虚线),Dotted(星罗棋布的),Thick(厚的),Double(双倍),Hair(头发)[上右下左顺序设置]
cellStyle.BorderBottom = BorderStyle.Thin;
cellStyle.BorderRight = BorderStyle.Thin;
cellStyle.BorderTop = BorderStyle.Thin;
cellStyle.BorderLeft = BorderStyle.Thin;
}
//是否设置边框颜色
if (isAddBorderColor)
{
//边框颜色[上右下左顺序设置]
cellStyle.TopBorderColor = HSSFColor.DarkGreen.Index;//DarkGreen(黑绿色)
cellStyle.RightBorderColor = HSSFColor.DarkGreen.Index;
cellStyle.BottomBorderColor = HSSFColor.DarkGreen.Index;
cellStyle.LeftBorderColor = HSSFColor.DarkGreen.Index;
}
/**
* 设置相关字体样式
*/
var cellStyleFont = (HSSFFont)workbook.CreateFont(); //创建字体
//假如字体大小只需要是粗体的话直接使用下面该属性即可
//cellStyleFont.IsBold = true;
cellStyleFont.Boldweight = boldWeight; //字体加粗
cellStyleFont.FontHeightInPoints = fontHeightInPoints; //字体大小
cellStyleFont.FontName = fontName;//字体(仿宋,楷体,宋体
cellStyleFont.Color = fontColor;//设置字体颜色
cellStyleFont.IsItalic = isItalic;//是否将文字变为斜体
cellStyleFont.Underline = underlineStyle;//字体下划线
cellStyleFont.TypeOffset = typeOffset;//字体上标下标
cellStyleFont.IsStrikeout = isStrikeout;//是否有删除线
cellStyle.SetFont(cellStyleFont); //将字体绑定到样式
return cellStyle;
}
}
}