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 }
/// <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 }
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()); } }
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 }
//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 }