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


    https://www.cnblogs.com/wei325/p/5454493.html

    导入NPOI的全部dll。

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

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

    一、制作好一个excel模板

    二、读取模板填充数据源

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

    复制代码
    ///


    /// 扩展方法
    ///

    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表为:

  • 相关阅读:
    常见网络攻击手段原理分析
    admins.py总结比较,转
    django的表与表之间的关系详细讲解
    django中的@login_required
    安装指定版本的第三方库
    在django中使用logging
    django的manytomany总结
    manyToManyField理解和用法
    django的多对一,一对一,多对多关系
    python 的os的总结
  • 原文地址:https://www.cnblogs.com/ellafive/p/15976659.html
Copyright © 2020-2023  润新知