C# 更改图形c的x轴值#
我对c#和visualstudio有点陌生,我正在尝试根据其他地方的csv文件的值绘制一个图形。我使用了c#中的ChartObjects和ChartWizard属性来创建图形。绘制的图形应该是我提供的列范围,在Y轴和X轴上应该有当前的行号(1,2,3,4等)。但是,默认情况下,我的图形将X轴作为csv文件中的第一列。如果我也为X轴指定了一个范围,它会正确打印,但如何才能在那里获得当前的行号 我浏览了很多文章和问题,甚至是关于堆栈溢出的,但似乎都没有帮助 以下是我的代码片段:C# 更改图形c的x轴值#,c#,excel,visual-studio,csv,C#,Excel,Visual Studio,Csv,我对c#和visualstudio有点陌生,我正在尝试根据其他地方的csv文件的值绘制一个图形。我使用了c#中的ChartObjects和ChartWizard属性来创建图形。绘制的图形应该是我提供的列范围,在Y轴和X轴上应该有当前的行号(1,2,3,4等)。但是,默认情况下,我的图形将X轴作为csv文件中的第一列。如果我也为X轴指定了一个范围,它会正确打印,但如何才能在那里获得当前的行号 我浏览了很多文章和问题,甚至是关于堆栈溢出的,但似乎都没有帮助 以下是我的代码片段: Microsoft.
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();
}
}
我尝试了你的代码,并做了一些修改,我希望这将工作
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();
}
}
是否要在excel中显示或导出为图像文件?我要将其导出为图像文件@imsome1您想在excel中显示还是导出为图像文件?我想将其导出为图像文件@imsome1感谢您@imsome1的帮助,但我的是一个控制台应用程序,所以application.ExecutablePath不起作用。除此之外,我仍然得到相同的输出。请给出您的路径而不是Application.ExecutablePath和tryYes,我尝试过了。但我仍然没有得到所需的输出。是否有任何错误?您是否正确更改了路径和文件名?它在这里工作,没有错误。只是创建的图形在我的csv中仍然有第一列作为X-Axis感谢您的帮助@imsome1,但是我的是一个控制台应用程序,所以application.ExecutablePath不起作用。除此之外,我仍然得到相同的输出。请给出您的路径而不是Application.ExecutablePath和tryYes,我尝试过了。但我仍然没有得到所需的输出。是否有任何错误?您是否正确更改了路径和文件名?它在这里工作,没有错误。只是创建的图形仍将csv中的第一列作为X轴