Exemplo n.º 1
0
        public void ExportToExcel(DateTime dt)
        {
            // 弹出对话框,并导出到Excel文件
            int            year  = dt.Year;
            int            month = dt.Month;
            string         name  = year + "年" + month + "月";
            SaveFileDialog dlg   = new SaveFileDialog();

            dlg.FileName = name + "雨量月表";
            dlg.Filter   = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                // 保存到Excel表格中
                System.Data.DataTable dataTable = new System.Data.DataTable();
                dataTable.Columns.Add("|");
                dataTable.Columns.Add(StationName);
                dataTable.Columns.Add(StationName1);

                for (int i = 1; i < 32; ++i)
                {
                    dataTable.Columns.Add(i.ToString() + "日", typeof(string));
                }

                dataTable.Columns.Add(AllRains, typeof(string));
                // 逐行读取数据
                int     iRowCount = m_dataTable.Rows.Count;
                DataRow row0      = dataTable.NewRow();

                for (int i = 0; i < iRowCount; ++i)
                {
                    // 赋值到dataTable中去
                    DataRow row = dataTable.NewRow();
                    // 多余的一列是state
                    for (int j = 0; j < m_dataTable.Columns.Count - 1; ++j)
                    {
                        row[j] = m_dataTable.Rows[i][j];
                    }
                    dataTable.Rows.Add(row);
                }
                // 显示提示框
                CMessageBox box = new CMessageBox()
                {
                    MessageInfo = "正在导出表格,请稍候"
                };
                box.ShowDialog(this);
                if (CExcelExport.ExportToExcelWrapper(dataTable, dlg.FileName, name + "雨量月表"))
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show(string.Format("导出成功,保存在文件\"{0}\"中", dlg.FileName));
                }
                else
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show("导出失败");
                }
            }//end of if dialog okay
        }
        private void EH_MI_ExportToExcel_Click(object sender, EventArgs e)
        {
            // 弹出对话框,并导出到Excel文件
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Filter = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                // 保存到Excel表格中
                DataTable dataTable = new DataTable();
                //   dataTable.Columns.Add(CS_VoltageID, typeof(Int64));
                dataTable.Columns.Add(CS_StationID);
                dataTable.Columns.Add(CS_StationName, typeof(string));
                dataTable.Columns.Add(CS_Voltage, typeof(string));
                dataTable.Columns.Add(CS_TimeCollected, typeof(DateTime));
                dataTable.Columns.Add(CS_TimeReceived, typeof(DateTime));
                dataTable.Columns.Add(CS_MsgType, typeof(string));
                dataTable.Columns.Add(CS_ChannelType, typeof(string));
                // 逐页读取数据
                for (int i = 0; i < m_iTotalPage; ++i)
                {
                    List <CEntityVoltage> tmpVoltages = m_proxyVoltage.GetPageData(i + 1);
                    foreach (CEntityVoltage voltage in tmpVoltages)
                    {
                        // 赋值到dataTable中去
                        DataRow row = dataTable.NewRow();
                        // row[CS_VoltageID] = voltage.VoltageID;
                        row[CS_StationID]     = voltage.StationID;
                        row[CS_StationName]   = CDBDataMgr.Instance.GetStationById(voltage.StationID).StationName;
                        row[CS_Voltage]       = voltage.Voltage;
                        row[CS_TimeCollected] = voltage.TimeCollect;
                        row[CS_TimeReceived]  = voltage.TimeRecieved;
                        row[CS_MsgType]       = CEnumHelper.MessageTypeToUIStr(voltage.MessageType);
                        row[CS_ChannelType]   = CEnumHelper.ChannelTypeToUIStr(voltage.ChannelType);
                        dataTable.Rows.Add(row);
                    }
                }
                // 显示提示框
                CMessageBox box = new CMessageBox()
                {
                    MessageInfo = "正在导出表格,请稍候"
                };
                box.ShowDialog(this);
                if (CExcelExport.ExportToExcelWrapper(dataTable, dlg.FileName, "电压表"))
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show(string.Format("导出成功,保存在文件\"{0}\"中", dlg.FileName));
                }
                else
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show("导出失败");
                }
            }//end of if dialog okay
        }
