我对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();
任何指导,将不胜感激。 谢谢!
你想在Excel或导出为图像文件显示? – imsome1
我想将其导出为图像文件。 @ imsome1 – TrishaR