2017-02-22 199 views
1

我對c#和Visual Studio有點新,我想根據位於其他位置的csv文件的值繪製圖形。我在c#中使用ChartObjects和ChartWizard屬性來創建圖形。繪製的圖應該是我提供的列範圍,在Y軸和X軸應該有當前行號(1,2,3,4等)。不過,我的默認圖形將X軸作爲我csv文件中的第一列。如果我也爲X軸指定了一個範圍,那麼它會正確繪圖,但是如何獲得當前的行號?更改圖形的x軸值c#

我經歷了很多文章和問題,即使在堆棧溢出,但似乎沒有幫助。

這裏是我的代碼片段:

Microsoft.Office.Interop.Excel.Application xlexcel; 
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 

object misValue = System.Reflection.Missing.Value; 
xlexcel = new Microsoft.Office.Interop.Excel.Application(); 

var xlWorkBooks = xlexcel.Workbooks; 

xlexcel.Visible = false; 

xlWorkBooks.OpenText(@"C:\" + processName + ".csv", misValue, misValue, Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited,   Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue); 

// Set Sheet 1 as the sheet you want to work with 
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBooks[1].Worksheets.get_Item(1); 

xlWorkSheet.Shapes.AddChart(misValue, misValue, misValue, misValue, misValue).Select(); 

//~~> Make it a Line Chart 
     xlexcel.ActiveChart.ApplyCustomType(Microsoft.Office.Interop.Excel.XlChartType.xlLine); 

//~~> Set the data range 
xlexcel.ActiveChart.SetSourceData(xlWorkSheet.Range["E2:E200"]); 
xlexcel.ActiveChart.ChartWizard(misValue, Title: chartName + " (" + processName + ")", CategoryTitle: "Iterations", ValueTitle: processType); 

Microsoft.Office.Interop.Excel.ChartObjects chartObjects =(Microsoft.Office.Interop.Excel.ChartObjects)(xlWorkSheet.ChartObjects(Type.Missing)); 
foreach (Microsoft.Office.Interop.Excel.ChartObject co in chartObjects) 
{ 
    co.Select(); 
    Microsoft.Office.Interop.Excel.Chart chart = (Microsoft.Office.Interop.Excel.Chart)co.Chart; 
    chart.Export(ConfigurationManager.AppSettings.Get("Charts") + "\\ProcessFiles" + @"\" + chartName + " (" + processName + "of" + processType + ")" + ".png", "PNG", false); 
    co.Delete(); 
} 
xlWorkBooks[1].Close(true, misValue, misValue); 
xlexcel.Quit(); 

任何指導,將不勝感激。 謝謝!

+0

你想在Excel或導出爲圖像文件顯示? – imsome1

+0

我想將其導出爲圖像文件。 @ imsome1 – TrishaR

回答

0

我想你的代碼,並做了一些修改,我希望這將工作

using Excel = Microsoft.Office.Interop.Excel; 

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

object misValue = System.Reflection.Missing.Value; 
//string appPath = Path.GetDirectoryName(Application.ExecutablePath); 
string fileName = "" + "YOUR_PATH" + "\\Templates\\myCSV.csv"; 

string processName = "test"; 
xlApp = new Excel.Application(); 
xlWorkBook = xlApp.Workbooks.Open(fileName); 
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

xlWorkSheet.Shapes.AddChart(misValue, misValue, misValue, misValue, misValue).Select(); 

//~~> Make it a Line Chart 
      xlApp.ActiveChart.ApplyCustomType(Microsoft.Office.Interop.Excel.XlChartType.xlLine); 

//~~> Set the data range 
xlApp.ActiveChart.SetSourceData(xlWorkSheet.Range["B1:B30"]); 
string chartName = "TEST CHART", processType="TEST TYPE"; 
xlApp.ActiveChart.ChartWizard(misValue, Title: chartName + " (" + processName + ")", CategoryTitle: "Iterations", ValueTitle: processType); 

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

foreach (Excel.ChartObject co in chartObjects) 
{ 
    co.Select(); 
    Excel.Chart chart = (Excel.Chart)co.Chart; 
    chart.Export("C:\\YOUR_PATH" + @"\" + chart.Name + ".png", "PNG", false); 
} 

xlWorkBook.Close(true, misValue, misValue); 
xlApp.Quit(); 


xlWorkSheet = null; 
xlWorkBook = null; 
xlApp = null; 
releaseObject(xlWorkBook); 
releaseObject(xlWorkSheet); 
releaseObject(xlApp); 

private void releaseObject(object obj) 
{ 
    try 
    { 
    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
    obj = null; 
    } 
    catch (Exception ex) 
    { 
    obj = null; 
    MessageBox.Show(ex.Message); 
    } 
    finally 
    { 
    GC.Collect(); 
    } 
} 
+0

感謝您的幫助@ imsome1,但我的控制檯應用程序,所以Application.ExecutablePath不起作用。除此之外,我仍然得到相同的輸出。 – TrishaR

+0

給你的路徑,而不是Application.ExecutablePath,並試試 – imsome1

+0

是的,我試過了。但我仍然沒有得到所需的輸出。 – TrishaR