本文整理汇总了C#中NPOI.HSSF.UserModel.HSSFWorkbook.NewCellStyle方法的典型用法代码示例。如果您正苦于以下问题:C# HSSFWorkbook.NewCellStyle方法的具体用法?C# HSSFWorkbook.NewCellStyle怎么用?C# HSSFWorkbook.NewCellStyle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NPOI.HSSF.UserModel.HSSFWorkbook
的用法示例。
在下文中一共展示了HSSFWorkbook.NewCellStyle方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Export
/// <summary>
/// 导出表定义。
/// </summary>
/// <param name="tables">表信息</param>
/// <param name="stream">IO流</param>
public void Export(IList<Table> tables, Stream stream)
{
if (tables == null)
{
throw new ArgumentNullException(nameof(tables));
}
if (tables.IsEmpty())
{
throw new ArgumentException("导出的表不能为空!", nameof(tables));
}
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
var workbook = new HSSFWorkbook();
// 标题单元格样式。
var tableCellStyle = workbook.NewCellStyle(fontCallback: font => font.FontHeightInPoints = 18, borderStyle: BorderStyle.None);
var titleCellStyle = workbook.NewCellStyle(
style =>
{
style.FillPattern = FillPattern.SolidForeground;
style.FillForegroundColor = IndexedColors.DarkBlue.Index;
}, font =>
{
font.Boldweight = 700;
font.FontHeightInPoints = 14;
font.Color = IndexedColors.White.Index;
});
var leftCellStyle = workbook.NewCellStyle(
style =>
{
style.Indention = 1;
style.Alignment = HorizontalAlignment.Left;
});
var centerCellStyle = workbook.NewCellStyle();
var hyperlinkCellStyle = workbook.NewCellStyle(
style =>
{
style.Indention = 1;
style.Alignment = HorizontalAlignment.Left;
}
, font =>
{
font.Color = IndexedColors.DarkBlue.Index;
font.Underline = FontUnderlineType.Single;
});
// 目录Sheet。
var bookmarkIndex = 1;
var bookmarkSheet = workbook.CreateSheet("目录");
var titleRow = bookmarkSheet.CreateRow(0);
titleRow.HeightInPoints = 32;
titleRow.CreateCell(0, titleCellStyle).SetCellValue("序号");
titleRow.CreateCell(1, titleCellStyle).SetCellValue("表名称");
titleRow.CreateCell(2, titleCellStyle).SetCellValue("描述");
titleRow.CreateCell(3, titleCellStyle).SetCellValue("备注");
bookmarkSheet.SetColumnWidth(0, 10 * 256);
bookmarkSheet.SetColumnWidth(1, 60 * 256);
bookmarkSheet.SetColumnWidth(2, 45 * 256);
bookmarkSheet.SetColumnWidth(3, 45 * 256);
// 首行冻结。
bookmarkSheet.CreateFreezePane(0, 1);
var schemas = tables.GroupBy(t => t.Schema);
foreach (var schema in schemas)
{
var rowIndex = 0;
var sheetName = string.IsNullOrWhiteSpace(schema.Key) ? "公共" : schema.Key;
var sheet = workbook.CreateSheet(sheetName);
// 添加表格定义。
foreach (var table in schema)
{
// 在目录Sheet中添加表的目录信息。
var bookmarkRow = bookmarkSheet.CreateRow(bookmarkIndex);
bookmarkRow.HeightInPoints = 22;
bookmarkRow.CreateCell(0, centerCellStyle).SetCellValue(bookmarkIndex++);
bookmarkRow.CreateCell(1, hyperlinkCellStyle).SetCellValue($"{table.Name}");
bookmarkRow.CreateCell(2, leftCellStyle).SetCellValue(table.Comments);
bookmarkRow.CreateCell(3, leftCellStyle).SetCellValue("");
bookmarkRow.Cells[1].Hyperlink = new HSSFHyperlink(HyperlinkType.Document)
{
Address = $"#{sheetName}!A{rowIndex + 3}"
};
//.........这里部分代码省略.........