NPOI根據模板生成chart圖表導出Excel

導入NPOI的所有dll。服務器

由於NPOI的API裏面尚未對於Chart圖表方面的操做,因此只能根據提示作好的圖表做爲模板,修改數據源的方法來改變圖表。this

注意:NPOI要用2003版如下的excel才能更好的支持,對才2007版以上的,導出來圖表會變形。spa

1、製做好一個excel模板excel

2、讀取模板填充數據源code

1.給NPOI擴展兩個方法,以避免下面代碼中進行過多的判斷orm

 /// <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.主要代碼:blog

  //導出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表爲:get

相關文章
相關標籤/搜索