手机
当前位置:查字典教程网 >编程开发 >C#教程 >用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法
用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法
摘要:今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,Excel中格式的设置,以及单元格的合并等等。下面就...

今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,Excel中格式的设置,以及单元格的合并等等。下面就介绍下,使用NPOI类库操作Excel的方法。

1.首先我们先在内存中生成一个Excel文件,代码如下:

HSSFWorkbook book = new HSSFWorkbook();

ISheet sheet = book.CreateSheet("Sheet1");

2.然后在新创建的sheet里面,创建我们的行和列,代码如下:

复制代码 代码如下:

IRow row = sheet.CreateRow(index);//index代表多少行

row.HeightInPoints = 35;//行高

ICell cell = row.CreateCell(0);//创建第一列

cell.SetCellValue(“设置单元格的值”);

3.设置单元格的样式已经字体大小,边框,以及合并单元格

(1).创建单元格字体的样式及大小

复制代码 代码如下:

/// <summary>

/// 获取字体样式

/// </summary>

/// <param name="hssfworkbook">Excel操作类</param>

/// <param name="fontname">字体名</param>

/// <param name="fontcolor">字体颜色</param>

/// <param name="fontsize">字体大小</param>

/// <returns></returns>

public static IFont GetFontStyle(HSSFWorkbook hssfworkbook, string fontfamily, HSSFColor fontcolor, int fontsize)

{

IFont font1 = hssfworkbook.CreateFont();

if (string.IsNullOrEmpty(fontfamily))

{

font1.FontName = fontfamily;

}

if (fontcolor != null)

{

font1.Color = fontcolor.GetIndex();

}

font1.IsItalic = true;

font1.FontHeightInPoints = (short)fontsize;

return font1;

}

(2).设置单元格内显示数据的格式

复制代码 代码如下:

ICell cell = row.CreateCell(1);

ICellStyle cellStyleNum = Excel.GetICellStyle(book);

IDataFormat formatNum = book.CreateDataFormat();

cellStyleNum.DataFormat = formatNum.GetFormat("0.00E+00");//设置单元格的格式为科学计数法cell.CellStyle = cellStyleNum;

(3).创建单元格的边框,背景颜色,以及对齐方式

复制代码 代码如下:

/// <summary>

/// 获取单元格样式

/// </summary>

/// <param name="hssfworkbook">Excel操作类</param>

/// <param name="font">单元格字体</param>

/// <param name="fillForegroundColor">图案的颜色</param>

/// <param name="fillPattern">图案样式</param>

/// <param name="fillBackgroundColor">单元格背景</param>

/// <param name="ha">垂直对齐方式</param>

/// <param name="va">垂直对齐方式</param>

/// <returns></returns>

public static ICellStyle GetCellStyle(HSSFWorkbook hssfworkbook, IFont font, HSSFColor fillForegroundColor, FillPatternType fillPattern, HSSFColor fillBackgroundColor, HorizontalAlignment ha, VerticalAlignment va)

{

ICellStyle cellstyle = hssfworkbook.CreateCellStyle();

cellstyle.FillPattern = fillPattern;

cellstyle.Alignment = ha;

cellstyle.VerticalAlignment = va;

if (fillForegroundColor != null)

{

cellstyle.FillForegroundColor = fillForegroundColor.GetIndex();

}

if (fillBackgroundColor != null)

{

cellstyle.FillBackgroundColor = fillBackgroundColor.GetIndex();

}

if (font != null)

{

cellstyle.SetFont(font);

}

//有边框

cellstyle.BorderBottom = CellBorderType.THIN;

cellstyle.BorderLeft = CellBorderType.THIN;

cellstyle.BorderRight = CellBorderType.THIN;

cellstyle.BorderTop = CellBorderType.THIN;

return cellstyle;

}

(4).合并单元格

复制代码 代码如下:

/// <summary>

/// 合并单元格

/// </summary>

/// <param name="sheet">要合并单元格所在的sheet</param>

/// <param name="rowstart">开始行的索引</param>

/// <param name="rowend">结束行的索引</param>

/// <param name="colstart">开始列的索引</param>

/// <param name="colend">结束列的索引</param>

public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)

{

CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);

sheet.AddMergedRegion(cellRangeAddress);

}

4.将Excel文件输出

FileStream stream = File.OpenWrite(@"F:/test.xls"); ;

book.Write(stream);

stream.Close();

以上就是使用NPOI动态生成Excel的行和列,以及单元格的样式,具体的可以参考Demo下载.

【用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法】相关文章:

C# 设置系统日期格式的方法

使用DateTime的ParseExact方法实现特殊日期时间的方法详解

基于c#图像灰度化、灰度反转、二值化的实现方法详解

C# 改变无边框窗体尺寸大小的方法

c# asp .net 动态创建sql数据库表的方法

用C#实现启动另一程序的方法实例

c#创建Graphics对象的三种方法

C# WINFORM 强制让窗体获得焦点的方法代码

C#访问应用程序配置文件的方法

使用C#开发Socket通讯的方法

精品推荐
分类导航