關(guān)于C#操作EXCEL,生成圖表的全面應(yīng)用之二(利用Microsoft.Office.Interop.OWC11) 收藏
續(xù)上篇,介紹OWC11的應(yīng)用:
開發(fā)者:Gailzhao
另添加引用
using OWC = Microsoft.Office.Interop.Owc11;
public void DataTableToSheet(string title, DataTable dt, Excel._Worksheet m_Sheet,
Excel._Workbook m_Book, int startrow)
{
。。。。。。(同上篇)。。。。。。
//使用OWC11組件畫圖
showChart(OWC.ChartChartTypeEnum.chChartTypeLine); //生成圖表文件
//將圖表文件插入到EXCEL文檔中
m_Sheet.Shapes.AddPicture("E:\\chart.gif", MsoTriState.msoFalse, MsoTriState.msoTrue, 250, 100, 350, 250 );
}
private void showChart(OWC.ChartChartTypeEnum Type)
{
OWC.ChartChartTypeEnum _Type;
OWC.ChartSpace axChartSpace1 = new OWC.ChartSpaceClass();
try
{
axChartSpace1.Clear();
OWC.ChChart objChart = axChartSpace1.Charts.Add(0);
OWC.ChAxis axis = objChart.Axes[0]; //X軸
OWC.ChAxis axis1 = objChart.Axes[1]; //Y軸
objChart.Type = Type;
objChart.HasLegend = true;
objChart.Legend.Position = OWC.ChartLegendPositionEnum.chLegendPositionTop;
//objChart.HasTitle = true;
//objChart.Title.Caption = "凈值指數(shù)圖";
//objChart.Axes[0].HasTitle = true ;
//objChart.Axes[0].Title.Caption = "日期";
//objChart.Axes[1].HasTitle = true;
//objChart.Axes[1].Title.Caption = "數(shù)值";
objChart.SeriesCollection.Add(0);
objChart.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimSeriesNames,
+(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, "開放式基金");
DataSet ds = ScData.ListData("exec zb_LiCaiZhouBao " + int.Parse(txtStartDate.Text.Replace("-","") ) + ",1");
string X_Value1 = "";
string X_Value2 = "";
string Y_Value = "";
if (ds.Tables.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
// Console.WriteLine(dr["日期"].ToString());
Y_Value = Y_Value + dr["日期"].ToString() + '\t';
X_Value1 = X_Value1 + dr["開放式基金"].ToString() + '\t';
X_Value2 = X_Value2 + dr["偏股型基金"].ToString() + '\t';
}
}
objChart.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimCategories,
+(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, Y_Value);
objChart.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimValues,
(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, X_Value1);
objChart.SeriesCollection.Add(1);
objChart.SeriesCollection[1].SetData(OWC.ChartDimensionsEnum.chDimSeriesNames,
+(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, "偏股型基金");
objChart.SeriesCollection[1].SetData(OWC.ChartDimensionsEnum.chDimCategories,
+(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, Y_Value);
objChart.SeriesCollection[1].SetData(OWC.ChartDimensionsEnum.chDimValues,
(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, X_Value2);
objChart.SeriesCollection[0].Line.Color = "orange";
//objChart.SeriesCollection[0].Line.Weight = OWC.ChLine.LineWeightEnum.owcLineWeightThick; //線條加粗
objChart.SeriesCollection[1].Line.Color = "maroon";
//objChart.SeriesCollection[1].Line.Weight = OWC.LineWeightEnum.owcLineWeightThick;
axis.TickMarkSpacing = 30;
axis.HasTickLabels = true;
axis.TickLabelSpacing =30;
axis1.HasMajorGridlines = true;
axis1.MajorGridlines.Line.DashStyle = OWC.ChartLineDashStyleEnum.chLineDashDotDot;//.chLineDashDot;
//axis1.MajorGridlines.Line.Color = "orange";
objChart.PlotArea.Interior.Color = "LightYellow"; //圖表區(qū)的背景色
// objChart.Interior.Color = "green";
//objChart.Interior.BackColor = "yellow";
//axis.TickMarkSpacing = 50;
}
catch (Exception ex)
{
//timer1.Enabled = false;
MessageBox.Show(ex.Message);
}
finally
{
_Type = Type;
//輸出成GIF文件.
string strAbsolutePath = "E:\\chart.gif"; //生成圖形文件
axChartSpace1.ExportPicture(strAbsolutePath, "GIF", 600, 350);
}
}