C# MSGraph.Chart.8 画图,制表

using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;
using Graph = Microsoft.Office.Interop.Graph;
using Word = Microsoft.Office.Interop.Word;

 

namespace WeeklyReporter
{
    public partial class MainForm : Form
    { 

        private void CreateReport(DateTime fromDate, DateTime toDate, string reportDepartment)
        {  

                DateTable dt = Query("...");

                if (oDoc.Bookmarks.Exists(Convert.ToString(oBookMark)))
                {
                    object nothing = System.Reflection.Missing.Value;

                    //文档中创建表格
                    Microsoft.Office.Interop.Word.Table gmsTable = oDoc.Tables.Add(oDoc.Bookmarks[oBookMark].Range, dt.Rows.Count + 1, dt.Columns.Count, ref nothing, ref nothing);

                    //设置表格样式
                    gmsTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    gmsTable.Borders.OutsideColor = Word.WdColor.wdColorPaleBlue;
                    gmsTable.Borders.OutsideLineWidth = Word.WdLineWidth.wdLineWidth225pt;

                    gmsTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    gmsTable.Borders.InsideColor = Word.WdColor.wdColorPaleBlue;
                    gmsTable.Borders.InsideLineWidth = Word.WdLineWidth.wdLineWidth225pt;

                    //填充表格内容
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        gmsTable.Cell(1, i + 1).Shading.ForegroundPatternColor = Word.WdColor.wdColorLightGreen;
                        gmsTable.Cell(1, i + 1).Range.Bold = 20;
                    }

                    gmsTable.Cell(1, 1).Range.Text = "A";
                    gmsTable.Cell(1, 2).Range.Text = "B";
                    gmsTable.Cell(1, 3).Range.Text = "C";
                    gm

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用`Task.Factory.StartNew`方法来启动一个后台线程,并使用`Invoke`方法在UI线程上更新`Chart`控件,以避免卡顿UI。以下是一个示例代码: ```csharp using System; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; class Program { static void Main(string[] args) { var chart = new Chart(); chart.Series.Add("Series1"); chart.ChartAreas.Add("ChartArea1"); chart.ChartAreas[0].AxisX.Minimum = 0; chart.ChartAreas[0].AxisX.Maximum = 10; chart.ChartAreas[0].AxisY.Minimum = 0; chart.ChartAreas[0].AxisY.Maximum = 100; var form = new Form(); form.Controls.Add(chart); form.Load += (sender, e) => { Task.Factory.StartNew(() => { var random = new Random(); var i = 0; while (true) { var value = random.Next(0, 100); chart.Invoke((Action)(() => { chart.Series["Series1"].Points.AddXY(i, value); if (chart.Series["Series1"].Points.Count > 10) { chart.ChartAreas[0].AxisX.Minimum = chart.Series["Series1"].Points[chart.Series["Series1"].Points.Count - 10].XValue; chart.ChartAreas[0].AxisX.Maximum = chart.Series["Series1"].Points[chart.Series["Series1"].Points.Count - 1].XValue; } })); Task.Delay(1000).Wait(); // 每隔1秒钟执行一次循环 i++; } }, TaskCreationOptions.LongRunning); }; Application.Run(form); } } ``` 在这个示例中,我们创建了一个`Chart`控件,并在`Form`中显示它。在`Form`的`Load`事件处理程序中,我们使用`Task.Factory.StartNew`方法来启动一个后台线程,并在其中执行循环。在循环中,我们使用`Invoke`方法来在UI线程上更新`Chart`控件。我们还检查了`Chart`中点的数量,以便在添加新点后自动调整`Chart`的`AxisX`范围,以保持最近的10个点可见。最后,我们使用`Application.Run`方法来运行`Form`并显示`Chart`控件。 请注意,在使用`Invoke`方法时,必须将更新UI的代码封装在一个`Action`委托中,以便它可以在UI线程上执行。此外,我们使用了`TaskCreationOptions.LongRunning`选项来告诉.NET运行时,该任务将运行较长时间,并可能在后台线程上使用较多的资源。最后,我们在`Task.Delay`后面调用了`.Wait()`方法来确保在执行下一次循环之前等待1秒钟,以避免在后台线程上卡顿UI。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值