Exemplo n.º 1
0
        /// <summary>
        /// 导出一个统计表,包含自变量字段和因变量字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExportTable_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(cmbxTableNames.Text))
                {
                    MessageBox.Show("未加载任何数据表!");
                    return;
                }
                if (String.IsNullOrEmpty(cmbxXField.Text) || String.IsNullOrEmpty(cmbxYField.Text))
                {
                    MessageBox.Show("自变量和因变量不能为空");
                    return;
                }


                //根据选择的自变量因变量字段,及行范围,新生成表
                DataTable     sourceDt   = dataGridView1.DataSource as DataTable;
                List <string> fieldNames = new List <string>()
                {
                    cmbxXField.Text, cmbxYField.Text
                };
                List <string> fieldNames1 = new List <string>()
                {
                    cmbxXField.Text, cmbxZField.Text
                };
                int       startRowNum = Convert.ToInt32(txbxStartRowNum.Text) - 1; //DataTable的行号从0开始
                int       endRowNum   = Convert.ToInt32(txbxEndRowNum.Text) - 1;
                DataTable resultDt    = DataTableImp.GetTableByFieldNamesAndRowNumRange(sourceDt, fieldNames, fieldNames1, startRowNum, endRowNum);


                SaveFileDialog saveFileDlg = new System.Windows.Forms.SaveFileDialog();
                saveFileDlg.Title    = "导出统计表";
                saveFileDlg.Filter   = "Excel文件|*.xls;*.xlsx";
                saveFileDlg.FileName = cmbxValueDescribe.Text;

                string excelFile;
                if (saveFileDlg.ShowDialog() == DialogResult.OK)
                {
                    excelFile = saveFileDlg.FileName;

                    if (ExcelFileOperate.SaveDataTableToXls(resultDt, excelFile))
                    {
                        MessageBox.Show("数据导出成功!保存到" + excelFile);
                    }
                    else
                    {
                        MessageBox.Show("导出失败!");
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("统计表导出失败!");
                return;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成统计图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChart_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(cmbxTableNames.Text))
            {
                MessageBox.Show("未加载任何数据表!");
                return;
            }
            if (String.IsNullOrEmpty(cmbxXField.Text) || String.IsNullOrEmpty(cmbxYField.Text))
            {
                MessageBox.Show("自变量和因变量不能为空");
                return;
            }

            //判断因变量字段是否是值字段,或者是否能强制转换成值
            DataTable      sourceDt   = dataGridView1.DataSource as DataTable;
            FieldValueType yFieldType = DataTableImp.GetFieldType(sourceDt, cmbxYField.Text);
            FieldValueType zFieldType = DataTableImp.GetFieldType(sourceDt, cmbxZField.Text);

            if (yFieldType == FieldValueType.非值文本)
            {
                MessageBox.Show("因变量字段不可为非数值字段!");
                return;
            }
            if (zFieldType == FieldValueType.非值文本)
            {
                MessageBox.Show("因变量字段不可为非数值字段!");
                return;
            }

            //根据选择的自变量因变量字段,及行范围,新生成表
            List <string> fieldNames = new List <string>()
            {
                cmbxXField.Text, cmbxYField.Text
            };
            List <string> fieldNames1 = new List <string>()
            {
                cmbxXField.Text, cmbxZField.Text
            };
            int startRowNum = Convert.ToInt32(txbxStartRowNum.Text) - 1; //DataTable的行号从0开始
            int endRowNum   = Convert.ToInt32(txbxEndRowNum.Text) - 1;
            //先进行行列筛选
            DataTable tempDt = DataTableImp.GetTableByFieldNamesAndRowNumRange(sourceDt, fieldNames, fieldNames1, startRowNum, endRowNum);
            //再进行因变量字段值的值类型转换
            DataTable resultDt = DataTableImp.ConvertFieldToType(tempDt, cmbxYField.Text, cmbxZField.Text, yFieldType, zFieldType);
            ChartForm chartFrm = new ChartForm(resultDt, cmbxXField.Text, cmbxYField.Text, cmbxZField.Text, cmbxValueDescribe.Text, cmbxValueDescribe1.Text);

            chartFrm.ShowDialog();
        }