用NPOI建立Excel、合併儲存格、設定儲存格樣式、邊框的方法

來源:互聯網
上載者:User

今天在做項目中,遇到使用代碼產生具有一定樣式的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下載.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.