1. 程式人生 > >NPOI導出excel使用

NPOI導出excel使用

tab .get code sfc ram tex string index width

1.在Page_Load或者click下使用ExportExcel方法。

 protected void Page_Load(object sender, EventArgs e)
    {
        ExportExcel(dt, "成型條件表", "成型條件表");//dt是DataTable類型的數據源,第二個參數設置導出的文件名,第三個參數設置工作簿名
    }

2.ExportExcel方法,其中setCellStyle方法看第三條。setCellStyle方法用來設置單元格樣式並賦值

  public void ExportExcel(DataTable dt, string
strFileName, string strSheetName) { HSSFWorkbook book = new HSSFWorkbook();//創建一個新的excel HSSFSheet sheet = book.CreateSheet("Sheet1") as HSSFSheet; //創建sheet頁 //第一行 IRow dataRow = sheet.CreateRow(0);//創建一行 0是行號表示第一行 dataRow.HeightInPoints = 20;//設置行高 setCellStyle(book, sheet, dataRow.CreateCell(0
), "模號:", 0, 0, 0, 0);//dataRow.CreateCell(0)創建一個單元格,0表示列號表示第一列 setCellStyle(book, sheet, dataRow.CreateCell(1), dt.Rows[0][0].ToString(), 0, 0, 1, 3); setCellStyle(book, sheet, dataRow.CreateCell(4), "圖號:", 0, 0, 4, 7); setCellStyle(book, sheet, dataRow.CreateCell(8), dt.Rows[0][1].ToString(), 0
, 0, 8, 13); setCellStyle(book, sheet, dataRow.CreateCell(14), "機臺:", 0, 0, 14, 17); setCellStyle(book, sheet, dataRow.CreateCell(18), dt.Rows[0][2].ToString(), 0, 0, 18, 25); //設置列寬 for (int i = 0; i < 26; i++) { sheet.SetColumnWidth(i, 5 * 256);//第i列列寬 } //第二行 dataRow = sheet.CreateRow(1); dataRow.HeightInPoints = 20; setCellStyle(book, sheet, dataRow.CreateCell(0), "原料:", 1, 1, 0, 0); setCellStyle(book, sheet, dataRow.CreateCell(1), dt.Rows[0][3].ToString(), 1, 1, 1, 7); setCellStyle(book, sheet, dataRow.CreateCell(8), "色號:", 1, 1, 8, 9); setCellStyle(book, sheet, dataRow.CreateCell(10), dt.Rows[0][4].ToString(), 1, 1, 10, 13); setCellStyle(book, sheet, dataRow.CreateCell(14), "日期:", 1, 1, 14, 16); setCellStyle(book, sheet, dataRow.CreateCell(17), dt.Rows[0][5].ToString(), 1, 1, 17, 20); setCellStyle(book, sheet, dataRow.CreateCell(21), "階段:", 1, 1, 21, 22); setCellStyle(book, sheet, dataRow.CreateCell(23), dt.Rows[0][6].ToString(), 1, 1, 23, 25); MemoryStream ms = new MemoryStream(); book.Write(ms); Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8))); Response.BinaryWrite(ms.ToArray()); Response.End(); book = null; ms.Close(); ms.Dispose(); }

3.setCellStyle方法

 /// <summary>
    /// 設置單元格樣式並賦值
    /// </summary>
    /// <param name="book">Excel操作類</param>
    /// <param name="sheet">工作表</param>
    ///<param name="dataCell">單元格</param>
    /// <param name="value">單元格值</param>
    /// <param name="startRow">合並單元格起始行</param>
    /// <param name="stopRow">合並單元格結束行</param>
    /// <param name="startCol">合並單元格起始列</param>
    /// <param name="stopCol">合並單元格結束列</param>
    /// <returns></returns>
    public void setCellStyle(HSSFWorkbook book, HSSFSheet sheet, ICell dataCell, string value, int startRow, int stopRow, int startCol, int stopCol)
    {
        DB db = new DB();//此類中包含一個判斷該數據是否是數字格式的方法,由於跟本文主題無關就不展示相應代碼
        ICellStyle style = book.CreateCellStyle();//創建單元格樣式實例
        //文字居中
        style.Alignment = HorizontalAlignment.Center;//水平居中
        style.VerticalAlignment = VerticalAlignment.Center;//垂直居中
        //自動換行
        style.WrapText = true;

      //設置文字樣式
      IFont font = book.CreateFont();
      font.FontName = "宋體";
      font.Boldweight = short.MaxValue;
      font.FontHeightInPoints = 28;
      font.Color = NPOI.HSSF.Util.HSSFColor.Grey50Percent.Index;
      style.SetFont(font);//將字體樣式加到樣式對象中去。

//設置背景色
        HSSFPalette palette = book.GetCustomPalette(); //調色板實例
        palette.SetColorAtIndex((short)63, (byte)255, (byte)255, (byte)255);//自定義顏色加入調色板 第一個參數:設置調色板新增顏色的編號,自已設置即可,取值範圍8-64,第二、第三、第四個參數,組成RGB值
        HSSFColor hssfColor = palette.FindColor((byte)255, (byte)255, (byte)255);//FindColor直接找到HSSFColor實例
        style.FillForegroundColor = hssfColor.Indexed;
        style.FillPattern = FillPattern.SolidForeground;
        dataCell.CellStyle = style;
        //單元格值類型判斷   作者這裏的數據都是string類型,想要將string類型的數字轉成int類型的數字
        if (value.Length>0&&db.isNumber(value) == true)
        {
            int intValue = 0;
            int.TryParse(value, out intValue);
            dataCell.SetCellValue(intValue);//為單元格賦值
        }
        else
        {
            dataCell.SetCellValue(value);
        }
       
        //合並單元格,設置邊框
        CellRangeAddress region = new CellRangeAddress(startRow, stopRow, startCol, stopCol);
        sheet.AddMergedRegion(region);
        sheet.SetEnclosedBorderOfRegion(region, NPOI.SS.UserModel.BorderStyle.Thin, NPOI.HSSF.Util.HSSFColor.Black.Index);
//此方法可用於合並單元格設置邊框,第二個參數是邊框類型,第三個參數是邊框顏色 }

NPOI導出excel使用