Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here i export a gridview into excel but i need to export a line chart into excel.

What I have tried:

/// Here i tried for Gridview ////

private void ExportToExcel()
    {
        try
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=OverallReport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);

                //To Export all pages
                dgvTrendChart.AllowPaging = false;
                this.SearchOverallList();

                dgvTrendChart.HeaderRow.BackColor = Color.White;
                foreach (TableCell cell in dgvTrendChart.HeaderRow.Cells)
                {
                    cell.BackColor = dgvTrendChart.HeaderStyle.BackColor;
                }

                for (int rowIndex = dgvTrendChart.Rows.Count - 2; rowIndex >= 0; rowIndex--)
                {
                    GridViewRow row = dgvTrendChart.Rows[rowIndex];
                    GridViewRow previousRow = dgvTrendChart.Rows[rowIndex + 1];

                    for (int i = 0; i < row.Cells.Count; i++)
                    {
                        if (i != 3)
                        {
                            if (row.Cells[i].Text == previousRow.Cells[i].Text)
                            {
                                row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
                                                       previousRow.Cells[i].RowSpan + 1;
                                previousRow.Cells[i].Visible = false;
                                row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
                                row.Cells[i].VerticalAlign = VerticalAlign.Top;
                            }
                        }
                    }
                }
                dgvTrendChart.RenderControl(hw);

                //style to format numbers to string
                string style = @"<style> .textmode { } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }
        catch
        {
            //throw new Exception("");
        }
    }
Posted
Updated 1-May-17 21:27pm
Comments
Suvendu Shekhar Giri 2-May-17 2:43am    
So what is the issue with the code you have tried?
Is it giving any error?
Ishakkhan 2-May-17 2:59am    
This code is for exporting gridview into excel.it is working fine.But i need to export a line chart into excel.So, i need your help.

1 solution

    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;
    Excel._Application xlApp = new Excel.Application();

    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

//add data
xlWorkSheet.Cells[1, 1] = 13;
xlWorkSheet.Cells[1, 2] = 27;
xlWorkSheet.Cells[1, 3] = 22;
xlWorkSheet.Cells[1, 4] = 22;

xlWorkSheet.Cells[2, 1] = 42    ;
xlWorkSheet.Cells[2, 2] = 35;
xlWorkSheet.Cells[2, 3] = 22;
xlWorkSheet.Cells[2, 4] = 22;

xlWorkSheet.Cells[3, 1] = 1;
xlWorkSheet.Cells[3, 2] = 10;
xlWorkSheet.Cells[3, 3] = 4;
xlWorkSheet.Cells[3, 4] = 4;


Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
myChart.Select();

chartPage.ChartType = Excel.XlChartType.xlXYScatterLines;
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();

Excel.Series series1 = seriesCollection.NewSeries();
series1.XValues = xlWorkSheet.get_Range("A1", "B1"); ;
series1.Values = xlWorkSheet.get_Range("C1", "D1");

Excel.Series series2 = seriesCollection.NewSeries();
series2.XValues = xlWorkSheet.get_Range("A2", "B2"); ;
series2.Values = xlWorkSheet.get_Range("C2", "D2");

Excel.Series series3 = seriesCollection.NewSeries();
series3.XValues = xlWorkSheet.get_Range("A3", "B3"); ;
series3.Values = xlWorkSheet.get_Range("C3", "D3");



xlWorkBook.SaveAs(@"C:\ProgramData\RadiolocationQ\Text.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
 
Share this answer
 
Comments
Ishakkhan 2-May-17 4:44am    
Thanks for your reply.I used this code but it shows an error like

Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Excel.SeriesCollection'. An explicit conversion exists (are you missing a cast?)
Wessel Beulink 2-May-17 4:53am    
replace (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1) to:

(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1)
or import Microsoft.Office.Interop
Ishakkhan 2-May-17 5:44am    
Thanks for your comments.I tried the code that you mentioned above but still it is exists an error.
Wessel Beulink 2-May-17 5:47am    
On which line does it brake? There multiple Excel.SeriesCollection casts in the example.
Ishakkhan 2-May-17 5:50am    
In this place,the error occured.

Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900