查询一句SQL语句,返回值怎么样和OWC的图表绑定?有没有实例?

解决方案 »

  1.   

    算你运气,我也是刚改的别人的?只要把网上的例子中的strCategory,strValue替换成你的项和值就成 private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    string strCategory = null;
    string strValue = null;
    using (SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString()))
    {
    try
    {
    DataTable dt = SqlHelper.ExecuteDataset(cn,CommandType.Text,"select top 1 * from un_tbl_VoteQuestion where isopen=1").Tables[0];
    if (dt.Rows.Count>0)
    {
    lbltitle.Text = dt.Rows[0]["QuestionTitle"].ToString();
    dt = SqlHelper.ExecuteDataset(cn,CommandType.Text,"select * from un_tbl_VoteItem where questionid ="+dt.Rows[0]["questionid"].ToString()).Tables[0];
    if (!Page.IsPostBack)
    {
    rbl.DataTextField = "ItemTitle";
    rbl.DataValueField = "ItemId";
    rbl.DataSource = dt.DefaultView;
    rbl.DataBind();
    }
    foreach(DataRow dr in dt.Rows)
    {
    strCategory += dr["ItemTitle"].ToString()+'\t';
    strValue += dr["ItemCount"].ToString()+'\t';
    }
    if (strCategory.Length>0)
    {
    strCategory = strCategory.Substring(0,strCategory.Length-1);
    strValue = strValue.Substring(0,strValue.Length-1);
    }
    }
    dt.Dispose();
    }
    catch(Exception ex)
    {
    cn.Close();
    throw new Exception(ex.Message);
    }
    }
    //声明对象
    OWC11.ChartSpace ThisChart = new  OWC11.ChartSpaceClass();
    OWC11.ChChart ThisChChart  = ThisChart.Charts.Add(0);
    OWC11.ChSeries ThisChSeries = ThisChChart.SeriesCollection.Add(0); //饼图百分比
    ThisChChart.SeriesCollection[0].DataLabelsCollection.Add();
    ThisChChart.SeriesCollection[0].DataLabelsCollection[0].HasPercentage = true;
    ThisChChart.SeriesCollection[0].DataLabelsCollection[0].HasCategoryName = false;
    ThisChChart.SeriesCollection[0].DataLabelsCollection[0].HasValue = false;
    ThisChChart.SeriesCollection[0].DataLabelsCollection[0].Interior.Color = "white";
    ThisChChart.SeriesCollection[0].DataLabelsCollection[0].Font.Name = "Verdana";
    //显示图例
    ThisChChart.HasLegend = true;
    //标题
    // ThisChChart.HasTitle = true;
    // ThisChChart.Title.Caption = "统计图";
    //
    // //给定x,y轴图示说明
    // ThisChChart.Axes[0].HasTitle = true;
    // ThisChChart.Axes[1].HasTitle = true;
    // ThisChChart.Axes[0].Title.Caption = "选项";
    // ThisChChart.Axes[1].Title.Caption = "数量";
    // ThisChChart.Axes[1].NumberFormat="0.0%"; //图表类型
    ThisChChart.Type = OWC11.ChartChartTypeEnum.chChartTypePieExploded3D;//.chChartTypePie3D;//chChartTypeColumnClustered3D;
    //旋转
    ThisChChart.Rotation  = 360;
    ThisChChart.Inclination = 10;
    //背景颜色
    ThisChChart.PlotArea.Interior.Color = "red";
    //底色
    ThisChChart.PlotArea.Floor.Interior.Color = "green"; //ThisChChart.Overlap = 50; //给定series的名字
    ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimSeriesNames,OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),"统计");
    //给定分类
    ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimCategories,OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strCategory);
    //给定值
    ThisChSeries.SetData(OWC11.ChartDimensionsEnum.chDimValues,OWC11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strValue);
    //导出图像文件
    try
    {
    DirectoryInfo di = new DirectoryInfo(Server.MapPath("tempimages"));
    if (!di.Exists)
    di.Create();
    if (File.Exists(Server.MapPath("tempimages/"+filename+".gif")))
    File.Delete(Server.MapPath("tempimages/"+filename+".gif"));
    filename = Guid.NewGuid().ToString();
    ThisChart.ExportPicture(Server.MapPath("tempimages/"+filename+".gif"),"gif",270,180);
    }
    catch(Exception ex)
    {
    throw new Exception(ex.Message);
    }
    }
      

  2.   

    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page 
    {
        Microsoft.Office.Interop.Owc11.SpreadsheetClass oExcelSheet = new Microsoft.Office.Interop.Owc11.SpreadsheetClass();
        string filepaht = "d:\test.xls";
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)//点buttion1按钮是写数据
        {
            oExcelSheet.ActiveSheet.Cells[2, 2] = "test";
            oExcelSheet.Export(filepaht, Microsoft.Office.Interop.Owc11.SheetExportActionEnum.ssExportActionNone, Microsoft.Office.Interop.Owc11.SheetExportFormat.ssExportXMLSpreadsheet);    }
    }我已经在项目中引用了owc1.dll,但每次,点按钮都出现异常{"异常来自 HRESULT:0xE004002A"}
    饭碗尤关,恳请高手协助,万分感激!
      

  3.   

    关键:
    foreach(DataRow dr in dt.Rows)
    {
    strCategory += dr["ItemTitle"].ToString()+'\t';
    strValue += dr["ItemCount"].ToString()+'\t';
    }
    也可以定义数组来实现