System.Drawing.Color.LightGreen

本文介绍了一个使用 ASP.NET 和 C# 进行数据绑定的例子。通过连接到 SQL Server 数据库并从“Titles”表中检索数据,展示如何在 DataGrid 控件中显示这些信息。此外,还实现了基于按钮命令的行样式更改功能。

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat=server>
void Page_Load(Object sender , EventArgs e)
{
 if (! IsPostBack ) {
  SqlConnection conPubs;
  SqlCommand cmdSelect;

        conPubs = new SqlConnection("server=IPROMISE;database=pubs;uid=sa;password=612121tjy;");
  cmdSelect = new SqlCommand( "Select * From Titles", conPubs );
  conPubs.Open();
  dgrdTitles.DataSource = cmdSelect.ExecuteReader();
  dgrdTitles.DataBind();
  conPubs.Close();
 }
}

void dgrdTitles_ItemCommand( object s, DataGridCommandEventArgs e ) {
 if ( e.CommandName == "select" ) {
  e.Item.BackColor = System.Drawing.Color.LightGreen;
  e.Item.Font.Bold = true;
 } else {
  e.Item.BackColor = System.Drawing.Color.White;
  e.Item.Font.Bold = false;

 }
}

</Script>

<html>
<head><title>DataGridButtonColumn.aspx</title></head>
<body>
<form Runat="Server">

<asp:DataGrid
  ID="dgrdTitles"
  OnItemCommand="dgrdTitles_ItemCommand"
  AutoGenerateColumns="False"
  CellPadding="10"
  Runat="Server">
<Columns>
  <asp:BoundColumn
    HeaderText="Book Titles"
    DataField="Title" />

  <asp:ButtonColumn
    CommandName="select"
    Text="Select!" />

  <asp:ButtonColumn
    CommandName="unselect"
    Text="UnSelect!" />

</Columns>
</asp:DataGrid>

</form>
</body>
</html>
 

