使用ASP.NET将折线图导出到excel [英] Export line chart into excel using ASP.NET

查看:140
本文介绍了使用ASP.NET将折线图导出到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我将gridview导出到excel,但是我需要将折线图导出到excel.

我尝试过的事情:

///在这里,我尝试使用Gridview////

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("");
        }
    }

推荐答案

    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();


这篇关于使用ASP.NET将折线图导出到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