(1). Create the style and size of cell fonts
/// <summary>
/// Get font style
/// </summary>
/// <param name="hssfworkbook">Excel operation class</param>
/// <param name="fontname">Font 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 = ();
if ((fontfamily))
{
= fontfamily;
}
if (fontcolor != null)
{
= ();
}
= true;
= (short)fontsize;
return font1;
}
(2). Set the format of displaying data in the cell
ICell cell = (1);
ICellStyle cellStyleNum = (book);
IDataFormat formatNum = ();
= ("0.00E+00");//Set the format of the cell to scientific notation = cellStyleNum;
(3). Create the border, background color, and alignment of the cell
/// <summary>
/// Get cell style
/// </summary>
/// <param name="hssfworkbook">Excel operation class</param>
/// <param name="font">cell font</param>
/// <param name="fillForegroundColor">Color of pattern</param>
/// <param name="fillPattern">Pattern Style</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, VerticalAlignment va)
{
ICellStyle cellstyle = ();
= fillPattern;
= ha;
= va;
if (fillForegroundColor != null)
{
= ();
}
if (fillBackgroundColor != null)
{
= ();
}
if (font != null)
{
(font);
}
//There are borders
= ;
= ;
= ;
= ;
return cellstyle;
}
(4). Merge cells
/// <summary>
/// Merge cells
/// </summary>
/// <param name="sheet">The sheet where the cell is to be merged</param>
/// <param name="rowstart">Index of the start line</param>
/// <param name="rowend">Index of end row</param>
/// <param name="colstart">Index of the start column</param>
/// <param name="colend">Index of end column</param>
public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
{
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
(cellRangeAddress);
}
4. Export Excel file
Copy the codeThe code is as follows:
/// <summary>
/// Get font style
/// </summary>
/// <param name="hssfworkbook">Excel operation class</param>
/// <param name="fontname">Font 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 = ();
if ((fontfamily))
{
= fontfamily;
}
if (fontcolor != null)
{
= ();
}
= true;
= (short)fontsize;
return font1;
}
(2). Set the format of displaying data in the cell
Copy the codeThe code is as follows:
ICell cell = (1);
ICellStyle cellStyleNum = (book);
IDataFormat formatNum = ();
= ("0.00E+00");//Set the format of the cell to scientific notation = cellStyleNum;
(3). Create the border, background color, and alignment of the cell
Copy the codeThe code is as follows:
/// <summary>
/// Get cell style
/// </summary>
/// <param name="hssfworkbook">Excel operation class</param>
/// <param name="font">cell font</param>
/// <param name="fillForegroundColor">Color of pattern</param>
/// <param name="fillPattern">Pattern Style</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, VerticalAlignment va)
{
ICellStyle cellstyle = ();
= fillPattern;
= ha;
= va;
if (fillForegroundColor != null)
{
= ();
}
if (fillBackgroundColor != null)
{
= ();
}
if (font != null)
{
(font);
}
//There are borders
= ;
= ;
= ;
= ;
return cellstyle;
}
(4). Merge cells
Copy the codeThe code is as follows:
/// <summary>
/// Merge cells
/// </summary>
/// <param name="sheet">The sheet where the cell is to be merged</param>
/// <param name="rowstart">Index of the start line</param>
/// <param name="rowend">Index of end row</param>
/// <param name="colstart">Index of the start column</param>
/// <param name="colend">Index of end column</param>
public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
{
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
(cellRangeAddress);
}
4. Export Excel file