我在使 Excel 图形类型显示为 ScatterLine 时遇到问题。奇怪的是,编译后,在我的电脑上它显示为 ScatterLine,但在其他人的电脑上它显示为没有 X 轴的 Line 类型!
private void showExcelXY_Open(ArrayList al)
{
ArrayList alX = new ArrayList();
ArrayList alY = new ArrayList();
alX.Clear();
alY.Clear();
for (int i = 0; i < al.Count; i++)
{
string[] val = al[i].ToString().Split('^');
alX.Add(val[0]);
alY.Add(val[1]);
}
xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;
// Now create the chart.
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
ChartObject chartObj = chartObjs.Add(100, 20, 500, 300);
Microsoft.Office.Interop.Excel.Chart xlChart = chartObj.Chart;
//add some data from your datasource like a dataset or like below.
ws.Cells[1, 1] = "Pressure";
ws.Cells[1, 2] = "Compression";
ws.Cells[2, 1] = alX[0];
ws.Cells[2, 2] = alY[0];
ws.Cells[3, 1] = alX[1];
ws.Cells[3, 2] = alY[1];
ws.Cells[4, 1] = alX[2];
ws.Cells[4, 2] = alY[2];
ws.Cells[5, 1] = alX[3];
ws.Cells[5, 2] = alY[3];
ws.Cells[6, 1] = alX[4];
ws.Cells[6, 2] = alY[4];
ws.Cells[7, 1] = alX[5];
ws.Cells[7, 2] = alY[5];
ws.Cells[8, 1] = alX[6];
ws.Cells[8, 2] = alY[6];
//set the source data and the chart type.
Range chartRange = ws.get_Range("A2", "B8");
xlChart.SetSourceData(chartRange, Type.Missing);
xlChart.ChartType = XlChartType.xlXYScatterLines;
// Customize axes:
Microsoft.Office.Interop.Excel.Axis xAxis = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
xAxis.HasMajorGridlines = true;
xAxis.HasTitle = true;
xAxis.AxisTitle.Text = "Above Pressure (psi) Below";
Microsoft.Office.Interop.Excel.Axis yAxis = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
yAxis.MajorTickMark = XlTickMark.xlTickMarkCross;
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = "Compression (lbf)";
// Add title:
xlChart.HasTitle = true;
xlChart.ChartTitle.Text = "XPak Performance Envelope" + '\n' + "(Open Top & Bottom)";
// Remove legend:
xlChart.HasLegend = false;
}
代码清楚地表明它将图表类型设置为:
xlChart.ChartType = XlChartType.xlXYScatterLines;
任何想法将不胜感激!