【问题标题】:Change the x axis values of graph c#更改图形 c# 的 x 轴值
【发布时间】:2017-02-22 11:32:33
【问题描述】:

我对 c# 和 Visual Studio 有点陌生,我正在尝试根据位于其他地方的 csv 文件的值绘制图表。我在 c# 中使用了 ChartObjects 和 ChartWizard 属性来创建图表。绘制的图表应该是我提供的列范围,在 Y 轴和 X 轴上应该有当前行号(1、2、3、4 等)。但是,默认情况下,我的图表将 X 轴作为我的 csv 文件中的第一列。如果我也为 X 轴指定一个范围,它会正确绘制,但我怎样才能在那里获得当前行号?

我甚至在 Stack Overflow 上也浏览了很多文章和问题,但似乎没有任何帮助。

这是我的代码的 sn-p:

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

标签: c# excel visual-studio csv


【解决方案1】:

我尝试了你的代码并做了一些修改,我希望这能工作

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

【讨论】:

  • 感谢@imsome1 的帮助,但我的是控制台应用程序,因此 Application.ExecutablePath 不起作用。除此之外,我仍然得到相同的输出。
  • 给出你的路径而不是 Application.ExecutablePath 并尝试
  • 是的,我试过了。但我仍然没有得到所需的输出。
  • 是否有任何错误?您是否正确更改了路径和文件名?它在这里工作
  • 没有错误。只是创建的图表仍然将我的 csv 中的第一列作为 X 轴
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 2013-09-12
相关资源
最近更新 更多