In Excel, draw chart is not very difficult, but it still takes some twists and turns to be complicated, the function shown in the following year-on-year charts is implemented after nearly two days. I checked a lot of information and made a lot of details to satisfy me!
Private bool savecharttoexcel (Excel. Workbook WB, Excel. Application TAPP)
{
Excel. worksheet xlchartsheet = (Excel. worksheet) WB. worksheets. Add (
Type. Missing, WB. Sheets [WB. Sheets. Count], type. Missing, type. Missing );
Xlchartsheet. Name = "Graph ";
Excel. Chart xlchart = (Excel. Chart) WB. charts. Add (
Type. Missing, WB. worksheets [WB. worksheets. Count], type. Missing, type. Missing );
Try
{
For (INT I = 0; I <m_axchartspace.charts.count; I ++)
{
Owc. worksheet xlspread = (owc. worksheet) m_axspreadsheet.activesheet;
// Select the row header and paste it to the newly added table.
Xlspread. get_range (xlspread. cells [1, 1],
Xlspread. cells [m_rows + m_headerrows, m_headercols]). Select ();
Xlspread. cells. Copy (type. Missing );
Xlchartsheet. paste (type. Missing, type. Missing );
// Select the data area and the columns for drawing, and paste them into the new table.
Xlspread. get_range (xlspread. cells [m_chartrow, m_chartcol],
Xlspread. cells [m_rows + m_headerrows, m_chartcol2]). Select ();
Xlspread. cells. Copy (type. Missing );
Excel. Range rng1 = xlchartsheet. get_range (xlchartsheet. cells [m_chartrow, m_headercols], xlchartsheet. cells [m_rows + m_headerrows, m_headercols]);
Excel. Range rng2 = xlchartsheet. get_range (xlchartsheet. cells [m_chartrow, m_chartcol], xlchartsheet. cells [m_rows + m_headerrows, m_chartcol2]);
Excel. range RNG = Tapp. union (rng1, rng2, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing );
// Xlchart. hasaxis (xlvalue, xlprimary) = true;
// Xlchart. hasaxis (xlvalue, xlsecondary) = true;
Xlchart. setsourcedata (RNG, Excel. xlrowcol. xlcolumns );
// (Excel. series) xlchart. seriescollection (3 )). xvalues = xlchartsheet. get_range (xlchartsheet. cells [m_chartrow, m_chartcol2], xlchartsheet. cells [m_rows + m_headerrows, m_chartcol2]);
Xlchart. set_hasaxis (Excel. xlaxistype. xlvalue, Excel. xlaxisgroup. xlprimary, type. Missing );
Xlchart. set_hasaxis (Excel. xlaxistype. xlvalue, Excel. xlaxisgroup. xlsecondary, type. Missing );
(Excel. Series) xlchart. seriescollection (1). charttype = excel. xlcharttype. xllinemarkers;
(Excel. Series) xlchart. seriescollection (2). charttype = excel. xlcharttype. xllinemarkers;
(Excel. Series) xlchart. seriescollection (3). charttype = excel. xlcharttype. xlcolumnclustered;
Excel. Series S0 = (Excel. Series) xlchart. seriescollection (3 ));
S0.axisgroup = excel. xlaxisgroup. xlsecondary;
// (Excel. Series) xlchart. seriescollection (3). axisgroup = true;
Xlchart. plotarea. Fill. twocolorgradient (
Microsoft. Office. Core. msogradientstyle. msogradienthorizontal, 2 );
Xlchart. plotarea. Fill. forecolor. schemecolor = 12;
Xlchart. plotarea. Fill. backcolor. schemecolor = 36;
Xlchart. chartarea. Font. size = 9;
Xlchart. Location (Excel. xlchartlocation. xllocationasobject,
Xlchartsheet. Name );
}
Return true;
}
Catch (exception ex)
{
Console. writeline (ex. Message );
Return false;
}
}