在c#中使用Interop访问3DPie图表

时间:2015-05-06 06:42:30

标签: visual-c#-express-2010

/ *我正在使用下面的代码创建一个3D饼图。它工作正常。该程序导出Excel文件。我现在想要发生的是禁用类别名称和值数据标签。我想将分隔符更改为新行。我该怎么办?有人可以帮我弄这个吗?请。非常感谢您的帮助。请参考下面的代码。 * /

private void btnGenerateChart_Click(object sender,EventArgs e){

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

object misValue = System.Reflection.Missing.Value;
int x = 2;

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);

// Create and select 1st sheet
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

try{
    SqlCommand compass = _SqlConnection._SqlCon.CreateCommand();
    compass.CommandText = ''sp_SelectRawData'';
    compass.CommandType = CommandType.StoredProcedure;
    _SqlConnection._SqlCon.Open();
    SqlDataReader dreader = compass.ExecuteReader();

    xlWorkSheet.Cells[1, 2] = ''Tag I - IV'';
    xlWorkSheet.Cells[1, 3] = ''Tag V'';
    xlWorkSheet.Cells[1, 4] = ''No Tag'';

    while(dreader.Read()){
        xlWorkSheet.Cells[x, 1] = dreader.ToString();
        xlWorkSheet.Cells[x, 2] = dreader.ToString();
        xlWorkSheet.Cells[x, 3] = dreader.ToString();
        xlWorkSheet.Cells[x, 4] = dreader.ToString();
    x++;
    }

    dreader.Close();
    _SqlConnection._SqlCon.Close();
}
catch(Exception ex){
    MessageBox.Show(ex.Message);
}

// Chart range is used to select cells
Excel.Range chartRange;

chartRange = xlWorkSheet.get_Range(''A1'', ''D'' + (x - 1));
chartRange.Columns.EntireColumn.AutoFit();

chartRange = xlWorkSheet.get_Range(''B2'', ''D'' + (x - 1));
chartRange.Columns.NumberFormat = ''_(#,##0.00_);_((#,##0.00);_(\''-\''??_);_(@_)'';

// Create and select 2nd sheet
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);

Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);

// Chart size
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 10, 750, 350);

Excel.Chart chartPage = myChart.Chart;

chartPage.HasLegend = true;
chartPage.ChartTitle.Text = ''Final IAS Provition'';

var xAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
xAxis.HasTitle = true;
xAxis.AxisTitle.Text = ''Scale Value'';

var yAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = ''Originating Unit'';

// Refer chart data source from the1st sheet
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
chartRange = xlWorkSheet.get_Range(''A1'', ''D'' + (x - 1));

// Go to sheet two
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);

// Set chart data source
chartPage.SetSourceData(chartRange, misValue);

chartPage.ApplyDataLabels();
chartPage.ChartType = Excel.XlChartType.xl3DPie;
chartPage.Rotation = 0;
chartPage.Perspective = Convert.ToInt16(0.1);

xlWorkBook.CheckCompatibility = false;
chartRange.Worksheet.Protect(Password: ''987654321'', AllowFormattingColumns: true, AllowFiltering: true);

xlWorkBook.SaveAs(yourpath + ''.xls'', Excel.XlFileFormat.xlWorkbookNormal, ''123'', misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

}

private void releaseObject(object obj){     尝试     {          System.Runtime.InteropServices.Marshal.ReleaseComObject(OBJ);         obj = null;     }     catch(Exception ex){     obj = null;     }     最后{     所以GC.Collect();     } }

0 个答案:

没有答案