使用asp.net创建Excel图表c# [英] Creating Excel chart using asp.net c#
本文介绍了使用asp.net创建Excel图表c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
创建Web应用程序,我在其中创建基于数据表和数据集的Excel工作表。我在这个任务中取得了成功,我现在的问题是我想在同一个excel文件中显示基于数据集的简单图表或者在excel上显示的数据?
我提供我的代码请编辑这个。
提前谢谢。
Hi,
I m creating web application in which I m create an excel sheet based on datatable and dataset. I m success in this task now my problem is that I want to display the simple chart in same excel file based on dataset or data which shows on excel?
I m providing my code please edit this.
Thankx in Advance.
protected void btn_download_Click(object sender, EventArgs e)
{
from = (fromCalendar.SelectedDate);
to = (ToCalendar.SelectedDate);
client_id = Convert.ToInt32(drp_Client.SelectedValue);
Compaign = drp_Compaign.SelectedValue;
try
{
SqlCommand cmd_down_tw = new SqlCommand("proc_GetSemDataFromToDate", con);
cmd_down_tw.CommandType = CommandType.StoredProcedure;
cmd_down_tw.Parameters.Add("@FromDate", System.Data.SqlDbType.DateTime).Value = (from);
cmd_down_tw.Parameters.Add("@ToDate", System.Data.SqlDbType.DateTime).Value = (to);
cmd_down_tw.Parameters.Add("@ClientId", System.Data.SqlDbType.Int).Value = (client_id);
cmd_down_tw.Parameters.Add("@Compaign", System.Data.SqlDbType.VarChar).Value = (Compaign);
SqlDataAdapter da_down_tw = new SqlDataAdapter(cmd_down_tw);
da_down_tw.SelectCommand = cmd_down_tw;
DataTable dt_down_tw = new DataTable();
da_down_tw.Fill(dt_down_tw);
SqlCommand cmd_comp_tw = new SqlCommand("proc_GetAllSemDataFromToDate", con);
cmd_comp_tw.CommandType = CommandType.StoredProcedure;
cmd_comp_tw.Parameters.Add("@FromDate", System.Data.SqlDbType.DateTime).Value = (from);
cmd_comp_tw.Parameters.Add("@ToDate", System.Data.SqlDbType.DateTime).Value = (to);
cmd_comp_tw.Parameters.Add("@ClientId", System.Data.SqlDbType.Int).Value = (client_id);
cmd_comp_tw.Parameters.Add("@Compaign", System.Data.SqlDbType.VarChar).Value = (Compaign);
da_comp_tw = new SqlDataAdapter(cmd_comp_tw);
da_comp_tw.SelectCommand = cmd_comp_tw;
dt_comp_tw = new DataTable();
da_comp_tw.Fill(dt_comp_tw);
dt_down_tw.Merge(dt_comp_tw);
//DataTable[] data_tables = new DataTable[2];
//data_tables[0] = dt_comp_tw;
//data_tables[1] = dt_down_tw;
ExportDataSetToExcel(dt_down_tw, "Report.xls");
dt_down_tw.Dispose();
}
catch
{
}
}
public void ExportDataSetToExcel(DataTable dt, string filename)
{
HttpResponse response = HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = dt;
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
推荐答案
看看这个 [ ^ ]例如,这可能对你有所帮助..
Look out this[^] example, this might be helpful for you..
尝试这个选项,
Hi,
try out this option,
// use this reference for MS-Excel
using Excel = Microsoft.Office.Interop.Excel;
//1.create the Excel Chart object
Excel.ChartObjects xlCharts = (Excel.ChartObjects)excelSheet.ChartObjects(Type.Missing);
//2. Set the position of chart where you need to place inside the Excel sheet
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 5, 300, 250);
//3. create a new chart page to display your value
Excel.Chart chartPage = myChart.Chart;
//4.Set the X & Y axis Range of data columns
//4.1 it takes Excel A Column as as X axis; Data value is from A20-A30
//4.2 it takes Excel B Column as as Y axis; Data value is from A20-A30
Excel.Range chartRange; = excelSheet.get_Range("A20", "B30");
//5.Set the chart Source data from your chart range
chartPage.SetSourceData(chartRange, Type.Missing);
//6.select the chart type to render your data values
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
//7.If you need to declare the chart title please follow the two steps
myChart.Chart.HasTitle = true;
chartPage.ChartTitle.Text = "Column Chart";
--Sowraaj
--Sowraaj
这篇关于使用asp.net创建Excel图表c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文