问题在于 Excel 中的折线图默认情况下会尝试确定它的 x 轴类型并得出结论,它是 Date 类型,在您的情况下只有一半正确。如果您随后将其更改为轴选项下的“文本”,它将把所有内容都视为类别并包括时间组件。请注意,这不会按日期/时间差异“正确”间隔它们,因此 1/1 和 1/2 之间的距离将与 1/2 和 1/6 相同 - 这可能是也可能不是您想要的. 无论哪种方式,我都看不到eAxisType
在 EPP 中设置 X 的选项,因为它是只读的,并且在构建图表对象时设置。我认为如果你想弄乱它,你将不得不使用手动 XML 操作。
您可能是在 XY 散点图之后吗?这样的事情可能会让你得到你想要的:
[TestMethod]
public void Chart_DateTime_Test()
{
//http://stackoverflow.com/questions/28158702/chart-x-axis-date-and-time
var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
if (existingFile.Exists)
existingFile.Delete();
using (var pck = new ExcelPackage(existingFile))
{
var wsContent = pck.Workbook.Worksheets.Add("Content");
var wsPressure = pck.Workbook.Worksheets.Add("Pressure");
//Some data
wsContent.Cells["A1"].Value = "A";
wsContent.Cells["B1"].Value = "B";
wsContent.Cells["C1"].Value = "C";
wsContent.Cells["D1"].Value = "D";
wsContent.Cells["A2"].Value = 1;
wsContent.Cells["A3"].Value = 4;
wsContent.Cells["A4"].Value = 2;
wsContent.Cells["A5"].Value = 3;
wsContent.Cells["A6"].Value = 6;
wsContent.Cells["A7"].Value = 5;
wsContent.Cells["D2"].Value = new DateTime(2015, 1, 1, 8, 15, 0);
wsContent.Cells["D3"].Value = new DateTime(2015, 1, 1, 15, 15, 0);
wsContent.Cells["D4"].Value = new DateTime(2015, 1, 2, 8, 15, 0);
wsContent.Cells["D5"].Value = new DateTime(2015, 1, 3, 8, 15, 0);
wsContent.Cells["D6"].Value = new DateTime(2015, 1, 3, 15, 15, 0);
wsContent.Cells["D7"].Value = new DateTime(2015, 1, 3, 20, 15, 0);
const int dataRow = 7;
const string FORMATDATE = "m/d/yy h:mm;@";
wsContent.Cells[2, 4, dataRow, 4].Style.Numberformat.Format = FORMATDATE;
//var chartPressure = wsPressure.Drawings.AddChart("PressureChart", eChartType.Line);
var chartPressure = wsPressure.Drawings.AddChart("PressureChart", eChartType.XYScatterLines);
chartPressure.SetSize(1280, 1024);
//var serie1 = (ExcelLineChartSerie)chartPressure.Series.Add("=Content!$A$2:$A$" + dataRow, "=Content!$D$2:$D$" + dataRow);
var serie1 = (ExcelScatterChartSerie)chartPressure.Series.Add(wsContent.Cells[2, 1, dataRow, 1], wsContent.Cells[2, 4, dataRow, 4]);
serie1.Header = wsContent.Cells[1, 1].Value.ToString();
pck.Save();
}
}