• NPOI根据模板生成chart图表导出Excel


    导入NPOI的全部dll。

    因为NPOI的API里面还没有对于Chart图表方面的操作,所以只能根据提示做好的图表作为模板,修改数据源的方法来改变图表。

    注意:NPOI要用2003版以下的excel才能更好的支持,对才2007版以上的,导出来图表会变形。

    一、制作好一个excel模板

    二、读取模板填充数据源

    1.给NPOI扩展两个方法,以免下面代码中进行过多的判断

     /// <summary>
        /// 扩展方法
        /// </summary>
      public static  class ExtFunction
        {
          public static ICell Cell(this IRow row,int index)
          {
              ICell cell = row.GetCell(index);
              if (cell == null)
              {
                  cell = row.CreateCell(index);
              }
              return cell;
                
          }
          public static IRow Row(this ISheet sheet,int index)
          {
              IRow row = sheet.GetRow(index);
              if (row == null)
              {
                  row = sheet.CreateRow(index);
              }
              return row;
          }
        }

    2.主要代码:

      //导出excel
            private void btnExportExcel_Click(object sender, EventArgs e)
            {
    
                try
                {
                    string filePath = string.Empty;
                    HSSFWorkbook workbook = null;
                    ISheet sheet1 = null;
                    IRow row = null;
                    int nowRowNum = 1;//当前行2,表头第一行
                    if (diskInfos.Count < 1)
                    {
                        MessageBox.Show("没有数据");
                        return;
                    }
                    //选择文件保存路径
                    filePath = getFilePath();
                    if (filePath == "")
                    {
                        return;
                    }
                    //模板路径
                    string excelTempPath = System.Environment.CurrentDirectory + "/temp.xls";
                    //读取Excel模板
                    using (FileStream fs = new FileStream(excelTempPath, FileMode.Open, FileAccess.Read))
                    {
                        workbook = new HSSFWorkbook(fs);
                    }
                    //获取sheet1
                    sheet1 = workbook.GetSheetAt(0);
    
                    for (int i = 0; i < diskInfos.Count; i++)
                    {
                        //获取当前行
                        row = sheet1.Row(nowRowNum);
                        //给行的单元格填充数据
                        row.Cell(0).SetCellValue(diskInfos[i].IP);
                        row.Cell(1).SetCellValue(diskInfos[i].DiskName);
                        row.Cell(2).SetCellValue(diskInfos[i].FreeSize);
                        row.Cell(3).SetCellValue(diskInfos[i].AllSize);
                        row.Cell(5).SetCellValue(diskInfos[i].Remark);
                        nowRowNum++;
                    }
    
                    //保存文件
                    using (Stream stream = File.OpenWrite(filePath))
                    {
                        workbook.Write(stream);
                    }
                    //弹出消息框
                    MsgForm msgForm = new MsgForm(filePath);
                    msgForm.ShowDialog();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    
            }
            //弹出选择保存的路径
            public string getFilePath()
            {
                string filePath = "";
                SaveFileDialog sfd=new SaveFileDialog ();
                //文件类型限制
                sfd.Filter = "Files|*.xls";
                //默认文件名
                sfd.FileName = "DiskReport.xls";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    filePath = sfd.FileName;
                }
    
                return filePath;
            }

     功能为查询公司几个服务器的磁盘使用情况,最后导出的excel表为:

  • 相关阅读:
    基数排序
    kt-Mapper 笔记
    归并排序
    快速排序
    第十一天——递归(五)
    第十天——闭包(一)
    第八天——函数的嵌套以及gloabal、nonlocal(三)(重点:执行过程)
    第八天——函数的作用域(二)
    第八天——函数的动态参数(一)
    第七天——函数的参数(二)
  • 原文地址:https://www.cnblogs.com/wei325/p/5454493.html
Copyright © 2020-2023  润新知