程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 使用C#和Excel進行報表開發(3)(5)

使用C#和Excel進行報表開發(3)(5)

編輯:關於C語言

最後是完整代碼:

private void button4_Click(object sender, EventArgs e)
{
  try
  {
    ThisApplication = new Excel.Application();
    m_objBooks = (Excel.Workbooks)ThisApplication.Workbooks;
    ThisWorkbook = (Excel._Workbook)(m_objBooks.Add(Type.Missing));
  ThisApplication.DisplayAlerts = false;
  this.DeleteSheet();
    this.AddDatasheet();
    this.LoadData();
  CreateChart();
  ThisWorkbook.SaveAs("z:\\Book2.xls", Type.Missing, Type.Missing,
          Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
  finally
  {
    ThisWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
    ThisApplication.Workbooks.Close();
  ThisApplication.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisWorkbook);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisApplication);
    ThisWorkbook = null;
    ThisApplication = null;
    GC.Collect();
    this.Close();
  }
  }
  Excel.Application ThisApplication = null;
Excel.Workbooks m_objBooks = null;
Excel._Workbook ThisWorkbook = null;
  Excel.Worksheet xlSheet = null;
  /**//// <summary>
/// 用生成的隨機數作數據
/// </summary>
private void LoadData()
{
  Random ran = new Random();
  for (int i = 1; i <= 12; i++)
  {
    xlSheet.Cells[i, 1] = i.ToString() + "月";
    xlSheet.Cells[i, 2] = ran.Next(2000).ToString();
  }
}
/**//// <summary>
/// 刪除多余的Sheet
/// </summary>
private void DeleteSheet()
{
  foreach (Excel.Worksheet ws in ThisWorkbook.Worksheets)
    if (ws != ThisApplication.ActiveSheet)
    {
      ws.Delete();
    }
  foreach (Excel.Chart cht in ThisWorkbook.Charts)
    cht.Delete();

}
/**//// <summary>
/// 創建一個Sheet,用來存數據
/// </summary>
private void AddDatasheet()
{
  xlSheet = (Excel.Worksheet)ThisWorkbook.
    Worksheets.Add(Type.Missing, ThisWorkbook.ActiveSheet,
    Type.Missing, Type.Missing);
  xlSheet.Name = "數據";
}
/**//// <summary>
/// 創建統計圖
/// </summary>
private void CreateChart()
{
  Excel.Chart xlChart = (Excel.Chart)ThisWorkbook.Charts.
    Add(Type.Missing, xlSheet, Type.Missing, Type.Missing);
  Excel.Range cellRange = (Excel.Range)xlSheet.Cells[1, 1];
  xlChart.ChartWizard(cellRange.CurrentRegion,
    Excel.XlChartType.xl3DColumn, Type.Missing,
    Excel.XlRowCol.xlColumns,1, 0, true ,
    "訪問量比較(dahuzizyd.cnblogs.com)", "月份", "訪問量",
    "");
  xlChart.Name = "統計";
  Excel.ChartGroup grp = (Excel.ChartGroup)xlChart.ChartGroups(1);
grp.GapWidth = 20;
  grp.VaryByCategorIEs = true;
  Excel.SerIEs s = (Excel.Series)grp.SerIEsCollection(1);
  s.BarShape = XlBarShape.xlCylinder;
  s.HasDataLabels = true;
  xlChart.Legend.Position = XlLegendPosition.xlLegendPositionTop;
  xlChart.ChartTitle.Font.Size = 24;
  xlChart.ChartTitle.Shadow = true;
  xlChart.ChartTitle.Border.LineStyle = Excel.XlLineStyle.xlContinuous;

  Excel.Axis valueAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlValue, XlAxisGroup.xlPrimary);
  valueAxis.AxisTitle.OrIEntation = -90;
  Excel.Axis categoryAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
  categoryAxis.AxisTitle.Font.Name = "MS UI Gothic";
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved