NPOI导出Excel文件(自动生成文档,公式自定义)

直接上代码吧:简单易懂 

        /// <summary>
        /// 通过NPOI导出excel文件
        /// </summary>
        /// <param name="sFileFullName">excel文件全路径</param>
        /// <returns>是否导出成功(0:导出成功,其他:导出不成功)</returns>
        private int OutputExcelByNPOI(string sFileFullName)
        {
            //1.创建sheet页
            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("sheet1");
            //2.创建样式
            //左对齐样式
            ICellStyle cLeftLoakStyle = workbook.CreateCellStyle();
            cLeftLoakStyle.IsLocked = true;
            cLeftLoakStyle.WrapText = true;
            cLeftLoakStyle.Alignment = HorizontalAlignment.Left;
            cLeftLoakStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            //右对齐样式
            ICellStyle cRightLoakStyle = workbook.CreateCellStyle();
            cRightLoakStyle.CloneStyleFrom(cLeftLoakStyle);
            cRightLoakStyle.Alignment = HorizontalAlignment.Right;
            //不锁定的样式(左对齐):
            ICellStyle cUnLoakStyle = workbook.CreateCellStyle();
            cUnLoakStyle.CloneStyleFrom(cRightLoakStyle);
            cUnLoakStyle.IsLocked = false;
            //3.设置具体数据
            #region Part-I
            SetCellValue(sheet, 0, 0, "PartⅠ-Title_1 ", cLeftLoakStyle);
            int nCount = data.Count;
            int nCurrentRowIndx = 0;
            for (int i = 0; i < nCount; i++)
            {
                Kpidata data = kpidata[i];
                SetCellValue(sheet, ++nCurrentRowIndx, 1, string.Format("KPI-{0}", i + 1), cRightLoakStyle);
                SetCellValue(sheet, ++nCurrentRowIndx, 2, "Latitude:", cRightLoakStyle);
                SetCellValue(sheet, nCurrentRowIndx, 3, data.Latitude, cLeftLoakStyle);
                SetCellValue(sheet, nCurrentRowIndx, 4, "Weights:", cRightLoakStyle);
                SetCellValue(sheet, nCurrentRowIndx, 5, string.Format("{0}%", data.Weights), cLeftLoakStyle);
                SetCellValue(sheet, nCurrentRowIndx, 6, "DataSources:", cRightLoakStyle);
                SetCellValue(sheet, nCurrentRowIndx, 7, data.DataSources, cLeftLoakStyle);

                SetCellValue(sheet, ++nCurrentRowIndx, 2, "TargetValue:", cRightLoakStyle);
                SetCellValue(sheet, nCurrentRowIndx, 3, data.TargetValue, cLeftLoakStyle);

                SetCellValue(sheet, ++nCurrentRowIndx, 2, "EmployeeSummary :", cRightLoakStyle);
                SetCellValue(sheet, nCurrentRowIndx, 3, data.EmployeeSummary, cUnLoakStyle);
            }
            
            
            //4.设置自动列宽
            for (int i = 0; i < 8; i++)
            {
                sheet.AutoSizeColumn(i);
            }
            //5.设置sheet页保护
            sheet.ProtectSheet("");
            //6.本地保存文件
            FileStream fs = new FileStream(sFileFullName, FileMode.CreateNew);
            workbook.Write(fs);
            fs.Close();
            return 0;
        }
        #endregion
        /// <summary>
        /// 设置exce单元格数据
        /// </summary>
        /// <param name="sheet">目标单元格所在sheet页</param>
        /// <param name="nRowIndex">目标单元格行下标</param>
        /// <param name="nCellIndex">目标单元格列下标</param>
        /// <param name="sCellVaule">目标单元格值</param>
        /// <param name="cellStyle">目标单元格样式</param>
        /// <returns>是否设置成功(0:成功,其他:不成功)</returns>
        private int SetCellValue(ISheet sheet, int nRowIndex, int nCellIndex, string sCellVaule, ICellStyle cellStyle)
        {
            //相关参数合法性检查
            if (null == sheet || 0 > nRowIndex || 0 > nCellIndex || "" == sCellVaule)
            {
                return -1;
            }
            //得到目标单元格所在行,从而确定是是否需要新建行
            IRow row = sheet.GetRow(nRowIndex);
            ICell cell = null;
            if (null == row)
            {
                row = sheet.CreateRow(nRowIndex);
                cell = row.CreateCell(nCellIndex);
            }
            else
            {
                //得到目标单元格是否存在,从而确定是否需要新建单元格
                if (null == row.GetCell(nCellIndex))
                {
                    cell = row.CreateCell(nCellIndex);
                }
                else
                {
                    cell = row.GetCell(nCellIndex);
                }
            }
            //设置单元格样式
            cell.CellStyle = cellStyle;
            //设置单元格的值
            cell.SetCellValue(sCellVaule);
            //if (-1 != sCellVaule.IndexOf('\n'))//手动计算行,从未设置行高
            //{
            //    //string[] Lines = sCellVaule.Split('\n');
            //    //if (Lines.Length > sheet.DefaultRowHeight)
            //    //{
            //    //    row.Height = (short)(sheet.DefaultRowHeight * Lines.Length);
            //    //}
            //}
            return 0;
        }

代码如上,说明都在注释

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值