星空网 > 软件开发 > ASP.net

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

导入NPOI的全部dll。

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

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

一、制作好一个excel模板

NPOI根据模板生成chart图表导出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表为:

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

 




原标题:NPOI根据模板生成chart图表导出Excel

关键词:excel

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok开通小黄车:https://www.goluckyvip.com/tag/81842.html
tiktok短剧:https://www.goluckyvip.com/tag/81843.html
tiktok上的中国网红:https://www.goluckyvip.com/tag/81844.html
tiktok股权结构图:https://www.goluckyvip.com/tag/81845.html
tiktok ads manager:https://www.goluckyvip.com/tag/81846.html
download tiktok lite:https://www.goluckyvip.com/tag/81847.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流