Today, when I was working on a project, I used code to generate an Excel file with a certain style. I found a lot of information and finally solved the problem. In Excel, I set the format and merged cells. The following describes how to use the NPOI class library to Operate Excel.
1. First, generate an Excel file in the memory. The Code is as follows:
HSSFWorkbook book = new HSSFWorkbook ();
ISheet sheet = book. CreateSheet ("Sheet1 ");
2. Create the rows and columns in the newly created sheet. The Code is as follows:
Copy codeThe Code is as follows: IRow row = sheet. CreateRow (index); // The number of rows indicated by index
Row. HeightInPoints = 35; // The row Height.
ICell cell = row. CreateCell (0); // create the first column
Cell. SetCellValue ("set cell value ");
3. Set the font size, border, and merged cells of the cell style.
(1). Create a cell font style and size.
Copy codeThe Code is as follows: // <summary>
/// Obtain the Font Style
/// </Summary>
/// <Param name = "hssfworkbook"> Excel operation class </param>
/// <Param name = "fontname"> body name </param>
/// <Param name = "fontcolor"> font color </param>
/// <Param name = "fontsize"> font size </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). Set the format of data displayed in Cells
Copy codeThe Code is as follows: ICell cell = row. CreateCell (1 );
ICellStyle cellStyleNum = Excel. GetICellStyle (book );
IDataFormat formatNum = book. CreateDataFormat ();
CellStyleNum. DataFormat = formatNum. GetFormat ("0.00E + 00"); // set the cell format to scientific notation cell. CellStyle = cellStyleNum;
(3) create a cell border, background color, and alignment
Copy codeThe Code is as follows: // <summary>
/// Obtain the cell style
/// </Summary>
/// <Param name = "hssfworkbook"> Excel operation class </param>
/// <Param name = "font"> cell font </param>
/// <Param name = "fillForegroundColor"> color of the pattern </param>
/// <Param name = "fillPattern"> pattern </param>
/// <Param name = "fillBackgroundColor"> cell background </param>
/// <Param name = "ha"> vertical alignment </param>
/// <Param name = "va"> vertical alignment </param>
/// <Returns> </returns>
Public static ICellStyle GetCellStyle (HSSFWorkbook hssfworkbook, IFont font, HSSFColor fillForegroundColor, FillPatternType fillPattern, HSSFColor fillBackgroundColor, HorizontalAlignment ha, and 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 );
}
// Border
Cellstyle. BorderBottom = CellBorderType. THIN;
Cellstyle. BorderLeft = CellBorderType. THIN;
Cellstyle. BorderRight = CellBorderType. THIN;
Cellstyle. BorderTop = CellBorderType. THIN;
Return cellstyle;
}
(4). Merge Cells Copy codeThe Code is as follows: // <summary>
/// Merge Cells
/// </Summary>
/// <Param name = "sheet"> the sheet in which the cell to be merged is located </param>
/// <Param name = "rowstart"> Start row index </param>
/// <Param name = "rowend"> end row index </param>
/// <Param name = "colstart"> index of the Start column </param>
/// <Param name = "colend"> end column index </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. output the Excel file
FileStream stream = File. OpenWrite (@ "F:/test.xls ");;
Book. Write (stream );
Stream. Close ();
The above section uses NPOI to dynamically generate Excel rows and columns, as well as cell styles. For details, refer to the Demo to download.