本文整理匯總了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}"
};
//.........這裏部分代碼省略.........