我正在使用 c# 和 excel interop dll 以编程方式开发图表。我必须在 excel 图表中设置 TickLabels 的边框颜色。这是我的屏幕截图,我指出需要以编程方式设置边框颜色的区域。我尝试了很多和搜索谷歌,但没有发现任何线索。所以请查看我的图片和代码并帮助我实现我的目标。
这是我的完整代码
public ChartData PrepareExcel()
{
bool flag = false;
int rowindex = 3;
if (oData != null)
{
if (oData.Tables.Count > 0)
{
if (oData.Tables[0].Rows.Count > 0)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet1;
Excel.Worksheet xlWorkSheet2;
Excel.Worksheet xlWorkSheetChart;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
try
{
xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet1.Name = "Detail Data";
xlWorkSheet2 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheet2.Name = "Summary Data";
xlWorkSheetChart = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
xlWorkSheetChart.Name = "Chart";
// for first sheet start
xlWorkSheet1.Cells[1, 1] = "Daily Finished Jobs History Detail Report From " +
DateTime.ParseExact(this.startdate, "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd") +" To " +
DateTime.ParseExact(this.enddate, "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
xlWorkSheet1.get_Range("A1", "Y1").Merge(true);
xlWorkSheet1.get_Range("A1", "Y1").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
xlWorkSheet1.get_Range("A1", "Y1").Font.Bold = true;
xlWorkSheet1.get_Range("A1", "Y1").Font.Size = 14;
xlWorkSheet1.get_Range("A3", "Y3").Font.Bold = true;
for (int x = 0; x <= oData.Tables[0].Columns.Count - 1; x++)
{
xlWorkSheet1.Cells[3, x + 1] = oData.Tables[0].Columns[x].ColumnName.ToString();
xlWorkSheet1.Columns[x + 1].AutoFit();
//xlWorkSheet1.Columns[x + 1].Font.Bold = true;
}
for (int x = 0; x <= oData.Tables[0].Rows.Count - 1; x++)
{
int row= rowindex + (x + 1);
for (int y = 0; y <= oData.Tables[0].Columns.Count - 1; y++)
{
xlWorkSheet1.Cells[row, y + 1] = oData.Tables[0].Rows[x][y].ToString();
xlWorkSheet1.Columns[1].AutoFit();
}
}
// for first sheet end
// for second sheet start
xlWorkSheet2.Cells[1, 1] = "Daily Finished Jobs History Summary Report From " +
DateTime.ParseExact(this.startdate, "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd") + " To " +
DateTime.ParseExact(this.enddate, "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
xlWorkSheet2.get_Range("A1", "I1").Merge(true);
xlWorkSheet2.get_Range("A1", "I1").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
xlWorkSheet2.get_Range("A1", "I1").Font.Bold = true;
xlWorkSheet2.get_Range("A1", "I1").Font.Size = 14;
xlWorkSheet2.get_Range("A3", "I3").Font.Bold = true;
for (int x = 0; x <= oData.Tables[1].Columns.Count - 1; x++)
{
xlWorkSheet2.Cells[3, x + 1] = oData.Tables[1].Columns[x].ColumnName.ToString();
xlWorkSheet2.Columns[x + 1].AutoFit();
}
for (int x = 0; x <= oData.Tables[1].Rows.Count - 1; x++)
{
int row = rowindex + (x + 1);
for (int y = 0; y <= oData.Tables[1].Columns.Count - 1; y++)
{
xlWorkSheet2.Cells[row, y + 1] = oData.Tables[1].Rows[x][y].ToString();
xlWorkSheet2.Columns[1].AutoFit();
}
flag = true;
}
//for second sheet end
// chart start
if (flag)
{
Excel.SeriesCollection seriesCollection;
Excel.Series series1, series2, series3;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheetChart.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 20, 900, 520);
Excel.Chart chartPage = myChart.Chart;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Daily Job History Chart From " +
DateTime.ParseExact(this.startdate, "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd") + " To " +
DateTime.ParseExact(this.enddate, "yyyyMMdd", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
chartPage.Legend.Position = XlLegendPosition.xlLegendPositionBottom;
seriesCollection = (Excel.SeriesCollection)chartPage.SeriesCollection(Type.Missing);
series1 = seriesCollection.NewSeries();
series2 = seriesCollection.NewSeries();
series3 = seriesCollection.NewSeries();
series1.Name = "='Summary Data'!$B$3";
series1.Values = "='Summary Data'!$B$4:$B$" + (5+ oData.Tables[1].Rows.Count);
series1.ChartType = XlChartType.xlColumnClustered;
series1.AxisGroup = XlAxisGroup.xlSecondary;
series2.Name = "='Summary Data'!$C$3";
series2.XValues = "='Summary Data'!$A$4:$A$" + (5 + oData.Tables[1].Rows.Count);
series2.Values = "='Summary Data'!$C$4:$C$" + (5 + oData.Tables[1].Rows.Count);
series2.ChartType = XlChartType.xlLineMarkers;
series3.Name = "='Summary Data'!$D$3";
series3.Values = "='Summary Data'!$D$4:$D$" + (5 + oData.Tables[1].Rows.Count);
series3.ChartType = XlChartType.xlLineMarkers;
series3.AxisGroup = XlAxisGroup.xlPrimary;
Axis axis;
axis = (Axis)chartPage.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
axis.HasTitle = true;
axis.AxisTitle.Text = "Percentage/Hour";
axis.AxisTitle.Font.Color = Color.Blue;
axis.Border.Color = Color.Transparent;
axis.HasMajorGridlines = false;
axis.HasMinorGridlines = false;
axis = (Axis)chartPage.Axes(XlAxisType.xlValue, XlAxisGroup.xlSecondary);
axis.HasTitle = true;
axis.AxisTitle.Text = "Total Jobs completed";
axis.AxisTitle.Font.Color = Color.Blue;
axis.HasMajorGridlines = true;
axis.HasMinorGridlines = false;
axis = (Excel.Axis)chartPage.Axes(XlAxisType.xlCategory);
axis.MajorTickMark = XlTickMark.xlTickMarkNone;
axis.HasMajorGridlines = true;
chartPage.Axes(Excel.XlAxisType.xlCategory).Select();
chartPage.Axes(Excel.XlAxisType.xlCategory).TickLabels.Orientation = 35;
}
// chart end
string strpath = System.Windows.Forms.Application.StartupPath.ToString() + "\\DailyFinishedJobHistoryReport_"+DateTime.Now.ToString("dd-MM-yyyy")+".xls";
if (File.Exists(strpath))
{
File.Delete(strpath);
}
xlWorkBook.SaveAs(strpath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception ex)
{
}
finally
{
xlWorkBook.Close(false, misValue, misValue);
xlWorkBook = null;
xlApp.Quit();
xlApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
}
return this;
}
请查看显示日期的箭头区域。我想更改该区域的边框颜色。引导我。谢谢
更新
看到这个屏幕截图。基本上我想将该区域的边框颜色更改为透明,但我不知道我需要在哪里添加代码。如果我仍然不清楚,请告诉我。谢谢