2017-02-22 194 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