# 气垫导轨实验系统 - 文件组织与实现 在 WinForms 项目中,核心实现代码和界面设计代码通常分别存放在不同的文件中,遵循 C# 的 partial 类设计模式。以下是完整的文件组织方案: ## 文件结构 ``` PhysicsExperimentMonitor/ ├── MainForm.cs // 核心逻辑代码 ├── MainForm.Designer.cs // 界面设计代码 ├── Program.cs // 应用程序入口 └── ExperimentData.cs // 实验数据模型(可选) ``` ## 1. MainForm.cs - 核心逻辑实现 ```csharp using System; using System.Collections.Generic; using System.Drawing; using System.IO.Ports; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace PhysicsExperimentMonitor { public partial class MainForm : Form { // 实验数据结构 public class ExperimentData { // 属性定义(与之前相同) // ... } // 成员变量 private SerialPort serialPort; private List<ExperimentData> experimentHistory = new List<ExperimentData>(); private int currentExperimentId = 1; private ExperimentData currentExperiment; // 当前实验参数 private double currentLightBlockWidth; private double currentGateDistance; private double currentSliderMass; private double currentWeightMass; // 正则表达式 private Regex timeRegex = new Regex(@"光电门(\d)挡光时间[::]\s*(\d+\.?\d*)\s*s"); private Regex velocityRegex = new Regex(@"通过光电门(\d)速度[::]\s*(\d+\.?\d*)\s*m/s"); private Regex accelRegex = new Regex(@"加速度[::]\s*(\d+\.?\d*)\s*m/s²"); private Regex dataStartRegex = new Regex(@"=+实验数据=+"); public MainForm() { InitializeComponent(); InitializeSerialPort(); InitializeChart(); InitializeDataGridView(); // 填充可用串口 cmbPorts.Items.AddRange(SerialPort.GetPortNames()); if (cmbPorts.Items.Count > 0) cmbPorts.SelectedIndex = 0; // 设置默认波特率 cmbBaudRate.SelectedIndex = 3; // 115200 // 设置默认参数值 txtLightBlockWidth.Text = "0.01"; txtGateDistance.Text = "0.3"; txtSliderMass.Text = "0.2"; txtWeightMass.Text = "0.05"; lblStatus.Text = "就绪"; btnEndExperiment.Enabled = false; } private void InitializeSerialPort() { serialPort = new SerialPort(); serialPort.DataReceived += SerialPort_DataReceived; } private void InitializeChart() { // 图表初始化代码(与之前相同) // ... } private void InitializeDataGridView() { // 数据表格初始化代码(与之前相同) // ... } // 启动新实验 private void btnStartExperiment_Click(object sender, EventArgs e) { // 启动实验逻辑(与之前相同) // ... } // 结束当前实验 private void btnEndExperiment_Click(object sender, EventArgs e) { // 结束实验逻辑(与之前相同) // ... } // 数据接收处理 private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { // 串口数据处理(与之前相同) // ... } // 处理串口数据 private void ProcessSerialData(string data) { // 数据解析逻辑(与之前相同) // ... } // 完成当前实验数据的收集 private void CompleteExperiment() { // 完成实验逻辑(与之前相同) // ... } // 更新数据显示 private void UpdateDataDisplay(ExperimentData data) { // 更新表格显示(与之前相同) // ... } // 更新图表 private void UpdateChart() { // 更新图表(与之前相同) // ... } // 连接串口 private void btnConnect_Click(object sender, EventArgs e) { // 串口连接逻辑(与之前相同) // ... } // 清空数据 private void btnClearData_Click(object sender, EventArgs e) { // 清空数据逻辑(与之前相同) // ... } // 参数输入验证 private void ValidateNumericInput(object sender, EventArgs e) { TextBox textBox = sender as TextBox; if (textBox != null) { if (!double.TryParse(textBox.Text, out double value) || value <= 0) { textBox.BackColor = Color.LightPink; btnStartExperiment.Enabled = false; } else { textBox.BackColor = SystemColors.Window; btnStartExperiment.Enabled = double.TryParse(txtLightBlockWidth.Text, out _) && double.TryParse(txtGateDistance.Text, out _) && double.TryParse(txtSliderMass.Text, out _) && double.TryParse(txtWeightMass.Text, out _); } } } // 关闭窗体时断开串口 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort != null && serialPort.IsOpen) serialPort.Close(); } } } ``` ## 2. MainForm.Designer.cs - 界面设计代码 ```csharp namespace PhysicsExperimentMonitor { partial class MainForm { private System.ComponentModel.IContainer components = null; // 控件声明 private ComboBox cmbPorts; private ComboBox cmbBaudRate; private Button btnConnect; private DataGridView dgvData; private Button btnExport; private RichTextBox txtLog; private Chart chartAcceleration; private Label label1; private Label label2; private TextBox txtLightBlockWidth; private TextBox txtGateDistance; private TextBox txtSliderMass; private TextBox txtWeightMass; private Label label3; private Label label4; private Label label5; private Label label6; private Button btnStartExperiment; private Button btnEndExperiment; private Label lblStatus; private Button btnClearData; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend(); System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series(); this.cmbPorts = new System.Windows.Forms.ComboBox(); this.cmbBaudRate = new System.Windows.Forms.ComboBox(); this.btnConnect = new System.Windows.Forms.Button(); this.dgvData = new System.Windows.Forms.DataGridView(); this.btnExport = new System.Windows.Forms.Button(); this.txtLog = new System.Windows.Forms.RichTextBox(); this.chartAcceleration = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.txtLightBlockWidth = new System.Windows.Forms.TextBox(); this.txtGateDistance = new System.Windows.Forms.TextBox(); this.txtSliderMass = new System.Windows.Forms.TextBox(); this.txtWeightMass = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.btnStartExperiment = new System.Windows.Forms.Button(); this.btnEndExperiment = new System.Windows.Forms.Button(); this.lblStatus = new System.Windows.Forms.Label(); this.btnClearData = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dgvData)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.chartAcceleration)).BeginInit(); this.SuspendLayout(); // cmbPorts this.cmbPorts.FormattingEnabled = true; this.cmbPorts.Location = new System.Drawing.Point(20, 20); this.cmbPorts.Name = "cmbPorts"; this.cmbPorts.Size = new System.Drawing.Size(120, 28); this.cmbPorts.TabIndex = 0; // cmbBaudRate this.cmbBaudRate.FormattingEnabled = true; this.cmbBaudRate.Items.AddRange(new object[] { "9600", "19200", "38400", "57600", "115200"}); this.cmbBaudRate.Location = new System.Drawing.Point(150, 20); this.cmbBaudRate.Name = "cmbBaudRate"; this.cmbBaudRate.Size = new System.Drawing.Size(120, 28); this.cmbBaudRate.TabIndex = 1; // btnConnect this.btnConnect.Location = new System.Drawing.Point(280, 20); this.btnConnect.Name = "btnConnect"; this.btnConnect.Size = new System.Drawing.Size(120, 30); this.btnConnect.TabIndex = 2; this.btnConnect.Text = "连接串口"; this.btnConnect.UseVisualStyleBackColor = true; this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click); // dgvData this.dgvData.AllowUserToAddRows = false; this.dgvData.AllowUserToDeleteRows = false; this.dgvData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.dgvData.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; this.dgvData.BackgroundColor = System.Drawing.SystemColors.ControlLightLight; this.dgvData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dgvData.Location = new System.Drawing.Point(20, 220); this.dgvData.Name = "dgvData"; this.dgvData.ReadOnly = true; this.dgvData.RowHeadersWidth = 51; this.dgvData.RowTemplate.Height = 24; this.dgvData.Size = new System.Drawing.Size(1200, 250); this.dgvData.TabIndex = 3; // btnExport this.btnExport.Location = new System.Drawing.Point(410, 20); this.btnExport.Name = "btnExport"; this.btnExport.Size = new System.Drawing.Size(120, 30); this.btnExport.TabIndex = 4; this.btnExport.Text = "导出数据"; this.btnExport.UseVisualStyleBackColor = true; this.btnExport.Click += new System.EventHandler(this.btnExport_Click); // txtLog this.txtLog.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txtLog.BackColor = System.Drawing.Color.Black; this.txtLog.Font = new System.Drawing.Font("Consolas", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.txtLog.ForeColor = System.Drawing.Color.White; this.txtLog.Location = new System.Drawing.Point(20, 480); this.txtLog.Name = "txtLog"; this.txtLog.Size = new System.Drawing.Size(1200, 150); this.txtLog.TabIndex = 5; this.txtLog.Text = ""; // chartAcceleration this.chartAcceleration.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); chartArea1.Name = "ChartArea1"; this.chartAcceleration.ChartAreas.Add(chartArea1); legend1.Name = "Legend1"; this.chartAcceleration.Legends.Add(legend1); this.chartAcceleration.Location = new System.Drawing.Point(430, 60); this.chartAcceleration.Name = "chartAcceleration"; series1.ChartArea = "ChartArea1"; series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series1.Legend = "Legend1"; series1.Name = "实测加速度"; series2.ChartArea = "ChartArea1"; series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series2.Legend = "Legend1"; series2.Name = "理论加速度"; series3.ChartArea = "ChartArea1"; series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series3.Legend = "Legend1"; series3.Name = "单片机加速度"; series4.ChartArea = "ChartArea1"; series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series4.Legend = "Legend1"; series4.Name = "误差(%)"; this.chartAcceleration.Series.Add(series1); this.chartAcceleration.Series.Add(series2); this.chartAcceleration.Series.Add(series3); this.chartAcceleration.Series.Add(series4); this.chartAcceleration.Size = new System.Drawing.Size(790, 150); this.chartAcceleration.TabIndex = 6; this.chartAcceleration.Text = "chart1"; // label1 this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(20, 70); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(117, 20); this.label1.TabIndex = 7; this.label1.Text = "挡光片宽度(m):"; // label2 this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(20, 100); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(117, 20); this.label2.TabIndex = 8; this.label2.Text = "光电门间距(m):"; // txtLightBlockWidth this.txtLightBlockWidth.Location = new System.Drawing.Point(150, 67); this.txtLightBlockWidth.Name = "txtLightBlockWidth"; this.txtLightBlockWidth.Size = new System.Drawing.Size(100, 27); this.txtLightBlockWidth.TabIndex = 9; this.txtLightBlockWidth.Text = "0.01"; this.txtLightBlockWidth.TextChanged += new System.EventHandler(this.ValidateNumericInput); // txtGateDistance this.txtGateDistance.Location = new System.Drawing.Point(150, 97); this.txtGateDistance.Name = "txtGateDistance"; this.txtGateDistance.Size = new System.Drawing.Size(100, 27); this.txtGateDistance.TabIndex = 10; this.txtGateDistance.Text = "0.3"; this.txtGateDistance.TextChanged += new System.EventHandler(this.ValidateNumericInput); // txtSliderMass this.txtSliderMass.Location = new System.Drawing.Point(150, 127); this.txtSliderMass.Name = "txtSliderMass"; this.txtSliderMass.Size = new System.Drawing.Size(100, 27); this.txtSliderMass.TabIndex = 12; this.txtSliderMass.Text = "0.2"; this.txtSliderMass.TextChanged += new System.EventHandler(this.ValidateNumericInput); // txtWeightMass this.txtWeightMass.Location = new System.Drawing.Point(150, 157); this.txtWeightMass.Name = "txtWeightMass"; this.txtWeightMass.Size = new System.Drawing.Size(100, 27); this.txtWeightMass.TabIndex = 13; this.txtWeightMass.Text = "0.05"; this.txtWeightMass.TextChanged += new System.EventHandler(this.ValidateNumericInput); // label3 this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(20, 130); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(117, 20); this.label3.TabIndex = 14; this.label3.Text = "滑块质量(kg):"; // label4 this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(20, 160); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(117, 20); this.label4.TabIndex = 15; this.label4.Text = "砝码质量(kg):"; // label5 this.label5.AutoSize = true; this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label5.Location = new System.Drawing.Point(280, 70); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(130, 20); this.label5.TabIndex = 16; this.label5.Text = "实验参数设置"; // label6 this.label6.AutoSize = true; this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label6.Location = new System.Drawing.Point(20, 190); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(130, 20); this.label6.TabIndex = 17; this.label6.Text = "实验数据表格"; // btnStartExperiment this.btnStartExperiment.BackColor = System.Drawing.Color.LightGreen; this.btnStartExperiment.Location = new System.Drawing.Point(280, 100); this.btnStartExperiment.Name = "btnStartExperiment"; this.btnStartExperiment.Size = new System.Drawing.Size(120, 40); this.btnStartExperiment.TabIndex = 18; this.btnStartExperiment.Text = "开始实验"; this.btnStartExperiment.UseVisualStyleBackColor = false; this.btnStartExperiment.Click += new System.EventHandler(this.btnStartExperiment_Click); // btnEndExperiment this.btnEndExperiment.BackColor = System.Drawing.Color.LightCoral; this.btnEndExperiment.Location = new System.Drawing.Point(280, 150); this.btnEndExperiment.Name = "btnEndExperiment"; this.btnEndExperiment.Size = new System.Drawing.Size(120, 40); this.btnEndExperiment.TabIndex = 19; this.btnEndExperiment.Text = "结束实验"; this.btnEndExperiment.UseVisualStyleBackColor = false; this.btnEndExperiment.Click += new System.EventHandler(this.btnEndExperiment_Click); // lblStatus this.lblStatus.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lblStatus.BackColor = System.Drawing.SystemColors.Info; this.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lblStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblStatus.Location = new System.Drawing.Point(430, 20); this.lblStatus.Name = "lblStatus"; this.lblStatus.Size = new System.Drawing.Size(790, 30); this.lblStatus.TabIndex = 20; this.lblStatus.Text = "就绪"; this.lblStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // btnClearData this.btnClearData.BackColor = System.Drawing.Color.LightSalmon; this.btnClearData.Location = new System.Drawing.Point(410, 150); this.btnClearData.Name = "btnClearData"; this.btnClearData.Size = new System.Drawing.Size(120, 40); this.btnClearData.TabIndex = 21; this.btnClearData.Text = "清空数据"; this.btnClearData.UseVisualStyleBackColor = false; this.btnClearData.Click += new System.EventHandler(this.btnClearData_Click); // MainForm this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1240, 650); this.Controls.Add(this.btnClearData); this.Controls.Add(this.lblStatus); this.Controls.Add(this.btnEndExperiment); this.Controls.Add(this.btnStartExperiment); this.Controls.Add(this.label6); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.txtWeightMass); this.Controls.Add(this.txtSliderMass); this.Controls.Add(this.txtGateDistance); this.Controls.Add(this.txtLightBlockWidth); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.chartAcceleration); this.Controls.Add(this.txtLog); this.Controls.Add(this.btnExport); this.Controls.Add(this.dgvData); this.Controls.Add(this.btnConnect); this.Controls.Add(this.cmbBaudRate); this.Controls.Add(this.cmbPorts); this.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Margin = new System.Windows.Forms.Padding(4); this.Name = "MainForm"; this.Text = "气垫导轨实验系统"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); ((System.ComponentModel.ISupportInitialize)(this.dgvData)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.chartAcceleration)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } } } ``` ## 3. Program.cs - 应用程序入口 ```csharp using System; using System.Windows.Forms; namespace PhysicsExperimentMonitor { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } } ``` ## 4. ExperimentData.cs - 数据模型(可选) ```csharp using System; namespace PhysicsExperimentMonitor { public class ExperimentData { public int ExperimentID { get; set; } public DateTime Timestamp { get; set; } public double LightBlockWidth { get; set; } public double GateDistance { get; set; } public double TimeGate1 { get; set; } public double TimeGate2 { get; set; } public double SliderMass { get; set; } public double WeightMass { get; set; } public double Velocity1 => LightBlockWidth / TimeGate1; public double Velocity2 => LightBlockWidth / TimeGate2; public double TotalMass => SliderMass + WeightMass; public double PullForce => WeightMass * 9.8; public double Acceleration => (Velocity2 * Velocity2 - Velocity1 * Velocity1) / (2 * GateDistance); public double ExpectedAcceleration => PullForce / TotalMass; public double MCU_Velocity1 { get; set; } public double MCU_Velocity2 { get; set; } public double MCU_Acceleration { get; set; } public double ErrorPercentage => Math.Abs(Acceleration - ExpectedAcceleration) / ExpectedAcceleration * 100; } } ``` ## 系统实现要点 1. **Partial 类设计**: - `MainForm.cs` 包含业务逻辑和事件处理 - `MainForm.Designer.cs` 包含界面布局和控件初始化 - 两部分通过 `partial class MainForm` 组合 2. **数据流处理**: - 串口数据接收 → 正则表达式解析 → 数据存储 → UI 更新 3. **参数动态设置**: - 每次实验前可修改挡光片宽度、光电门间距等参数 - 参数验证确保输入有效性 4. **多行数据解析**: - 使用正则表达式处理不同格式的数据行 - 自动识别数据开始标记和结束标记 5. **错误处理**: - 参数验证防止无效输入 - 异常捕获确保程序稳定性 - 状态指示器显示当前系统状态
11-23
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.IO.Ports; using ScottPlot; using System.IO; namespace PhysicsExperimentDisplay { // 关键修复1:添加partial修饰符 public partial class MainForm : Form { // 控件声明 private TableLayoutPanel mainLayout; private GroupBox gbSerial; private ComboBox cmbPorts; private ComboBox cmbBaudRate; private Button btnConnect; private Label lblPort; private Label lblBaud; private GroupBox gbData; private DataGridView dgvResults; private GroupBox gbChart; private FormsPlot formsPlot; private Button btnExport; private Button btnClear; private SerialPort serialPort; private FlowLayoutPanel buttonPanel; // 数据存储 private List<double> accelerationData = new List<double>(); private TableLayoutPanel serialLayout; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn5; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn6; private int dataPointCount = 0; public MainForm() { // 关键修复2:确保只有一个InitializeComponent调用 InitializeComponent(); LoadAvailablePorts(); } // 关键修复3:明确的InitializeComponent实现 private void InitializeComponent() { this.mainLayout = new System.Windows.Forms.TableLayoutPanel(); this.gbSerial = new System.Windows.Forms.GroupBox(); this.serialLayout = new System.Windows.Forms.TableLayoutPanel(); this.lblPort = new System.Windows.Forms.Label(); this.cmbPorts = new System.Windows.Forms.ComboBox(); this.lblBaud = new System.Windows.Forms.Label(); this.cmbBaudRate = new System.Windows.Forms.ComboBox(); this.btnConnect = new System.Windows.Forms.Button(); this.buttonPanel = new System.Windows.Forms.FlowLayoutPanel(); this.btnExport = new System.Windows.Forms.Button(); this.btnClear = new System.Windows.Forms.Button(); this.gbData = new System.Windows.Forms.GroupBox(); this.dgvResults = new System.Windows.Forms.DataGridView(); this.gbChart = new System.Windows.Forms.GroupBox(); this.formsPlot = new ScottPlot.FormsPlot(); this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dataGridViewTextBoxColumn5 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dataGridViewTextBoxColumn6 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.mainLayout.SuspendLayout(); this.gbSerial.SuspendLayout(); this.serialLayout.SuspendLayout(); this.buttonPanel.SuspendLayout(); this.gbData.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dgvResults)).BeginInit(); this.gbChart.SuspendLayout(); this.SuspendLayout(); // // mainLayout // this.mainLayout.ColumnCount = 1; this.mainLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F)); this.mainLayout.Controls.Add(this.gbSerial, 0, 0); this.mainLayout.Controls.Add(this.buttonPanel, 0, 1); this.mainLayout.Controls.Add(this.gbData, 0, 2); this.mainLayout.Controls.Add(this.gbChart, 0, 3); this.mainLayout.Dock = System.Windows.Forms.DockStyle.Fill; this.mainLayout.Location = new System.Drawing.Point(0, 0); this.mainLayout.Name = "mainLayout"; this.mainLayout.RowCount = 4; this.mainLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 100F)); this.mainLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40F)); this.mainLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 40F)); this.mainLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 60F)); this.mainLayout.Size = new System.Drawing.Size(1178, 744); this.mainLayout.TabIndex = 0; // // gbSerial // this.gbSerial.Controls.Add(this.serialLayout); this.gbSerial.Dock = System.Windows.Forms.DockStyle.Fill; this.gbSerial.Location = new System.Drawing.Point(10, 10); this.gbSerial.Margin = new System.Windows.Forms.Padding(10); this.gbSerial.Name = "gbSerial"; this.gbSerial.Size = new System.Drawing.Size(1158, 80); this.gbSerial.TabIndex = 0; this.gbSerial.TabStop = false; this.gbSerial.Text = "串口设置"; // // serialLayout // this.serialLayout.ColumnCount = 4; this.serialLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 100F)); this.serialLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F)); this.serialLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 100F)); this.serialLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F)); this.serialLayout.Controls.Add(this.lblPort, 0, 0); this.serialLayout.Controls.Add(this.cmbPorts, 1, 0); this.serialLayout.Controls.Add(this.lblBaud, 2, 0); this.serialLayout.Controls.Add(this.cmbBaudRate, 3, 0); this.serialLayout.Controls.Add(this.btnConnect, 0, 1); this.serialLayout.Dock = System.Windows.Forms.DockStyle.Fill; this.serialLayout.Location = new System.Drawing.Point(3, 29); this.serialLayout.Name = "serialLayout"; this.serialLayout.RowCount = 2; this.serialLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); this.serialLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); this.serialLayout.Size = new System.Drawing.Size(1152, 48); this.serialLayout.TabIndex = 0; // // lblPort // this.lblPort.Location = new System.Drawing.Point(3, 0); this.lblPort.Name = "lblPort"; this.lblPort.Size = new System.Drawing.Size(94, 20); this.lblPort.TabIndex = 0; this.lblPort.Text = "串口号:"; this.lblPort.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // cmbPorts // this.cmbPorts.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbPorts.Font = new System.Drawing.Font("Consolas", 10F); this.cmbPorts.Location = new System.Drawing.Point(103, 3); this.cmbPorts.Name = "cmbPorts"; this.cmbPorts.Size = new System.Drawing.Size(121, 31); this.cmbPorts.TabIndex = 1; // // lblBaud // this.lblBaud.Location = new System.Drawing.Point(579, 0); this.lblBaud.Name = "lblBaud"; this.lblBaud.Size = new System.Drawing.Size(94, 20); this.lblBaud.TabIndex = 2; this.lblBaud.Text = "波特率:"; this.lblBaud.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // cmbBaudRate // this.cmbBaudRate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbBaudRate.Font = new System.Drawing.Font("Consolas", 10F); this.cmbBaudRate.Location = new System.Drawing.Point(679, 3); this.cmbBaudRate.Name = "cmbBaudRate"; this.cmbBaudRate.Size = new System.Drawing.Size(121, 31); this.cmbBaudRate.TabIndex = 3; // // btnConnect // this.btnConnect.BackColor = System.Drawing.Color.LightGreen; this.serialLayout.SetColumnSpan(this.btnConnect, 4); this.btnConnect.Dock = System.Windows.Forms.DockStyle.Fill; this.btnConnect.Font = new System.Drawing.Font("Microsoft YaHei UI", 10F, System.Drawing.FontStyle.Bold); this.btnConnect.Location = new System.Drawing.Point(20, 25); this.btnConnect.Margin = new System.Windows.Forms.Padding(20, 5, 20, 5); this.btnConnect.Name = "btnConnect"; this.btnConnect.Size = new System.Drawing.Size(1112, 18); this.btnConnect.TabIndex = 4; this.btnConnect.Text = "连接串口"; this.btnConnect.UseVisualStyleBackColor = false; // // buttonPanel // this.buttonPanel.Controls.Add(this.btnExport); this.buttonPanel.Controls.Add(this.btnClear); this.buttonPanel.Dock = System.Windows.Forms.DockStyle.Fill; this.buttonPanel.Location = new System.Drawing.Point(3, 103); this.buttonPanel.Name = "buttonPanel"; this.buttonPanel.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0); this.buttonPanel.Size = new System.Drawing.Size(1172, 34); this.buttonPanel.TabIndex = 1; // // btnExport // this.btnExport.BackColor = System.Drawing.Color.LightBlue; this.btnExport.Location = new System.Drawing.Point(13, 3); this.btnExport.Name = "btnExport"; this.btnExport.Size = new System.Drawing.Size(150, 35); this.btnExport.TabIndex = 0; this.btnExport.Text = "导出数据(CSV)"; this.btnExport.UseVisualStyleBackColor = false; // // btnClear // this.btnClear.BackColor = System.Drawing.Color.LightSalmon; this.btnClear.Location = new System.Drawing.Point(169, 3); this.btnClear.Name = "btnClear"; this.btnClear.Size = new System.Drawing.Size(150, 35); this.btnClear.TabIndex = 1; this.btnClear.Text = "清除数据"; this.btnClear.UseVisualStyleBackColor = false; // // gbData // this.gbData.Controls.Add(this.dgvResults); this.gbData.Dock = System.Windows.Forms.DockStyle.Fill; this.gbData.Location = new System.Drawing.Point(10, 150); this.gbData.Margin = new System.Windows.Forms.Padding(10); this.gbData.Name = "gbData"; this.gbData.Size = new System.Drawing.Size(1158, 221); this.gbData.TabIndex = 2; this.gbData.TabStop = false; this.gbData.Text = "实验数据"; // // dgvResults // this.dgvResults.AllowUserToAddRows = false; this.dgvResults.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; this.dgvResults.BackgroundColor = System.Drawing.SystemColors.Window; this.dgvResults.BorderStyle = System.Windows.Forms.BorderStyle.None; this.dgvResults.ColumnHeadersHeight = 34; this.dgvResults.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.dataGridViewTextBoxColumn1, this.dataGridViewTextBoxColumn2, this.dataGridViewTextBoxColumn3, this.dataGridViewTextBoxColumn4, this.dataGridViewTextBoxColumn5, this.dataGridViewTextBoxColumn6}); this.dgvResults.Dock = System.Windows.Forms.DockStyle.Fill; this.dgvResults.Location = new System.Drawing.Point(3, 29); this.dgvResults.Name = "dgvResults"; this.dgvResults.ReadOnly = true; this.dgvResults.RowHeadersVisible = false; this.dgvResults.RowHeadersWidth = 62; this.dgvResults.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.dgvResults.Size = new System.Drawing.Size(1152, 189); this.dgvResults.TabIndex = 0; // // gbChart // this.gbChart.Controls.Add(this.formsPlot); this.gbChart.Dock = System.Windows.Forms.DockStyle.Fill; this.gbChart.Location = new System.Drawing.Point(10, 391); this.gbChart.Margin = new System.Windows.Forms.Padding(10); this.gbChart.Name = "gbChart"; this.gbChart.Size = new System.Drawing.Size(1158, 343); this.gbChart.TabIndex = 3; this.gbChart.TabStop = false; this.gbChart.Text = "加速度变化曲线"; // // formsPlot // this.formsPlot.Dock = System.Windows.Forms.DockStyle.Fill; this.formsPlot.Location = new System.Drawing.Point(3, 29); this.formsPlot.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); this.formsPlot.Name = "formsPlot"; this.formsPlot.Size = new System.Drawing.Size(1152, 311); this.formsPlot.TabIndex = 0; // // dataGridViewTextBoxColumn1 // this.dataGridViewTextBoxColumn1.HeaderText = "时间"; this.dataGridViewTextBoxColumn1.MinimumWidth = 8; this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1"; this.dataGridViewTextBoxColumn1.ReadOnly = true; // // dataGridViewTextBoxColumn2 // this.dataGridViewTextBoxColumn2.HeaderText = "T1(s)"; this.dataGridViewTextBoxColumn2.MinimumWidth = 8; this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2"; this.dataGridViewTextBoxColumn2.ReadOnly = true; // // dataGridViewTextBoxColumn3 // this.dataGridViewTextBoxColumn3.HeaderText = "T2(s)"; this.dataGridViewTextBoxColumn3.MinimumWidth = 8; this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3"; this.dataGridViewTextBoxColumn3.ReadOnly = true; // // dataGridViewTextBoxColumn4 // this.dataGridViewTextBoxColumn4.HeaderText = "V1(m/s)"; this.dataGridViewTextBoxColumn4.MinimumWidth = 8; this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4"; this.dataGridViewTextBoxColumn4.ReadOnly = true; // // dataGridViewTextBoxColumn5 // this.dataGridViewTextBoxColumn5.HeaderText = "V2(m/s)"; this.dataGridViewTextBoxColumn5.MinimumWidth = 8; this.dataGridViewTextBoxColumn5.Name = "dataGridViewTextBoxColumn5"; this.dataGridViewTextBoxColumn5.ReadOnly = true; // // dataGridViewTextBoxColumn6 // this.dataGridViewTextBoxColumn6.HeaderText = "加速度(m/s²)"; this.dataGridViewTextBoxColumn6.MinimumWidth = 8; this.dataGridViewTextBoxColumn6.Name = "dataGridViewTextBoxColumn6"; this.dataGridViewTextBoxColumn6.ReadOnly = true; // // MainForm // this.ClientSize = new System.Drawing.Size(1178, 744); this.Controls.Add(this.mainLayout); this.Font = new System.Drawing.Font("Microsoft YaHei UI", 10F); this.Name = "MainForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "气垫导轨实验数据监控"; this.mainLayout.ResumeLayout(false); this.gbSerial.ResumeLayout(false); this.serialLayout.ResumeLayout(false); this.buttonPanel.ResumeLayout(false); this.gbData.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dgvResults)).EndInit(); this.gbChart.ResumeLayout(false); this.ResumeLayout(false); } // 初始化图表 private void InitializePlot() { var plt = formsPlot.Plot; // 设置图表样式 plt.Title("加速度变化曲线", size: 16); plt.XLabel("实验序号"); plt.YLabel("加速度 (m/s²)"); plt.Grid(enable: true, color: Color.LightGray); plt.Style(ScottPlot.Style.Blue1); // 添加数据序列 plt.AddSignal(new double[] { 0 }, 1, Color.Blue, label: "加速度"); // 添加图例 plt.Legend(location: Alignment.LowerRight); plt.SetAxisLimits(0, 10, 0, 10); } // 加载可用串口 private void LoadAvailablePorts() { cmbPorts.Items.Clear(); string[] ports = SerialPort.GetPortNames(); cmbPorts.Items.AddRange(ports); if (ports.Length > 0) cmbPorts.SelectedIndex = 0; // 设置波特率选项 cmbBaudRate.Items.AddRange(new object[] { 9600, 19200, 38400, 57600, 115200 }); cmbBaudRate.SelectedIndex = 4; // 默认115200 } // 连接/断开串口 private void btnConnect_Click(object sender, EventArgs e) { if (serialPort == null || !serialPort.IsOpen) { try { serialPort = new SerialPort(cmbPorts.Text, (int)cmbBaudRate.SelectedItem); serialPort.DataReceived += SerialPort_DataReceived; serialPort.Open(); btnConnect.Text = "断开连接"; btnConnect.BackColor = Color.LightCoral; } catch (Exception ex) { MessageBox.Show($"串口连接错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { serialPort.Close(); serialPort = null; btnConnect.Text = "连接串口"; btnConnect.BackColor = Color.LightGreen; } } // 串口数据接收处理 private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { string data = serialPort.ReadLine(); this.Invoke(new Action(() => ProcessData(data))); } catch (Exception ex) { Console.WriteLine($"数据接收错误: {ex.Message}"); } } // 数据处理和显示 private void ProcessData(string rawData) { /* 数据格式示例: * "T1:0.025,T2:0.023,V1:0.4,V2:0.44,A:0.2" */ try { string[] pairs = rawData.Split(','); double t1 = 0, t2 = 0, v1 = 0, v2 = 0, acc = 0; foreach (string pair in pairs) { string[] keyValue = pair.Split(':'); if (keyValue.Length != 2) continue; switch (keyValue[0]) { case "T1": t1 = double.Parse(keyValue[1]); break; case "T2": t2 = double.Parse(keyValue[1]); break; case "V1": v1 = double.Parse(keyValue[1]); break; case "V2": v2 = double.Parse(keyValue[1]); break; case "A": acc = double.Parse(keyValue[1]); break; } } // 添加新行到DataGridView int rowIndex = dgvResults.Rows.Add(); dgvResults.Rows[rowIndex].Cells["Time"].Value = DateTime.Now.ToString("HH:mm:ss.fff"); dgvResults.Rows[rowIndex].Cells["T1"].Value = t1; dgvResults.Rows[rowIndex].Cells["T2"].Value = t2; dgvResults.Rows[rowIndex].Cells["V1"].Value = v1; dgvResults.Rows[rowIndex].Cells["V2"].Value = v2; dgvResults.Rows[rowIndex].Cells["Acc"].Value = acc; // 自动滚动到最后一行 dgvResults.FirstDisplayedScrollingRowIndex = dgvResults.RowCount - 1; // 更新图表 UpdatePlot(acc); } catch (Exception ex) { Console.WriteLine($"数据处理错误: {ex.Message}"); } } // 更新图表数据 private void UpdatePlot(double acc) { var plt = formsPlot.Plot; // 添加新数据点 accelerationData.Add(acc); dataPointCount++; // 限制显示点数 if (accelerationData.Count > 100) { accelerationData.RemoveAt(0); } // 清除原有图表数据 plt.Clear(); // 添加新数据 plt.AddSignal(accelerationData.ToArray(), 1, label: "加速度", color: Color.Blue); // 设置X轴范围 plt.SetAxisLimitsX(0, accelerationData.Count); // 自动调整Y轴范围 double min = double.MaxValue; double max = double.MinValue; foreach (double val in accelerationData) { if (val < min) min = val; if (val > max) max = val; } // 添加一些边距 double range = max - min; if (range < 0.1) range = 0.1; // 避免过小范围 min = Math.Max(0, min - range * 0.1); max += range * 0.1; plt.SetAxisLimitsY(min, max); // 刷新图表 formsPlot.Render(); } // 导出数据到CSV private void btnExport_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "CSV文件|*.csv"; sfd.Title = "保存实验数据"; if (sfd.ShowDialog() == DialogResult.OK) { try { using (StreamWriter sw = new StreamWriter(sfd.FileName)) { // 写入列标题 sw.WriteLine("时间,T1(s),T2(s),V1(m/s),V2(m/s),加速度(m/s²)"); // 写入数据 foreach (DataGridViewRow row in dgvResults.Rows) { if (!row.IsNewRow) { sw.WriteLine( $"{row.Cells["Time"].Value}," + $"{row.Cells["T1"].Value}," + $"{row.Cells["T2"].Value}," + $"{row.Cells["V1"].Value}," + $"{row.Cells["V2"].Value}," + $"{row.Cells["Acc"].Value}"); } } } MessageBox.Show("数据导出成功!", "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"导出失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } // 清除数据 private void btnClear_Click(object sender, EventArgs e) { if (MessageBox.Show("确定要清除所有数据吗?", "确认清除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { dgvResults.Rows.Clear(); accelerationData.Clear(); dataPointCount = 0; // 清除图表数据 var plt = formsPlot.Plot; plt.Clear(); plt.AddSignal(new double[] { 0 }, 1, label: "加速度", color: Color.Blue); plt.SetAxisLimits(0, 10, 0, 10); formsPlot.Render(); } } // 程序入口点 [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } } 请你根据这个代码优化一下实验数据导出,要求:导出文件可见且为excel表格
11-16
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.IO.Ports; using System.IO; using System.Globalization; using System.Data; using System.Windows.Forms.DataVisualization.Charting; using OfficeOpenXml; using OfficeOpenXml.Style; using OfficeOpenXml.Drawing.Chart; namespace PhysicsExperimentDisplay { public partial class MainForm : Form { // 控件声明 private TableLayoutPanel mainLayout; private GroupBox gbSerial; private ComboBox cmbPorts; private ComboBox cmbBaudRate; private Button btnConnect; private Label lblPort; private Label lblBaud; private GroupBox gbData; private DataGridView dgvResults; private GroupBox gbChart; private Button btnExport; private Button btnClear; private SerialPort serialPort; private FlowLayoutPanel buttonPanel; private TableLayoutPanel serialLayout; private Chart chartPlot; // 数据存储 private List<double> accelerationData = new List<double>(); private int dataPointCount = 0; private DataGridViewTextBoxColumn Time; private DataGridViewTextBoxColumn T1; private DataGridViewTextBoxColumn T2; private DataGridViewTextBoxColumn V1; private DataGridViewTextBoxColumn V2; private DataGridViewTextBoxColumn Acc; private DateTime experimentStartTime = DateTime.Now; public MainForm() { InitializeComponent(); LoadAvailablePorts(); InitializePlot(); // 绑定按钮事件 btnConnect.Click += btnConnect_Click; btnExport.Click += btnExport_Click; btnClear.Click += btnClear_Click; } private void InitializeComponent() { this.mainLayout = new System.Windows.Forms.TableLayoutPanel(); this.gbSerial = new System.Windows.Forms.GroupBox(); this.serialLayout = new System.Windows.Forms.TableLayoutPanel(); this.lblPort = new System.Windows.Forms.Label(); this.cmbPorts = new System.Windows.Forms.ComboBox(); this.lblBaud = new System.Windows.Forms.Label(); this.cmbBaudRate = new System.Windows.Forms.ComboBox(); this.btnConnect = new System.Windows.Forms.Button(); this.buttonPanel = new System.Windows.Forms.FlowLayoutPanel(); this.btnExport = new System.Windows.Forms.Button(); this.btnClear = new System.Windows.Forms.Button(); this.gbData = new System.Windows.Forms.GroupBox(); this.dgvResults = new System.Windows.Forms.DataGridView(); this.Time = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.T1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.T2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.V1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.V2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Acc = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.gbChart = new System.Windows.Forms.GroupBox(); this.chartPlot = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.mainLayout.SuspendLayout(); this.gbSerial.SuspendLayout(); this.serialLayout.SuspendLayout(); this.buttonPanel.SuspendLayout(); this.gbData.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dgvResults)).BeginInit(); this.gbChart.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.chartPlot)).BeginInit(); this.SuspendLayout(); // // mainLayout // this.mainLayout.ColumnCount = 1; this.mainLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F)); this.mainLayout.Controls.Add(this.gbSerial, 0, 0); this.mainLayout.Controls.Add(this.buttonPanel, 0, 1); this.mainLayout.Controls.Add(this.gbData, 0, 2); this.mainLayout.Controls.Add(this.gbChart, 0, 3); this.mainLayout.Dock = System.Windows.Forms.DockStyle.Fill; this.mainLayout.Location = new System.Drawing.Point(0, 0); this.mainLayout.Name = "mainLayout"; this.mainLayout.Padding = new System.Windows.Forms.Padding(10); this.mainLayout.RowCount = 4; this.mainLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 100F)); this.mainLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50F)); this.mainLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 40F)); this.mainLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 60F)); this.mainLayout.Size = new System.Drawing.Size(1178, 744); this.mainLayout.TabIndex = 0; // // gbSerial // this.gbSerial.Controls.Add(this.serialLayout); this.gbSerial.Dock = System.Windows.Forms.DockStyle.Fill; this.gbSerial.Location = new System.Drawing.Point(13, 13); this.gbSerial.Name = "gbSerial"; this.gbSerial.Size = new System.Drawing.Size(1152, 94); this.gbSerial.TabIndex = 0; this.gbSerial.TabStop = false; this.gbSerial.Text = "串口设置"; // // serialLayout // this.serialLayout.ColumnCount = 4; this.serialLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 100F)); this.serialLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F)); this.serialLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 100F)); this.serialLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F)); this.serialLayout.Controls.Add(this.lblPort, 0, 0); this.serialLayout.Controls.Add(this.cmbPorts, 1, 0); this.serialLayout.Controls.Add(this.lblBaud, 2, 0); this.serialLayout.Controls.Add(this.cmbBaudRate, 3, 0); this.serialLayout.Controls.Add(this.btnConnect, 0, 1); this.serialLayout.Dock = System.Windows.Forms.DockStyle.Fill; this.serialLayout.Location = new System.Drawing.Point(3, 29); this.serialLayout.Name = "serialLayout"; this.serialLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); this.serialLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); this.serialLayout.Size = new System.Drawing.Size(1146, 62); this.serialLayout.TabIndex = 0; // // lblPort // this.lblPort.Location = new System.Drawing.Point(3, 0); this.lblPort.Name = "lblPort"; this.lblPort.Size = new System.Drawing.Size(94, 20); this.lblPort.TabIndex = 0; this.lblPort.Text = "串口号:"; this.lblPort.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // cmbPorts // this.cmbPorts.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbPorts.Font = new System.Drawing.Font("Consolas", 10F); this.cmbPorts.Location = new System.Drawing.Point(103, 3); this.cmbPorts.Name = "cmbPorts"; this.cmbPorts.Size = new System.Drawing.Size(121, 31); this.cmbPorts.TabIndex = 1; // // lblBaud // this.lblBaud.Location = new System.Drawing.Point(576, 0); this.lblBaud.Name = "lblBaud"; this.lblBaud.Size = new System.Drawing.Size(94, 20); this.lblBaud.TabIndex = 2; this.lblBaud.Text = "波特率:"; this.lblBaud.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // cmbBaudRate // this.cmbBaudRate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbBaudRate.Font = new System.Drawing.Font("Consolas", 10F); this.cmbBaudRate.Location = new System.Drawing.Point(676, 3); this.cmbBaudRate.Name = "cmbBaudRate"; this.cmbBaudRate.Size = new System.Drawing.Size(121, 31); this.cmbBaudRate.TabIndex = 3; // // btnConnect // this.btnConnect.BackColor = System.Drawing.Color.LightGreen; this.serialLayout.SetColumnSpan(this.btnConnect, 4); this.btnConnect.Font = new System.Drawing.Font("Microsoft YaHei UI", 10F, System.Drawing.FontStyle.Bold); this.btnConnect.Location = new System.Drawing.Point(3, 23); this.btnConnect.Name = "btnConnect"; this.btnConnect.Size = new System.Drawing.Size(75, 23); this.btnConnect.TabIndex = 4; this.btnConnect.Text = "连接串口"; this.btnConnect.UseVisualStyleBackColor = false; // // buttonPanel // this.buttonPanel.Controls.Add(this.btnExport); this.buttonPanel.Controls.Add(this.btnClear); this.buttonPanel.Dock = System.Windows.Forms.DockStyle.Fill; this.buttonPanel.Location = new System.Drawing.Point(13, 113); this.buttonPanel.Name = "buttonPanel"; this.buttonPanel.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0); this.buttonPanel.Size = new System.Drawing.Size(1152, 44); this.buttonPanel.TabIndex = 1; // // btnExport // this.btnExport.BackColor = System.Drawing.Color.LightBlue; this.btnExport.Location = new System.Drawing.Point(13, 3); this.btnExport.Name = "btnExport"; this.btnExport.Size = new System.Drawing.Size(180, 40); this.btnExport.TabIndex = 0; this.btnExport.Text = "导出数据(Excel)"; this.btnExport.UseVisualStyleBackColor = false; // // btnClear // this.btnClear.BackColor = System.Drawing.Color.LightSalmon; this.btnClear.Location = new System.Drawing.Point(199, 3); this.btnClear.Name = "btnClear"; this.btnClear.Size = new System.Drawing.Size(180, 40); this.btnClear.TabIndex = 1; this.btnClear.Text = "清除数据"; this.btnClear.UseVisualStyleBackColor = false; // // gbData // this.gbData.Controls.Add(this.dgvResults); this.gbData.Dock = System.Windows.Forms.DockStyle.Fill; this.gbData.Location = new System.Drawing.Point(13, 163); this.gbData.Name = "gbData"; this.gbData.Size = new System.Drawing.Size(1152, 223); this.gbData.TabIndex = 2; this.gbData.TabStop = false; this.gbData.Text = "实验数据"; // // dgvResults // this.dgvResults.AllowUserToAddRows = false; this.dgvResults.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; this.dgvResults.BackgroundColor = System.Drawing.SystemColors.Window; this.dgvResults.BorderStyle = System.Windows.Forms.BorderStyle.None; this.dgvResults.ColumnHeadersHeight = 34; this.dgvResults.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Time, this.T1, this.T2, this.V1, this.V2, this.Acc}); this.dgvResults.Dock = System.Windows.Forms.DockStyle.Fill; this.dgvResults.Location = new System.Drawing.Point(3, 29); this.dgvResults.Name = "dgvResults"; this.dgvResults.ReadOnly = true; this.dgvResults.RowHeadersVisible = false; this.dgvResults.RowHeadersWidth = 62; this.dgvResults.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.dgvResults.Size = new System.Drawing.Size(1146, 191); this.dgvResults.TabIndex = 0; // // Time // this.Time.HeaderText = "时间"; this.Time.MinimumWidth = 8; this.Time.Name = "Time"; this.Time.ReadOnly = true; // // T1 // this.T1.HeaderText = "T1(s)"; this.T1.MinimumWidth = 8; this.T1.Name = "T1"; this.T1.ReadOnly = true; // // T2 // this.T2.HeaderText = "T2(s)"; this.T2.MinimumWidth = 8; this.T2.Name = "T2"; this.T2.ReadOnly = true; // // V1 // this.V1.HeaderText = "V1(m/s)"; this.V1.MinimumWidth = 8; this.V1.Name = "V1"; this.V1.ReadOnly = true; // // V2 // this.V2.HeaderText = "V2(m/s)"; this.V2.MinimumWidth = 8; this.V2.Name = "V2"; this.V2.ReadOnly = true; // // Acc // this.Acc.HeaderText = "加速度(m/s²)"; this.Acc.MinimumWidth = 8; this.Acc.Name = "Acc"; this.Acc.ReadOnly = true; // // gbChart // this.gbChart.Controls.Add(this.chartPlot); this.gbChart.Dock = System.Windows.Forms.DockStyle.Fill; this.gbChart.Location = new System.Drawing.Point(13, 392); this.gbChart.Name = "gbChart"; this.gbChart.Size = new System.Drawing.Size(1152, 339); this.gbChart.TabIndex = 3; this.gbChart.TabStop = false; this.gbChart.Text = "加速度变化曲线"; // // chartPlot // this.chartPlot.Dock = System.Windows.Forms.DockStyle.Fill; this.chartPlot.Location = new System.Drawing.Point(3, 29); this.chartPlot.Name = "chartPlot"; this.chartPlot.Size = new System.Drawing.Size(1146, 307); this.chartPlot.TabIndex = 0; // // MainForm // this.ClientSize = new System.Drawing.Size(1178, 744); this.Controls.Add(this.mainLayout); this.Font = new System.Drawing.Font("Microsoft YaHei UI", 10F); this.Name = "MainForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "气垫导轨实验数据监控"; this.mainLayout.ResumeLayout(false); this.gbSerial.ResumeLayout(false); this.serialLayout.ResumeLayout(false); this.buttonPanel.ResumeLayout(false); this.gbData.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dgvResults)).EndInit(); this.gbChart.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.chartPlot)).EndInit(); this.ResumeLayout(false); } // 初始化图表 private void InitializePlot() { // 清除现有图表区域和系列 chartPlot.ChartAreas.Clear(); chartPlot.Series.Clear(); chartPlot.Titles.Clear(); // 创建图表区域 ChartArea chartArea = new ChartArea("MainArea"); chartArea.AxisX.Title = "实验序号"; chartArea.AxisY.Title = "加速度 (m/s²)"; chartArea.AxisX.MajorGrid.LineColor = Color.LightGray; chartArea.AxisY.MajorGrid.LineColor = Color.LightGray; chartArea.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dot; chartPlot.ChartAreas.Add(chartArea); // 创建数据系列 Series series = new Series("加速度"); series.ChartType = SeriesChartType.Line; series.Color = Color.Blue; series.BorderWidth = 2; series.MarkerStyle = MarkerStyle.Circle; series.MarkerSize = 8; series.MarkerColor = Color.DarkBlue; chartPlot.Series.Add(series); // 添加标题 Title title = new Title("加速度变化曲线", Docking.Top, new Font("Microsoft YaHei UI", 12, FontStyle.Bold), Color.Black); chartPlot.Titles.Add(title); // 初始化空数据点 series.Points.AddXY(0, 0); // 设置初始轴范围 chartArea.AxisX.Minimum = 0; chartArea.AxisX.Maximum = 10; chartArea.AxisY.Minimum = 0; chartArea.AxisY.Maximum = 10; } // 加载可用串口 private void LoadAvailablePorts() { cmbPorts.Items.Clear(); string[] ports = SerialPort.GetPortNames(); cmbPorts.Items.AddRange(ports); if (ports.Length > 0) cmbPorts.SelectedIndex = 0; // 设置波特率选项 cmbBaudRate.Items.AddRange(new object[] { 9600, 19200, 38400, 57600, 115200 }); cmbBaudRate.SelectedIndex = 4; // 默认115200 } // 连接/断开串口 private void btnConnect_Click(object sender, EventArgs e) { if (serialPort == null || !serialPort.IsOpen) { try { if (cmbPorts.SelectedItem == null) { MessageBox.Show("请选择有效的串口", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } serialPort = new SerialPort(cmbPorts.Text, (int)cmbBaudRate.SelectedItem); serialPort.DataReceived += SerialPort_DataReceived; serialPort.Open(); btnConnect.Text = "断开连接"; btnConnect.BackColor = Color.LightCoral; // 重置实验开始时间 experimentStartTime = DateTime.Now; } catch (Exception ex) { MessageBox.Show($"串口连接错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { try { serialPort.Close(); serialPort.Dispose(); serialPort = null; btnConnect.Text = "连接串口"; btnConnect.BackColor = Color.LightGreen; } catch (Exception ex) { MessageBox.Show($"断开连接错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } // 串口数据接收处理 private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { string data = serialPort.ReadLine(); this.Invoke(new Action(() => ProcessData(data))); } catch (Exception ex) { this.Invoke(new Action(() => MessageBox.Show($"数据接收错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error))); } } // 数据处理和显示 private void ProcessData(string rawData) { /* 数据格式示例: * "T1:0.025,T2:0.023,V1:0.4,V2:0.44,A:0.2" */ try { string[] pairs = rawData.Split(','); double t1 = 0, t2 = 0, v1 = 0, v2 = 0, acc = 0; foreach (string pair in pairs) { string[] keyValue = pair.Split(':'); if (keyValue.Length != 2) continue; switch (keyValue[0].Trim()) { case "T1": t1 = ParseDouble(keyValue[1]); break; case "T2": t2 = ParseDouble(keyValue[1]); break; case "V1": v1 = ParseDouble(keyValue[1]); break; case "V2": v2 = ParseDouble(keyValue[1]); break; case "A": acc = ParseDouble(keyValue[1]); break; } } // 计算相对时间(从实验开始) TimeSpan elapsed = DateTime.Now - experimentStartTime; string timeString = $"{elapsed.Minutes:00}:{elapsed.Seconds:00}.{elapsed.Milliseconds:000}"; // 添加新行到DataGridView int rowIndex = dgvResults.Rows.Add(); dgvResults.Rows[rowIndex].Cells["Time"].Value = timeString; dgvResults.Rows[rowIndex].Cells["T1"].Value = t1.ToString("0.000"); dgvResults.Rows[rowIndex].Cells["T2"].Value = t2.ToString("0.000"); dgvResults.Rows[rowIndex].Cells["V1"].Value = v1.ToString("0.000"); dgvResults.Rows[rowIndex].Cells["V2"].Value = v2.ToString("0.000"); dgvResults.Rows[rowIndex].Cells["Acc"].Value = acc.ToString("0.000"); // 设置加速度单元格颜色 if (acc > 0) dgvResults.Rows[rowIndex].Cells["Acc"].Style.BackColor = Color.FromArgb(230, 255, 230); else if (acc < 0) dgvResults.Rows[rowIndex].Cells["Acc"].Style.BackColor = Color.FromArgb(255, 230, 230); // 自动滚动到最后一行 if (dgvResults.Rows.Count > 0) dgvResults.FirstDisplayedScrollingRowIndex = dgvResults.Rows.Count - 1; // 更新图表 UpdatePlot(acc); } catch (Exception ex) { MessageBox.Show($"数据处理错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // 解析双精度值,使用不变文化 private double ParseDouble(string value) { return double.Parse(value.Trim(), CultureInfo.InvariantCulture); } // 更新图表数据 private void UpdatePlot(double acc) { // 添加新数据点 accelerationData.Add(acc); dataPointCount++; // 限制显示点数 if (accelerationData.Count > 100) accelerationData.RemoveAt(0); // 清除原有数据 chartPlot.Series["加速度"].Points.Clear(); // 添加新数据点 for (int i = 0; i < accelerationData.Count; i++) { chartPlot.Series["加速度"].Points.AddXY(i + 1, accelerationData[i]); } // 更新标题 chartPlot.Titles[0].Text = $"加速度变化曲线 (共 {dataPointCount} 个数据点)"; // 自动调整Y轴范围 if (accelerationData.Count > 0) { double min = accelerationData[0]; double max = accelerationData[0]; foreach (double val in accelerationData) { if (val < min) min = val; if (val > max) max = val; } // 添加边距 double range = max - min; if (range < 0.1) range = 0.1; // 避免过小范围 min = min - range * 0.1; max = max + range * 0.1; // 更新Y轴范围 chartPlot.ChartAreas["MainArea"].AxisY.Minimum = min; chartPlot.ChartAreas["MainArea"].AxisY.Maximum = max; } // 更新X轴范围 chartPlot.ChartAreas["MainArea"].AxisX.Minimum = 1; chartPlot.ChartAreas["MainArea"].AxisX.Maximum = accelerationData.Count > 0 ? accelerationData.Count + 1 : 10; // 刷新图表 chartPlot.Invalidate(); } // 导出数据到Excel(使用EPPlus库) private void btnExport_Click(object sender, EventArgs e) { if (dgvResults.Rows.Count <= 1) // 检查是否有数据 { MessageBox.Show("没有数据可导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.Filter = "Excel 文件|*.xlsx"; sfd.Title = "保存实验数据"; sfd.FileName = $"气垫导轨实验_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx"; if (sfd.ShowDialog() == DialogResult.OK) { try { ExcelExporter.ExportToExcel(dgvResults, accelerationData, sfd.FileName); MessageBox.Show("数据导出成功! Excel文件已生成。", "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information); // 尝试打开导出的Excel文件 try { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo { FileName = sfd.FileName, UseShellExecute = true }); } catch { /* 忽略打开错误 */ } } catch (Exception ex) { MessageBox.Show($"导出失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } // 计算平均加速度 private double CalculateAverageAcceleration() { if (accelerationData.Count == 0) return 0; double sum = 0; foreach (double acc in accelerationData) sum += acc; return sum / accelerationData.Count; } // 计算最大加速度 private double CalculateMaxAcceleration() { if (accelerationData.Count == 0) return 0; double max = accelerationData[0]; foreach (double acc in accelerationData) if (Math.Abs(acc) > Math.Abs(max)) max = acc; return max; } // 清除数据 private void btnClear_Click(object sender, EventArgs e) { if (MessageBox.Show("确定要清除所有数据吗? 此操作不可恢复。", "确认清除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { dgvResults.Rows.Clear(); accelerationData.Clear(); dataPointCount = 0; // 重置实验开始时间 experimentStartTime = DateTime.Now; // 重置图表 InitializePlot(); } } } // Excel导出助手类 public static class ExcelExporter { public static void ExportToExcel(DataGridView dgv, List<double> accelerationData, string filePath) { // 创建Excel包 using (ExcelPackage excelPackage = new ExcelPackage()) { // 1. 创建实验信息表 CreateInfoSheet(excelPackage, dgv, accelerationData); // 2. 创建数据工作表 CreateDataSheet(excelPackage, dgv); // 3. 创建图表工作表(如果有数据) if (accelerationData.Count > 0) { CreateChartSheet(excelPackage, accelerationData); } // 4. 保存文件 FileInfo excelFile = new FileInfo(filePath); excelPackage.SaveAs(excelFile); } } private static void CreateInfoSheet(ExcelPackage excelPackage, DataGridView dgv, List<double> accelerationData) { ExcelWorksheet infoSheet = excelPackage.Workbook.Worksheets.Add("实验信息"); // 标题行 infoSheet.Cells[1, 1].Value = "气垫导轨实验报告"; infoSheet.Cells[1, 1, 1, 6].Merge = true; infoSheet.Cells[1, 1].Style.Font.Bold = true; infoSheet.Cells[1, 1].Style.Font.Size = 16; infoSheet.Cells[1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // 实验信息 infoSheet.Cells[3, 1].Value = "实验时间"; infoSheet.Cells[3, 2].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); infoSheet.Cells[4, 1].Value = "数据点数"; infoSheet.Cells[4, 2].Value = dgv.Rows.Count - 1; infoSheet.Cells[5, 1].Value = "平均加速度"; double avgAcc = CalculateAverageAcceleration(accelerationData); infoSheet.Cells[5, 2].Value = avgAcc.ToString("0.000") + " m/s²"; infoSheet.Cells[6, 1].Value = "最大加速度"; double maxAcc = CalculateMaxAcceleration(accelerationData); infoSheet.Cells[6, 2].Value = maxAcc.ToString("0.000") + " m/s²"; // 设置列宽 infoSheet.Column(1).Width = 15; infoSheet.Column(2).Width = 25; } private static void CreateDataSheet(ExcelPackage excelPackage, DataGridView dgv) { ExcelWorksheet dataSheet = excelPackage.Workbook.Worksheets.Add("实验数据"); // 添加标题行 for (int i = 0; i < dgv.Columns.Count; i++) { dataSheet.Cells[1, i + 1].Value = dgv.Columns[i].HeaderText; dataSheet.Cells[1, i + 1].Style.Fill.PatternType = ExcelFillStyle.Solid; dataSheet.Cells[1, i + 1].Style.Fill.BackgroundColor.SetColor(Color.LightBlue); dataSheet.Cells[1, i + 1].Style.Font.Bold = true; dataSheet.Cells[1, i + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; } // 添加数据行 for (int row = 0; row < dgv.Rows.Count - 1; row++) { for (int col = 0; col < dgv.Columns.Count; col++) { var cellValue = dgv.Rows[row].Cells[col].Value; ExcelRange cell = dataSheet.Cells[row + 2, col + 1]; if (col == 0) // 时间列 { cell.Value = cellValue?.ToString(); } else if (double.TryParse(cellValue?.ToString(), out double numValue)) { cell.Value = numValue; cell.Style.Numberformat.Format = "0.000"; // 加速度列特殊样式 if (col == 5) { cell.Style.Font.Color.SetColor( numValue > 0 ? Color.DarkGreen : Color.DarkRed); } } else { cell.Value = cellValue?.ToString(); } } } // 自动调整列宽 dataSheet.Cells.AutoFitColumns(); // 添加表格边框 using (ExcelRange range = dataSheet.Cells[1, 1, dgv.Rows.Count, dgv.Columns.Count]) { range.Style.Border.Top.Style = ExcelBorderStyle.Thin; range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin; range.Style.Border.Left.Style = ExcelBorderStyle.Thin; range.Style.Border.Right.Style = ExcelBorderStyle.Thin; } } private static void CreateChartSheet(ExcelPackage excelPackage, List<double> accelerationData) { ExcelWorksheet chartSheet = excelPackage.Workbook.Worksheets.Add("加速度曲线"); // 添加标题行 chartSheet.Cells[1, 1].Value = "实验序号"; chartSheet.Cells[1, 2].Value = "加速度 (m/s²)"; // 添加数据 for (int i = 0; i < accelerationData.Count; i++) { chartSheet.Cells[i + 2, 1].Value = i + 1; chartSheet.Cells[i + 2, 2].Value = accelerationData[i]; } // 创建折线图 ExcelLineChart chart = chartSheet.Drawings.AddChart("加速度曲线", eChartType.Line) as ExcelLineChart; chart.Title.Text = "加速度变化曲线"; chart.SetPosition(1, 0, 3, 0); chart.SetSize(800, 400); // 添加数据系列 var series = chart.Series.Add( chartSheet.Cells[2, 2, accelerationData.Count + 1, 2], chartSheet.Cells[2, 1, accelerationData.Count + 1, 1]); series.Header = "加速度"; // 设置图表样式 chart.YAxis.Title.Text = "加速度 (m/s²)"; chart.XAxis.Title.Text = "实验序号"; // 添加趋势线 var trendline = series.TrendLines.Add(eTrendLine.Linear); trendline.DisplayEquation = true; trendline.DisplayRSquaredValue = false; // 设置图表区域样式 chart.Legend.Position = eLegendPosition.Bottom; chart.Legend.Font.Size = 10; chart.Title.Font.Size = 14; chart.Title.Font.Bold = true; } private static double CalculateAverageAcceleration(List<double> accelerationData) { if (accelerationData.Count == 0) return 0; double sum = 0; foreach (double acc in accelerationData) sum += acc; return sum / accelerationData.Count; } private static double CalculateMaxAcceleration(List<double> accelerationData) { if (accelerationData.Count == 0) return 0; double max = accelerationData[0]; foreach (double acc in accelerationData) if (Math.Abs(acc) > Math.Abs(max)) max = acc; return max; } } // 程序入口类 static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { // 设置EPPlus许可证(非商业用途) ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } } 请你在这个代码的基础上修改我上个一发出的错误
11-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值