Exemplo n.º 3
0
        /// <summary>
        /// 导出到Excel表格中
        /// </summary>
        public void ExportToExcel(DateTime dt)
        {
            // 弹出对话框,并导出到Excel文件
            SaveFileDialog dlg  = new SaveFileDialog();
            string         name = dt.Year + "年" + dt.Month + "月" + "畅通率表";

            dlg.Filter   = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
            dlg.FileName = name;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                // 保存到Excel表格中
                System.Data.DataTable dataTable = new System.Data.DataTable();
                dataTable.Columns.Add(CS_StationID, typeof(string));
                //dataTable.Columns.Add(CS_StationName, typeof(string));
                for (int i = 1; i < 32; i++)
                {
                    dataTable.Columns.Add(i.ToString() + "日");
                }
                dataTable.Columns.Add(CS_TheoreticalCount);
                dataTable.Columns.Add(CS_ActualCount);
                dataTable.Columns.Add(CS_CommunicationRate, typeof(string));
                dataTable.Columns.Add(CS_GSM);
                dataTable.Columns.Add(CS_GPRS);
                // 逐行读取数据
                int iRowCount = m_dataTable.Rows.Count;
                for (int i = 0; i < iRowCount; ++i)
                {
                    // 赋值到dataTable中去
                    DataRow row = dataTable.NewRow();
                    // 多余的一列是state
                    for (int j = 0; j < m_dataTable.Columns.Count - 1; ++j)
                    {
                        row[j] = m_dataTable.Rows[i][j];
                    }
                    dataTable.Rows.Add(row);
                }
                // 显示提示框
                CMessageBox box = new CMessageBox()
                {
                    MessageInfo = "正在导出表格,请稍候"
                };
                box.ShowDialog(this);
                if (CExcelExport.ExportToExcelWrapper(dataTable, dlg.FileName, name))
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show(string.Format("导出成功,保存在文件\"{0}\"中", dlg.FileName));
                }
                else
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show("导出失败");
                }
            }//end of if dialog okay
        }
