更改graphicsc#的x轴值

我有点新的C#和Visual Studio,我想绘制一个基于其他地方的csv文件的值的graphics。 我已经在c#中使用ChartObjects和ChartWizard属性来创buildgraphics。 绘制的图应该是我提供的列范围,在Y轴和X轴应该有当前行号(1,2,3,4等)。 然而,我的graphics默认情况下,X轴是我的CSV文件中的第一列。 如果我也为X轴指定了一个范围,但是我怎样才能得到当前的行号呢?

我经历了很多文章和问题,即使堆栈溢出,但似乎没有帮助。

这是我的代码片段:

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(); 

任何指导将不胜感激。 谢谢!

我试过你的代码,并做了一些修改,我希望这会起作用

 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(); } }