2013-10-23 538 views
2

我必須將數據從數據表格導出到excel表格,並且還需要在同一張表格中顯示折線圖。我的問題是我怎麼能顯示數據&圖表在同一張表。如何使用NPOI庫生成折線圖以及excel表格中的數據使用NPOI庫c#

我使用NPOI庫版本1.2.5.0 & DOTNET 2.0我有管理導出數據使用NPOI圖書館這樣的方式,從數據表中出類拔萃。這裏是我的示例代碼。

class Program 
    { 
     static void Main(string[] args) 
     { 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("First Name", typeof(string)); 
      dt.Columns.Add("Last Name", typeof(string)); 
      dt.Columns.Add("Salary", typeof(double)); 

      DataRow dr = null; 
      dr = dt.NewRow(); 
      dr[0] = "Konna"; 
      dr[1] = "Lombard"; 
      dr[2] = "3000"; 
      dt.Rows.Add(dr); 

      dr = dt.NewRow(); 
      dr[0] = "Tunip"; 
      dr[1] = "Mansar"; 
      dr[2] = "4000"; 
      dt.Rows.Add(dr); 

      dr = dt.NewRow(); 
      dr[0] = "Dobby"; 
      dr[1] = "Bhell"; 
      dr[2] = "5000"; 
      dt.Rows.Add(dr); 

      Utility.Export(dt, "Result"); 

     } 
    } 

public static class Utility 
    { 
     public static string GetParentDirectory() 
     { 
      System.IO.DirectoryInfo myDirectory = new DirectoryInfo(Environment.CurrentDirectory); 
      return myDirectory.Parent.Parent.FullName; 
     } 

     public static void Export(DataTable dt,string strSheetName) 
     { 
      try 
      { 
       HSSFWorkbook workbook = new HSSFWorkbook(); 
       HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(strSheetName); 
       HSSFRow headerRow = (HSSFRow) sheet.CreateRow(0); 

       IFont font = workbook.CreateFont(); 
       font.FontHeightInPoints = 14; 
       font.FontName = "Calibri"; 
       font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD; 

       ICell titleCell = headerRow.CreateCell(0); 
       titleCell.SetCellValue("Daily Finished Job History " + DateTime.Now.ToString("dd/MM/yyyy")); 
       titleCell.CellStyle = workbook.CreateCellStyle(); 
       titleCell.CellStyle.SetFont(font); 
       sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dt.Columns.Count)); 

       int rowIndex = 2; 
       HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex); 
       foreach (DataColumn column in dt.Columns) 
       { 
        font = workbook.CreateFont(); 
        font.FontHeightInPoints = 11; 
        font.FontName = "Calibri"; 
        font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD; 

        titleCell = dataRow.CreateCell(column.Ordinal); 
        titleCell.SetCellValue(column.ColumnName); 
        titleCell.CellStyle = workbook.CreateCellStyle(); 
        titleCell.CellStyle.SetFont(font); 

        sheet.AutoSizeColumn(column.Ordinal); 
       } 
       rowIndex = 3; 

       foreach (DataRow row in dt.Rows) 
       { 
        dataRow = (HSSFRow)sheet.CreateRow(rowIndex); 

        foreach (DataColumn column in dt.Columns) 
        { 
         font = workbook.CreateFont(); 
         font.FontHeightInPoints = 11; 
         font.FontName = "Calibri"; 
         font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.NORMAL; 

         titleCell = dataRow.CreateCell(column.Ordinal); 
         titleCell.SetCellValue(row[column].ToString()); 
         titleCell.CellStyle = workbook.CreateCellStyle(); 
         titleCell.CellStyle.SetFont(font); 

         sheet.AutoSizeColumn(column.Ordinal); 
        } 

        rowIndex++; 
       } 



       string strParentDirectory = GetParentDirectory(); 
       strParentDirectory = strParentDirectory + "\\Data"; 
       if (!Directory.Exists(strParentDirectory)) 
       { 
        Directory.CreateDirectory(strParentDirectory); 
       } 
       string strFileName = strParentDirectory + "\\DailyFinishedJobHistory_" + DateTime.Now.ToString("yyyyMMdd")+".xls"; 
       if (File.Exists(strFileName)) 
       { 
        File.Delete(strFileName); 
       } 
       FileStream file = new FileStream(strFileName, FileMode.Create); 
       workbook.Write(file); 

       file.Close(); 
       headerRow = null; 
       sheet = null; 
       workbook = null; 
      } 
      catch (Exception ex) 
      { 
       //MessageBox.Show(ex.Message); 
      } 
      finally 
      { 
       dt.Dispose(); 
      } 
     } 
    } 

現在我的Excel數據看起來像 enter image description here

,但我需要產生這種Excel文件,其中數據&圖表都將在那裏在同一張紙上。這裏是屏幕截圖。

enter image description here

回答

0

線型圖將在NPOI 2.1的支持。它將在2014年6月發佈

0

我認爲下面的代碼可以幫助你解決問題。

