NPOI导出excel使用

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);
//此方法可用于合并单元格设置边框,第二个参数是边框类型,第三个参数是边框颜色 }

 

posted @ 2017-08-23 15:43  Deer家的鹿  阅读(358)  评论(0编辑  收藏  举报