Exemplo n.º 4
0
        public void ExportToExcelNew(DataGridView dgv, DateTime dt, string stationName)
        {
            int            year  = dt.Year;
            int            month = dt.Month;
            string         name  = stationName + "_" + year + "年" + month + "月";
            SaveFileDialog dlg   = new SaveFileDialog();

            dlg.FileName = name + "水位表";
            dlg.Filter   = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                int rowscount = dgv.Rows.Count;
                int colscount = dgv.Columns.Count;
                //行数必须大于0
                if (rowscount <= 0)
                {
                    MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //列数必须大于0
                if (colscount <= 0)
                {
                    MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //行数不可以大于65536
                if (rowscount > 65536)
                {
                    MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                FileInfo file = new FileInfo(dlg.FileName);
                if (file.Exists)
                {
                    try
                    {
                        file.Delete();
                    }
                    catch (Exception error)
                    {
                        MessageBox.Show(error.Message, "删除失败 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                CMessageBox box = new CMessageBox();
                box.MessageInfo = "正在导出报表";
                box.ShowDialog(this);
                this.Enabled = false;
                Microsoft.Office.Interop.Excel.Application objExcel    = null;
                Microsoft.Office.Interop.Excel.Workbook    objWorkbook = null;
                Microsoft.Office.Interop.Excel.Worksheet   objsheet    = null;
                ProgressBar tempProgressBar = new ProgressBar();
                try
                {
                    //申明对象
                    objExcel    = new Microsoft.Office.Interop.Excel.Application();
                    objWorkbook = objExcel.Workbooks.Add(Missing.Value);
                    objsheet    = (Microsoft.Office.Interop.Excel.Worksheet)objWorkbook.ActiveSheet;
                    //设置EXCEL不可见
                    objExcel.Visible = false;
                    objsheet.DisplayAutomaticPageBreaks   = true;                                                         //显示分页线
                    objsheet.PageSetup.CenterFooter       = "第 &P 页,共 &N 页";
                    objsheet.PageSetup.CenterHorizontally = true;                                                         //水平居中
                    objsheet.PageSetup.PrintTitleRows     = "$1:$1";                                                      //顶端标题行
                    objsheet.PageSetup.PaperSize          = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4;         //A4纸张大小
                    objsheet.PageSetup.Orientation        = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape; //纸张方向.纵向
                    Range range2 = objsheet.Range[objsheet.Cells[1, 1], objsheet.Cells[1, 8]];
                    Range range  = objsheet.Range[objsheet.Cells[1, 9], objsheet.Cells[1, 30]];
                    range.Font.Bold        = true;
                    range.Font.ColorIndex  = 0;
                    range2.Font.Bold       = true;
                    range2.Font.ColorIndex = 0;
                    objsheet.Cells[1, 7]   = name + "水位表";
                    int displayColumnsCount = 1;
                    objsheet.Cells[2, displayColumnsCount] = "日/时";
                    displayColumnsCount++;
                    for (int i = 1; i <= 24; i++)
                    {
                        if (dgv.Columns[i].Visible == true)
                        {
                            objsheet.Cells[2, displayColumnsCount] = "     " + (i + "时").ToString().Trim();
                            displayColumnsCount++;
                        }
                    }
                    objsheet.Cells[2, displayColumnsCount] = "    平均";
                    displayColumnsCount++;
                    objsheet.Cells[2, displayColumnsCount] = "    最大";
                    displayColumnsCount++;
                    objsheet.Cells[2, displayColumnsCount] = " 最大时分";
                    displayColumnsCount++;
                    objsheet.Cells[2, displayColumnsCount] = "    最小";
                    displayColumnsCount++;
                    objsheet.Cells[2, displayColumnsCount] = " 最小时分";
                    displayColumnsCount++;
                    for (int row = 0; row <= dgv.RowCount; row++)
                    {
                        //tempProgressBar.PerformStep();
                        displayColumnsCount = 1;
                        for (int col = 0; col < colscount; col++)
                        {
                            if (dgv.Columns[col].Visible == true)
                            {
                                try
                                {
                                    objsheet.Cells[row + 3, displayColumnsCount] = dgv.Rows[row].Cells[col].Value.ToString().Trim();
                                    displayColumnsCount++;
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                    }
                    objWorkbook.SaveAs(dlg.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                                       Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,
                                       Missing.Value, Missing.Value);
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                finally
                {
                    //关闭Excel应用
                    if (objWorkbook != null)
                    {
                        objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
                    }
                    if (objExcel.Workbooks != null)
                    {
                        objExcel.Workbooks.Close();
                    }
                    if (objExcel != null)
                    {
                        objExcel.Quit();
                    }

                    objsheet    = null;
                    objWorkbook = null;
                    objExcel    = null;
                }
                this.Enabled = true;
                box.CloseDialog();
                MessageBox.Show(dlg.FileName + "导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Exemplo n.º 5
0
        public void ExportToExcelNew(DataGridView dgv, DateTime dt, string stationName)
        {
            int            year  = dt.Year;
            int            month = dt.Month;
            string         name  = stationName + "_" + year + "年";
            SaveFileDialog dlg   = new SaveFileDialog();

            dlg.FileName = name + "水位表";
            dlg.Filter   = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                int rowscount = dgv.Rows.Count;
                int colscount = dgv.Columns.Count;
                if (rowscount <= 0)
                {
                    MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (colscount <= 0)
                {
                    MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (rowscount > 65536)
                {
                    MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (colscount > 255)
                {
                    MessageBox.Show("数据记录行数太多,不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                FileInfo file = new FileInfo(dlg.FileName);
                if (file.Exists)
                {
                    try
                    {
                        file.Delete();
                    }
                    catch (Exception error)
                    {
                        MessageBox.Show(error.Message, "删除失败 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                CMessageBox box = new CMessageBox();
                box.MessageInfo = "正在导出报表";
                box.ShowDialog(this);
                this.Enabled = false;
                Microsoft.Office.Interop.Excel.Application objExcel    = null;
                Microsoft.Office.Interop.Excel.Workbook    objWorkbook = null;
                Microsoft.Office.Interop.Excel.Worksheet   objsheet    = null;
                try
                {
                    //申明对象
                    objExcel    = new Microsoft.Office.Interop.Excel.Application();
                    objWorkbook = objExcel.Workbooks.Add(Missing.Value);
                    objsheet    = (Microsoft.Office.Interop.Excel.Worksheet)objWorkbook.ActiveSheet;
                    //设置EXCEL不可见
                    objExcel.Visible               = false;
                    objsheet.PageSetup.PaperSize   = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4;         //A4纸张大小
                    objsheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape; //纸张方向.纵向
                    Range range2 = objsheet.Range[objsheet.Cells[1, 1], objsheet.Cells[1, 8]];
                    Range range  = objsheet.Range[objsheet.Cells[1, 9], objsheet.Cells[1, 26]];
                    range.Font.Bold = true;
                    // range.Font.
                    //设置字体颜色
                    range.Font.ColorIndex = 0;
                    //设置颜色背景
                    range.Interior.ColorIndex = 15;

                    range2.Font.Bold = true;
                    // range.Font.
                    //设置字体颜色
                    range2.Font.ColorIndex = 0;
                    //设置颜色背景
                    range2.Interior.ColorIndex = 15;
                    objsheet.Cells[1, 7]       = name + "水位表";
                    //  ((Microsoft.Office.Interop.Excel.Range)objsheet.Cells[1, 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;

                    //向Excel中写入表格的表头
                    int displayColumnsCount = 1;
                    objsheet.Cells[2, displayColumnsCount] = "站点/日期";
                    displayColumnsCount++;
                    for (int i = 1; i <= 31; i++)
                    {
                        if (dgv.Columns[i].Visible == true)
                        {
                            objsheet.Cells[2, displayColumnsCount] = "     " + i + "日";
                            displayColumnsCount++;
                        }
                    }

                    //objsheet.Cells[2, displayColumnsCount] = "日雨量";
                    //设置进度条
                    //tempProgressBar.Refresh();
                    //tempProgressBar.Visible   =   true;
                    //tempProgressBar.Minimum=1;
                    //tempProgressBar.Maximum=dgv.RowCount;
                    //tempProgressBar.Step=1;
                    //向Excel中逐行逐列写入表格中的数据
                    for (int row = 0; row <= dgv.RowCount; row++)
                    {
                        //tempProgressBar.PerformStep();

                        displayColumnsCount = 1;
                        for (int col = 0; col < colscount; col++)
                        {
                            if (dgv.Columns[col].Visible == true)
                            {
                                try
                                {
                                    objsheet.Cells[row + 3, displayColumnsCount] = "'" + dgv.Rows[row].Cells[col].Value.ToString().Trim();
                                    displayColumnsCount++;
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                    }
                    //隐藏进度条
                    //tempProgressBar.Visible   =   false;
                    //保存文件
                    objWorkbook.SaveAs(dlg.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                                       Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,
                                       Missing.Value, Missing.Value);
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                finally
                {
                    //关闭Excel应用
                    if (objWorkbook != null)
                    {
                        objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
                    }
                    if (objExcel.Workbooks != null)
                    {
                        objExcel.Workbooks.Close();
                    }
                    if (objExcel != null)
                    {
                        objExcel.Quit();
                    }

                    objsheet    = null;
                    objWorkbook = null;
                    objExcel    = null;
                }
                this.Enabled = true;
                box.CloseDialog();
                MessageBox.Show(dlg.FileName + "/n/n导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 private void EH_MI_ExportToExcel_Click(object sender, EventArgs e)
 {
     try
     {
         // 弹出对话框,并导出到Excel文件
         SaveFileDialog dlg = new SaveFileDialog();
         dlg.Filter = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
         if (DialogResult.OK == dlg.ShowDialog())
         {
             // 保存到Excel表格中
             DataTable dataTable = new DataTable();
             //CS_StationID,CS_StationName
             //  dataTable.Columns.Add(CS_RainID, typeof(Int64));
             dataTable.Columns.Add(CS_StationID);
             dataTable.Columns.Add(CS_StationName, typeof(string));
             dataTable.Columns.Add(CS_TotalRain, typeof(string));
             dataTable.Columns.Add(CS_DifferenceRain, typeof(string));
             dataTable.Columns.Add(CS_DayRain, typeof(string));
             // dataTable.Columns.Add(CS_PeriodRain, typeof(Decimal));
             dataTable.Columns.Add(CS_PeriodRain, typeof(string));
             dataTable.Columns.Add(CS_DataState, typeof(string));
             dataTable.Columns.Add(CS_TimeCollected, typeof(DateTime));
             dataTable.Columns.Add(CS_TimeReceived, typeof(DateTime));
             dataTable.Columns.Add(CS_MsgType, typeof(string));
             dataTable.Columns.Add(CS_ChannelType, typeof(string));
             // 逐页读取数据
             for (int i = 0; i < m_iTotalPage; ++i)
             {
                 List <CEntityRain> tmpRains = m_proxyRain.GetPageData(i + 1);
                 foreach (CEntityRain rain in tmpRains)
                 {
                     // 赋值到dataTable中去
                     DataRow row = dataTable.NewRow();
                     //  row[CS_RainID] = rain.RainID;
                     row[CS_StationID]     = rain.StationID;
                     row[CS_StationName]   = CDBDataMgr.Instance.GetStationById(rain.StationID).StationName;
                     row[CS_DayRain]       = rain.DayRain;
                     row[CS_TotalRain]     = rain.TotalRain;
                     row[CS_DayRain]       = rain.DayRain;
                     row[CS_PeriodRain]    = rain.PeriodRain;
                     row[CS_DataState]     = rain.BState == 1 ? "正常" : "不正常";
                     row[CS_TimeCollected] = rain.TimeCollect;
                     row[CS_TimeReceived]  = rain.TimeRecieved;
                     row[CS_MsgType]       = CEnumHelper.MessageTypeToUIStr(rain.MessageType);
                     row[CS_ChannelType]   = CEnumHelper.ChannelTypeToUIStr(rain.ChannelType);
                     dataTable.Rows.Add(row);
                 }
             }
             // 显示提示框
             CMessageBox box = new CMessageBox()
             {
                 MessageInfo = "正在导出表格,请稍候"
             };
             box.ShowDialog(this);
             if (CExcelExport.ExportToExcelWrapper(dataTable, dlg.FileName, "雨量表"))
             {
                 //box.Invoke((Action)delegate { box.Close(); });
                 box.CloseDialog();
                 MessageBox.Show(string.Format("导出成功,保存在文件\"{0}\"中", dlg.FileName));
             }
             else
             {
                 //box.Invoke((Action)delegate { box.Close(); });
                 box.CloseDialog();
                 MessageBox.Show("导出失败");
             }
         }//end of if dialog okay
     }
     catch (Exception ex)
     {
         MessageBox.Show("导出失败" + ex.ToString());
     }
 }
Exemplo n.º 7
0
        public void ExportToExcel(DateTime dt, string stationName)
        {
            // 弹出对话框,并导出到Excel文件
            int            year  = dt.Year;
            int            month = dt.Month;
            string         name  = stationName + "_" + year + "年" + month + "月";
            SaveFileDialog dlg   = new SaveFileDialog();

            dlg.FileName = name + "雨量表";
            dlg.Filter   = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
            PrintDialog plg = new PrintDialog();

            PrinterSettings pss = new System.Drawing.Printing.PrinterSettings();

            pss.DefaultPageSettings.Landscape = true;
            plg.PrinterSettings = pss;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                // 保存到Excel表格中
                System.Data.DataTable dataTable = new System.Data.DataTable();
                dataTable.Columns.Add("站点");
                for (int i = 9; i < 25; ++i)
                {
                    dataTable.Columns.Add(i.ToString(), typeof(string));
                }
                for (int i = 1; i < 9; ++i)
                {
                    dataTable.Columns.Add(i.ToString(), typeof(string));
                }


                dataTable.Columns.Add(aver, typeof(string));
                // 逐行读取数据
                int     iRowCount = m_dataTable.Rows.Count;
                DataRow row0      = dataTable.NewRow();

                for (int i = 0; i < iRowCount; ++i)
                {
                    // 赋值到dataTable中去
                    DataRow row = dataTable.NewRow();
                    // 多余的一列是state
                    for (int j = 0; j < m_dataTable.Columns.Count - 1; ++j)
                    {
                        row[j] = m_dataTable.Rows[i][j];
                    }
                    dataTable.Rows.Add(row);
                }
                // 显示提示框
                CMessageBox box = new CMessageBox()
                {
                    MessageInfo = "正在导出表格,请稍候"
                };
                box.ShowDialog(this);
                if (CExcelExport.ExportToExcelWrapper(dataTable, dlg.FileName, name + "降水量月表"))
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show(string.Format("导出成功,保存在文件\"{0}\"中", dlg.FileName));
                }
                else
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show("导出失败");
                }
            }//end of if dialog okay
        }
        public override bool DoSave()
        {
            if (this.IsCurrentCellInEditMode)
            {
                //MessageBox.Show("请完成当前的编辑");
                this.EndEdit();
                //return false;
            }
            if (!AssertSaveData())
            {
                // 有非法数据
                return(false);
            }
            GenaratePortXMLConfig();
            GetUpdatedData();
            GetDeletedData();
            if ((m_listAddedPort.Count > 0 || m_listUpdatedPorts.Count > 0 || m_listDeletedPorts.Count > 0) && IsDataChanged())
            {
                // 应该作为一个事物一起处理
                //return base.DoSave();
                bool result = true;
                // 先删除,然后添加
                if (m_listDeletedPorts.Count > 0)
                {
                    // 删除
                    result = result && m_porxy.DeleteRange(m_listDeletedPorts);
                }
                if (m_listAddedPort.Count > 0)
                {
                    // 增加
                    result = result && m_porxy.AddRange(m_listAddedPort);
                }
                if (m_listUpdatedPorts.Count > 0)
                {
                    // 更新
                    result = result && m_porxy.UpdateRange(m_listUpdatedPorts);
                }

                result = result && XmlDocManager.Instance.ResetComOrPortConfig(m_listPortConfig, true);
                if (result)
                {
                    CMessageBox box = new CMessageBox();
                    box.MessageInfo = "正在更新串口";
                    //box.StartPosition = new Point(0, 0);
                    box.StartPosition = FormStartPosition.Manual;


                    int xWidth = SystemInformation.PrimaryMonitorSize.Width;   //获取显示器屏幕宽度

                    int yHeight = SystemInformation.PrimaryMonitorSize.Height; //高度
                    box.Top  = (int)(yHeight * 0.3);
                    box.Left = (int)(xWidth * 0.4);
                    //     box.Location = new Point(SystemInformation.WorkingArea.Width - this.Width, SystemInformation.WorkingArea.Height - this.Height);
                    box.ShowDialog(this);
                    // 通知其他界面更新消息
                    CDBDataMgr.Instance.UpdateAllSerialPort();
                    box.CloseDialog();
                    this.Hide();
                    MessageBox.Show("保存成功");
                    this.Show();
                }
                else
                {
                    this.Hide();
                    //MessageBox.Show("保存失败, 串口配置或者数据库连接错误");
                    MessageBox.Show("保存失败, 连接通信设备失败");
                    this.Show();
                }
                // 重新加载
                ClearAllState();
                m_listPortConfig = XmlDocManager.Instance.GetComOrPortConfig();
                LoadData();
                UpdateDataToUI();
            }
            else
            {
                this.Revert();
                MessageBox.Show("没有任何修改,无需保存");
            }

            return(true);
        }
        //11.12
        private void EH_MI_ExportToExcel_Click(object sender, EventArgs e)
        {
            // 弹出对话框,并导出到Excel文件
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Filter = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                // 保存到Excel表格中
                DataTable dataTable = new DataTable();
                //   dataTable.Columns.Add(CS_VoltageID, typeof(Int64));
                dataTable.Columns.Add(CS_StationID);
                dataTable.Columns.Add(CS_StationName, typeof(string));
                dataTable.Columns.Add(CS_TimeCollected, typeof(DateTime));
                dataTable.Columns.Add(CS_Voltage, typeof(string));
                dataTable.Columns.Add(CS_V10, typeof(string));
                dataTable.Columns.Add(CS_M10, typeof(string));
                dataTable.Columns.Add(CS_V20, typeof(string));
                dataTable.Columns.Add(CS_M20, typeof(string));
                dataTable.Columns.Add(CS_V30, typeof(string));
                dataTable.Columns.Add(CS_M30, typeof(string));
                dataTable.Columns.Add(CS_V40, typeof(string));
                dataTable.Columns.Add(CS_M40, typeof(string));
                dataTable.Columns.Add(CS_V60, typeof(string));
                dataTable.Columns.Add(CS_M60, typeof(string));
                dataTable.Columns.Add(CS_ReciveTime, typeof(DateTime));
                dataTable.Columns.Add(CS_MsgType, typeof(string));
                dataTable.Columns.Add(CS_ChannelType, typeof(string));
                // 逐页读取数据
                for (int i = 0; i < m_iTotalPage; ++i)
                {
                    List <CEntitySoilData> tmpListData = m_proxySoilData.QueryByStationAndTime(m_strStaionId, m_dateTimeStart, m_dateTimeEnd);
                    foreach (CEntitySoilData soilData in tmpListData)
                    {
                        // 赋值到dataTable中去
                        DataRow row = dataTable.NewRow();
                        // row[CS_VoltageID] = voltage.VoltageID;
                        row[CS_StationID]     = soilData.StationID;
                        row[CS_StationName]   = CDBSoilDataMgr.Instance.GetSoilStationInfoByStationId(soilData.StationID).StationName;
                        row[CS_TimeCollected] = soilData.DataTime;
                        row[CS_Voltage]       = soilData.DVoltage;
                        row[CS_V10]           = soilData.Voltage10;
                        row[CS_M10]           = soilData.Voltage10;
                        row[CS_V20]           = soilData.Voltage20;
                        row[CS_M20]           = soilData.Voltage20;
                        row[CS_V40]           = soilData.Voltage40;
                        row[CS_M40]           = soilData.Voltage40;
                        row[CS_V60]           = soilData.Voltage60;
                        row[CS_M60]           = soilData.Voltage60;
                        row[CS_ReciveTime]    = soilData.reciveTime;
                        row[CS_MsgType]       = CEnumHelper.MessageTypeToUIStr(soilData.MessageType);
                        row[CS_ChannelType]   = CEnumHelper.ChannelTypeToUIStr(soilData.ChannelType);
                        dataTable.Rows.Add(row);
                    }
                }
                // 显示提示框
                CMessageBox box = new CMessageBox()
                {
                    MessageInfo = "正在导出表格,请稍候"
                };
                box.ShowDialog(this);
                if (CExcelExport.ExportToExcelWrapper(dataTable, dlg.FileName, "电压表"))
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show(string.Format("导出成功,保存在文件\"{0}\"中", dlg.FileName));
                }
                else
                {
                    //box.Invoke((Action)delegate { box.Close(); });
                    box.CloseDialog();
                    MessageBox.Show("导出失败");
                }
            }//end of if dialog okay
        }