private void btnRun_Click(object sender, System.EventArgs e) 
     { 
      Workbook workbook = new Workbook(); 

      //Initailize worksheet 
      workbook.CreateEmptySheets(1); 
      Worksheet sheet = workbook.Worksheets[0]; 
      sheet.Name = "Chart data"; 
      sheet.GridLinesVisible = false; 

      //Writes chart data 
      CreateChartData(sheet); 
      //Add a new chart worsheet to workbook 
      Chart chart = sheet.Charts.Add(); 
      if (checkBox1.Checked) 
      { 
       chart.ChartType = ExcelChartType.Line3D; 
      } 
      else 
      { 
       chart.ChartType = ExcelChartType.Line; 
      } 

      //Set region of chart data 
      chart.DataRange = sheet.Range["A1:E5"]; 

      //Set position of chart 
      chart.LeftColumn = 1; 
      chart.TopRow = 6; 
      chart.RightColumn = 11; 
      chart.BottomRow = 29; 


      //Chart title 
      chart.ChartTitle = "Sales market by country"; 
      chart.ChartTitleArea.IsBold = true; 
      chart.ChartTitleArea.Size = 12; 

      chart.PrimaryCategoryAxis.Title = "Month"; 
      chart.PrimaryCategoryAxis.Font.IsBold = true; 
      chart.PrimaryCategoryAxis.TitleArea.IsBold = true; 

      chart.PrimaryValueAxis.Title = "Sales(in Dollars)"; 
      chart.PrimaryValueAxis.HasMajorGridLines = false; 
      chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90; 
      chart.PrimaryValueAxis.MinValue = 1000; 
      chart.PrimaryValueAxis.TitleArea.IsBold = true; 

      foreach (Charts.ChartSerie cs in chart.Series) 
      { 
       cs.Format.Options.IsVaryColor = true; 
       cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true; 

       if (!checkBox1.Checked) 
        cs.DataFormat.MarkerStyle = ChartMarkerType.Circle; 
      } 

      chart.PlotArea.Fill.Visible = false; 

      chart.Legend.Position = LegendPositionType.Top; 
      workbook.SaveToFile("Sample.xls"); 
      ExcelDocViewer(workbook.FileName); 
     } 

     private void CreateChartData(Worksheet sheet) 
     { 
      //Country 
      sheet.Range["A1"].Value = "Country"; 
      sheet.Range["A2"].Value = "Cuba"; 
      sheet.Range["A3"].Value = "Mexico"; 
      sheet.Range["A4"].Value = "France"; 
      sheet.Range["A5"].Value = "German"; 

      //Jun 
      sheet.Range["B1"].Value = "Jun"; 
      sheet.Range["B2"].NumberValue = 3300; 
      sheet.Range["B3"].NumberValue = 2300; 
      sheet.Range["B4"].NumberValue = 4500; 
      sheet.Range["B5"].NumberValue = 6700; 

      //Jul 
      sheet.Range["C1"].Value = "Jul"; 
      sheet.Range["C2"].NumberValue = 7500; 
      sheet.Range["C3"].NumberValue = 2900; 
      sheet.Range["C4"].NumberValue = 2300; 
      sheet.Range["C5"].NumberValue = 4200; 

      //Aug 
      sheet.Range["D1"].Value = "Aug"; 
      sheet.Range["D2"].NumberValue = 7700; 
      sheet.Range["D3"].NumberValue = 6900; 
      sheet.Range["D4"].NumberValue = 8400; 
      sheet.Range["D5"].NumberValue = 4200; 

      //Sep 
      sheet.Range["E1"].Value = "Sep"; 
      sheet.Range["E2"].NumberValue = 8000; 
      sheet.Range["E3"].NumberValue = 7200; 
      sheet.Range["E4"].NumberValue = 8100; 
      sheet.Range["E5"].NumberValue = 5600; 

      //Style 
      sheet.Range["A1:E1"].Style.Font.IsBold = true; 
      sheet.Range["A2:E2"].Style.KnownColor = ExcelColors.LightYellow; 
      sheet.Range["A3:E3"].Style.KnownColor = ExcelColors.LightGreen1; 
      sheet.Range["A4:E4"].Style.KnownColor = ExcelColors.LightOrange; 
      sheet.Range["A5:E5"].Style.KnownColor = ExcelColors.LightTurquoise; 

      //Border 
      sheet.Range["A1:E5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(0, 0, 128); 
      sheet.Range["A1:E5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin; 
      sheet.Range["A1:E5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(0, 0, 128); 
      sheet.Range["A1:E5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin; 
      sheet.Range["A1:E5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(0, 0, 128); 
      sheet.Range["A1:E5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin; 
      sheet.Range["A1:E5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(0, 0, 128); 
      sheet.Range["A1:E5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin; 

      sheet.Range["B2:D5"].Style.NumberFormat = "\"$\"#,##0"; 
     } 

在使用上述代碼之前,您需要添加Spire.Xls庫。