我對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