Draw two charts with Y axes in Excel

Source: Internet
Author: User

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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.