excel import data

This commit is contained in:
JiaHao Liu 2021-09-12 10:18:12 +08:00
parent f1b355566e
commit ee46ed9c94
6 changed files with 303 additions and 82 deletions

View File

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Text;
// ref https://www.c-sharpcorner.com/article/read-excel-file-in-c-sharp-winform/
//using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Data;
using Microsoft.Office.Interop;
namespace solarApp.Service
{
public class excelHelper
{
void readExcel(string fname, ref System.Data.DataTable dt) {
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fname);
Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
DataRow mydr;
// dt.Column = colCount;
//dataGridView1.ColumnCount = colCount;
//dataGridView1.RowCount = rowCount;
for (int i = 1; i <= rowCount; i++)
{
mydr = dt.NewRow();
for (int j = 1; j <= colCount; j++)
{
//write the value to the Grid
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
{
//dataGridView1.Rows[i - 1].Cells[j - 1].Value = xlRange.Cells[i, j].Value2.ToString();
}
// Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");
//add useful things here!
}
}
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
//rule of thumb for releasing com objects:
// never use two dots, all COM objects must be referenced and released individually
// ex: [somthing].[something].[something] is bad
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
//public void ReadSample()
//{
// Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
// if (excelApp != null)
// {
// Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(@"C:\test.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkbook.Sheets[1];
// Excel.Range excelRange = excelWorksheet.UsedRange;
// int rowCount = excelRange.Rows.Count;
// int colCount = excelRange.Columns.Count;
// for (int i = 1; i <= rowCount; i++)
// {
// for (int j = 1; j <= colCount; j++)
// {
// Excel.Range range = (excelWorksheet.Cells[i, 1] as Excel.Range);
// string cellValue = range.Value.ToString();
// //do anything
// }
// }
// excelWorkbook.Close();
// excelApp.Quit();
// }
//}
}
}

View File

@ -374,5 +374,70 @@ namespace solarApp.Service
return result; return result;
} }
bool insert_meter() {
bool result = false;
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
try
{
//先判斷是否存在 meter
string ss = $@"SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = '" + _siteDB + "' AND table_name = 's" + _siteID01 + "' ";
List<string> ds_meter = conn.Query<string>(ss, new { siteID = _siteID01 }).AsList<string>();
if (ds_meter.Count == 0) return false;
//每小時 table exs02202000101_meter
ss = @$"delete from solar_master.meter_history_hour where PowerStationId = " + _powerStationID + @" and left(`TIMESTAMP`, 10) = @date1;
INSERT INTO solar_master.meter_history_hour(PowerStationId, `TIMESTAMP`, METERID, V_AB, V_BC, V_CA, I_A, I_B, I_C, P, F, INPUT_KWH, OUTPUT_KWH)
SELECT " + _powerStationID + @" as PowerStationId, m1.`timestamp`, m1.METERID, m1.V_AB, m1.V_BC, m1.V_CA, m1.I_A, m1.I_B, m1.I_C, m1.P, m1.F, m2.INPUT_KWH, m2.OUTPUT_KWH
FROM ( --
SELECT FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') AS `timestamp`, m.METERID, AVG(m.V_AB) AS V_AB,
AVG(m.V_BC) AS V_BC, AVG(m.V_CA) AS V_CA, AVG(m.I_A) AS I_A, AVG(m.I_B) AS I_B, AVG(m.I_C) AS I_C, AVG(m.P) AS P, AVG(m.F) AS F
FROM " + _siteDB + ".s" + _siteID01 + @"_meter m
WHERE FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') = @date1
GROUP BY FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H'), m.METERID
) m1
LEFT JOIN ( -- INPUT_KWH, OUTPUT_KWH
SELECT FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') AS `timestamp`, m.METERID, m.INPUT_KWH AS INPUT_KWH, m.OUTPUT_KWH AS OUTPUT_KWH
FROM " + _siteDB + ".s" + _siteID01 + @" m
WHERE LEFT(FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H'), 10) = @date1
AND RIGHT(FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H'), 2) = '55' ) m2
ON m1.timestamp = m2.timestamp AND m1.METERID = m2.METERID;";
conn.Execute(ss, new { date1 = _date1 });
//每天
ss = @$" delete from solar_master.meter_history_day where PowerStationId = " + _powerStationID + @" and left(`TIMESTAMP`, 10) = @date1;
INSERT INTO solar_master.meter_history_day(PowerStationId, `TIMESTAMP`, METERID, V_AB, V_BC, V_CA, I_A, I_B, I_C, P, F, INPUT_KWH, OUTPUT_KWH)
SELECT m.PowerStationId, DATE_FORMAT(m.TIMESTAMP, '%Y-%m-%d') AS TIMESTAMP, m.METERID, AVG(m.V_AB) AS V_AB,
AVG(m.V_BC) AS V_BC, AVG(m.V_CA) AS V_CA, AVG(m.I_A) AS I_A, AVG(m.I_B) AS I_B, AVG(m.I_C) AS I_C,
AVG(m.P) AS P, AVG(m.F) AS F, AVG(m.INPUT_KWH) AS INPUT_KWH, AVG(m.OUTPUT_KWH) AS OUTPUT_KWH
FROM meter_history_hour m
WHERE DATE_FORMAT(m.TIMESTAMP, '%Y-%m-%d') = @date1 AND m.PowerStationId = @PowerStationId
GROUP BY DATE_FORMAT(m.TIMESTAMP, '%Y-%m-%d'), m.METERID;";
conn.Execute(ss, new { powerStationID = _powerStationID, date1 = _date1 });
//每月
ss = @$" delete from solar_master.meter_history_month where PowerStationId = " + _powerStationID + @" and left(`TIMESTAMP`, 10) = @date1;
INSERT INTO solar_master.meter_history_month(PowerStationId, `TIMESTAMP`, METERID, V_AB, V_BC, V_CA, I_A, I_B, I_C, P, F, INPUT_KWH, OUTPUT_KWH)
SELECT m.PowerStationId, DATE_FORMAT(m.TIMESTAMP, '%Y-%m') AS TIMESTAMP, m.METERID, AVG(m.V_AB) AS V_AB, AVG(m.V_BC) AS V_BC, AVG(m.V_CA) AS V_CA,
AVG(m.I_A) AS I_A, AVG(m.I_B) AS I_B, AVG(m.I_C) AS I_C, AVG(m.P) AS P,
AVG(m.F) AS F, AVG(m.INPUT_KWH) AS INPUT_KWH, AVG(m.OUTPUT_KWH) AS OUTPUT_KWH
FROM meter_history_day m
WHERE DATE_FORMAT(m.TIMESTAMP, '%Y-%m') = '2021-07'
AND m.PowerStationId = 1
GROUP BY DATE_FORMAT(m.TIMESTAMP, '%Y-%m'), m.METERID;";
conn.Execute(ss, new { powerStationID = _powerStationID, date1 = _date1 });
result = true;
}
catch (Exception ex)
{
throw ex;
}
conn.Close();
}
return result;
}
} }
} }

View File

@ -32,12 +32,14 @@ namespace solarApp
this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage(); this.tabPage1 = new System.Windows.Forms.TabPage();
this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.lbmsg = new System.Windows.Forms.Label();
this.dtSelect2 = new System.Windows.Forms.DateTimePicker();
this.bt_archive = new System.Windows.Forms.Button(); this.bt_archive = new System.Windows.Forms.Button();
this.bt_clear_sensor = new System.Windows.Forms.Button(); this.bt_clear_sensor = new System.Windows.Forms.Button();
this.bt_clear_station = new System.Windows.Forms.Button(); this.bt_clear_station = new System.Windows.Forms.Button();
this.bt_clear_inv = new System.Windows.Forms.Button(); this.bt_clear_inv = new System.Windows.Forms.Button();
this.fp_site = new System.Windows.Forms.FlowLayoutPanel(); this.fp_site = new System.Windows.Forms.FlowLayoutPanel();
this.dtSelect = new System.Windows.Forms.DateTimePicker(); this.dtSelect1 = new System.Windows.Forms.DateTimePicker();
this.bt_site = new System.Windows.Forms.Button(); this.bt_site = new System.Windows.Forms.Button();
this.bt_Inv = new System.Windows.Forms.Button(); this.bt_Inv = new System.Windows.Forms.Button();
this.bt_Sensor = new System.Windows.Forms.Button(); this.bt_Sensor = new System.Windows.Forms.Button();
@ -90,12 +92,14 @@ namespace solarApp
// splitContainer1.Panel1 // splitContainer1.Panel1
// //
this.splitContainer1.Panel1.BackColor = System.Drawing.SystemColors.ActiveCaption; this.splitContainer1.Panel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.splitContainer1.Panel1.Controls.Add(this.lbmsg);
this.splitContainer1.Panel1.Controls.Add(this.dtSelect2);
this.splitContainer1.Panel1.Controls.Add(this.bt_archive); this.splitContainer1.Panel1.Controls.Add(this.bt_archive);
this.splitContainer1.Panel1.Controls.Add(this.bt_clear_sensor); this.splitContainer1.Panel1.Controls.Add(this.bt_clear_sensor);
this.splitContainer1.Panel1.Controls.Add(this.bt_clear_station); this.splitContainer1.Panel1.Controls.Add(this.bt_clear_station);
this.splitContainer1.Panel1.Controls.Add(this.bt_clear_inv); this.splitContainer1.Panel1.Controls.Add(this.bt_clear_inv);
this.splitContainer1.Panel1.Controls.Add(this.fp_site); this.splitContainer1.Panel1.Controls.Add(this.fp_site);
this.splitContainer1.Panel1.Controls.Add(this.dtSelect); this.splitContainer1.Panel1.Controls.Add(this.dtSelect1);
this.splitContainer1.Panel1.Controls.Add(this.bt_site); this.splitContainer1.Panel1.Controls.Add(this.bt_site);
this.splitContainer1.Panel1.Controls.Add(this.bt_Inv); this.splitContainer1.Panel1.Controls.Add(this.bt_Inv);
this.splitContainer1.Panel1.Controls.Add(this.bt_Sensor); this.splitContainer1.Panel1.Controls.Add(this.bt_Sensor);
@ -109,12 +113,28 @@ namespace solarApp
this.splitContainer1.SplitterWidth = 10; this.splitContainer1.SplitterWidth = 10;
this.splitContainer1.TabIndex = 0; this.splitContainer1.TabIndex = 0;
// //
// lbmsg
//
this.lbmsg.AutoSize = true;
this.lbmsg.Location = new System.Drawing.Point(15, 591);
this.lbmsg.Name = "lbmsg";
this.lbmsg.Size = new System.Drawing.Size(20, 19);
this.lbmsg.TabIndex = 10;
this.lbmsg.Text = "~";
//
// dtSelect2
//
this.dtSelect2.Location = new System.Drawing.Point(15, 616);
this.dtSelect2.Name = "dtSelect2";
this.dtSelect2.Size = new System.Drawing.Size(145, 27);
this.dtSelect2.TabIndex = 9;
//
// bt_archive // bt_archive
// //
this.bt_archive.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_archive.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_archive.Location = new System.Drawing.Point(15, 796); this.bt_archive.Location = new System.Drawing.Point(9, 825);
this.bt_archive.Name = "bt_archive"; this.bt_archive.Name = "bt_archive";
this.bt_archive.Size = new System.Drawing.Size(282, 100); this.bt_archive.Size = new System.Drawing.Size(282, 71);
this.bt_archive.TabIndex = 8; this.bt_archive.TabIndex = 8;
this.bt_archive.Text = "單日歸檔"; this.bt_archive.Text = "單日歸檔";
this.bt_archive.UseVisualStyleBackColor = true; this.bt_archive.UseVisualStyleBackColor = true;
@ -124,7 +144,7 @@ namespace solarApp
// //
this.bt_clear_sensor.Enabled = false; this.bt_clear_sensor.Enabled = false;
this.bt_clear_sensor.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_clear_sensor.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_clear_sensor.Location = new System.Drawing.Point(15, 634); this.bt_clear_sensor.Location = new System.Drawing.Point(10, 663);
this.bt_clear_sensor.Name = "bt_clear_sensor"; this.bt_clear_sensor.Name = "bt_clear_sensor";
this.bt_clear_sensor.Size = new System.Drawing.Size(135, 44); this.bt_clear_sensor.Size = new System.Drawing.Size(135, 44);
this.bt_clear_sensor.TabIndex = 7; this.bt_clear_sensor.TabIndex = 7;
@ -136,7 +156,7 @@ namespace solarApp
// //
this.bt_clear_station.Enabled = false; this.bt_clear_station.Enabled = false;
this.bt_clear_station.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_clear_station.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_clear_station.Location = new System.Drawing.Point(15, 734); this.bt_clear_station.Location = new System.Drawing.Point(10, 763);
this.bt_clear_station.Name = "bt_clear_station"; this.bt_clear_station.Name = "bt_clear_station";
this.bt_clear_station.Size = new System.Drawing.Size(135, 44); this.bt_clear_station.Size = new System.Drawing.Size(135, 44);
this.bt_clear_station.TabIndex = 6; this.bt_clear_station.TabIndex = 6;
@ -148,7 +168,7 @@ namespace solarApp
// //
this.bt_clear_inv.Enabled = false; this.bt_clear_inv.Enabled = false;
this.bt_clear_inv.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_clear_inv.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_clear_inv.Location = new System.Drawing.Point(15, 684); this.bt_clear_inv.Location = new System.Drawing.Point(10, 713);
this.bt_clear_inv.Name = "bt_clear_inv"; this.bt_clear_inv.Name = "bt_clear_inv";
this.bt_clear_inv.Size = new System.Drawing.Size(135, 44); this.bt_clear_inv.Size = new System.Drawing.Size(135, 44);
this.bt_clear_inv.TabIndex = 5; this.bt_clear_inv.TabIndex = 5;
@ -161,21 +181,21 @@ namespace solarApp
this.fp_site.Dock = System.Windows.Forms.DockStyle.Top; this.fp_site.Dock = System.Windows.Forms.DockStyle.Top;
this.fp_site.Location = new System.Drawing.Point(0, 0); this.fp_site.Location = new System.Drawing.Point(0, 0);
this.fp_site.Name = "fp_site"; this.fp_site.Name = "fp_site";
this.fp_site.Size = new System.Drawing.Size(300, 583); this.fp_site.Size = new System.Drawing.Size(300, 553);
this.fp_site.TabIndex = 4; this.fp_site.TabIndex = 4;
// //
// dtSelect // dtSelect1
// //
this.dtSelect.Location = new System.Drawing.Point(15, 601); this.dtSelect1.Location = new System.Drawing.Point(15, 559);
this.dtSelect.Name = "dtSelect"; this.dtSelect1.Name = "dtSelect1";
this.dtSelect.Size = new System.Drawing.Size(204, 27); this.dtSelect1.Size = new System.Drawing.Size(145, 27);
this.dtSelect.TabIndex = 3; this.dtSelect1.TabIndex = 3;
this.dtSelect.ValueChanged += new System.EventHandler(this.dtSelect_ValueChanged); this.dtSelect1.ValueChanged += new System.EventHandler(this.dtSelect_ValueChanged);
// //
// bt_site // bt_site
// //
this.bt_site.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_site.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_site.Location = new System.Drawing.Point(164, 734); this.bt_site.Location = new System.Drawing.Point(159, 763);
this.bt_site.Name = "bt_site"; this.bt_site.Name = "bt_site";
this.bt_site.Size = new System.Drawing.Size(135, 44); this.bt_site.Size = new System.Drawing.Size(135, 44);
this.bt_site.TabIndex = 2; this.bt_site.TabIndex = 2;
@ -186,7 +206,7 @@ namespace solarApp
// bt_Inv // bt_Inv
// //
this.bt_Inv.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_Inv.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_Inv.Location = new System.Drawing.Point(162, 684); this.bt_Inv.Location = new System.Drawing.Point(157, 713);
this.bt_Inv.Name = "bt_Inv"; this.bt_Inv.Name = "bt_Inv";
this.bt_Inv.Size = new System.Drawing.Size(135, 44); this.bt_Inv.Size = new System.Drawing.Size(135, 44);
this.bt_Inv.TabIndex = 1; this.bt_Inv.TabIndex = 1;
@ -197,7 +217,7 @@ namespace solarApp
// bt_Sensor // bt_Sensor
// //
this.bt_Sensor.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_Sensor.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_Sensor.Location = new System.Drawing.Point(162, 634); this.bt_Sensor.Location = new System.Drawing.Point(157, 663);
this.bt_Sensor.Name = "bt_Sensor"; this.bt_Sensor.Name = "bt_Sensor";
this.bt_Sensor.Size = new System.Drawing.Size(135, 44); this.bt_Sensor.Size = new System.Drawing.Size(135, 44);
this.bt_Sensor.TabIndex = 0; this.bt_Sensor.TabIndex = 0;
@ -250,6 +270,7 @@ namespace solarApp
this.lbSiteDB_sensor.Size = new System.Drawing.Size(79, 24); this.lbSiteDB_sensor.Size = new System.Drawing.Size(79, 24);
this.lbSiteDB_sensor.TabIndex = 11; this.lbSiteDB_sensor.TabIndex = 11;
this.lbSiteDB_sensor.Text = "Site_DB"; this.lbSiteDB_sensor.Text = "Site_DB";
this.lbSiteDB_sensor.Click += new System.EventHandler(this.lbSiteDB_sensor_Click);
// //
// lbSiteID_sensor // lbSiteID_sensor
// //
@ -295,6 +316,7 @@ namespace solarApp
this.tabControl1.ResumeLayout(false); this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false); this.tabPage1.ResumeLayout(false);
this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false); this.splitContainer1.ResumeLayout(false);
@ -313,7 +335,7 @@ namespace solarApp
private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TabPage tabPage2; private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.DateTimePicker dtSelect; private System.Windows.Forms.DateTimePicker dtSelect1;
private System.Windows.Forms.Button bt_site; private System.Windows.Forms.Button bt_site;
private System.Windows.Forms.Button bt_Inv; private System.Windows.Forms.Button bt_Inv;
private System.Windows.Forms.Button bt_Sensor; private System.Windows.Forms.Button bt_Sensor;
@ -327,5 +349,7 @@ namespace solarApp
private System.Windows.Forms.Button bt_clear_station; private System.Windows.Forms.Button bt_clear_station;
private System.Windows.Forms.Button bt_archive; private System.Windows.Forms.Button bt_archive;
private System.Windows.Forms.Button bt_clear_sensor; private System.Windows.Forms.Button bt_clear_sensor;
private System.Windows.Forms.Label lbmsg;
private System.Windows.Forms.DateTimePicker dtSelect2;
} }
} }

View File

@ -24,7 +24,7 @@ namespace solarApp
private void bt_Sensor_Click(object sender, EventArgs e) private void bt_Sensor_Click(object sender, EventArgs e)
{ {
string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); string date1 = dtSelect1.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procSensorSvc sensorSvc = new procSensorSvc(); procSensorSvc sensorSvc = new procSensorSvc();
//sensorSvc._siteDB = lbSiteDB_sensor.Text; //sensorSvc._siteDB = lbSiteDB_sensor.Text;
@ -84,7 +84,7 @@ namespace solarApp
private void bt_Inv_Click(object sender, EventArgs e) private void bt_Inv_Click(object sender, EventArgs e)
{ {
string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); string date1 = dtSelect1.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procInvSvc invSvc = new procInvSvc(); procInvSvc invSvc = new procInvSvc();
//invSvc._siteDB = lbSiteDB_sensor.Text; //invSvc._siteDB = lbSiteDB_sensor.Text;
@ -102,14 +102,14 @@ namespace solarApp
bt_clear_inv.Enabled = true; bt_clear_inv.Enabled = true;
bt_clear_station.Enabled = true; bt_clear_station.Enabled = true;
bt_clear_sensor.Enabled = true; bt_clear_sensor.Enabled = true;
bt_clear_inv.Text = "clear " + dtSelect.Value.ToString("MM-dd"); bt_clear_inv.Text = "clear " + dtSelect1.Value.ToString("MM-dd");
bt_clear_station.Text = "clear " + dtSelect.Value.ToString("MM-dd"); bt_clear_station.Text = "clear " + dtSelect1.Value.ToString("MM-dd");
bt_clear_sensor.Text = "clear " + dtSelect.Value.ToString("MM-dd"); bt_clear_sensor.Text = "clear " + dtSelect1.Value.ToString("MM-dd");
} }
private void bt_clear_inv_Click(object sender, EventArgs e) private void bt_clear_inv_Click(object sender, EventArgs e)
{ {
string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); string date1 = dtSelect1.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procInvSvc invSvc = new procInvSvc(); procInvSvc invSvc = new procInvSvc();
invSvc._siteDB = lbSiteDB_sensor.Text; invSvc._siteDB = lbSiteDB_sensor.Text;
@ -124,7 +124,7 @@ namespace solarApp
private void bt_clear_station_Click(object sender, EventArgs e) private void bt_clear_station_Click(object sender, EventArgs e)
{ {
string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); string date1 = dtSelect1.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procStationSvc siteSvc = new procStationSvc(); procStationSvc siteSvc = new procStationSvc();
siteSvc._siteDB = lbSiteDB_sensor.Text; siteSvc._siteDB = lbSiteDB_sensor.Text;
@ -139,7 +139,7 @@ namespace solarApp
private void bt_site_Click(object sender, EventArgs e) private void bt_site_Click(object sender, EventArgs e)
{ {
string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); string date1 = dtSelect1.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procStationSvc siteSvc = new procStationSvc(); procStationSvc siteSvc = new procStationSvc();
//siteSvc._siteDB = lbSiteDB_sensor.Text; //siteSvc._siteDB = lbSiteDB_sensor.Text;
@ -161,7 +161,7 @@ namespace solarApp
private void bt_clear_sensor_Click(object sender, EventArgs e) private void bt_clear_sensor_Click(object sender, EventArgs e)
{ {
string date1 = dtSelect.Value.ToString("yyyy-MM-dd"); string date1 = dtSelect1.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1); //procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procSensorSvc sensorSvc = new procSensorSvc(); procSensorSvc sensorSvc = new procSensorSvc();
sensorSvc._siteDB = lbSiteDB_sensor.Text; sensorSvc._siteDB = lbSiteDB_sensor.Text;
@ -173,5 +173,11 @@ namespace solarApp
//sensorSvc.archiveData(); //sensorSvc.archiveData();
MessageBox.Show("OK"); MessageBox.Show("OK");
} }
private void lbSiteDB_sensor_Click(object sender, EventArgs e)
{
fmExcel fm = new fmExcel();
fm.Show();
}
} }
} }

View File

@ -102,7 +102,7 @@ namespace solarApp
// bt_inv_hour_hj // bt_inv_hour_hj
// //
this.bt_inv_hour_hj.Font = new System.Drawing.Font("Microsoft JhengHei UI", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_inv_hour_hj.Font = new System.Drawing.Font("Microsoft JhengHei UI", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_inv_hour_hj.Location = new System.Drawing.Point(5, 251); this.bt_inv_hour_hj.Location = new System.Drawing.Point(0, 186);
this.bt_inv_hour_hj.Name = "bt_inv_hour_hj"; this.bt_inv_hour_hj.Name = "bt_inv_hour_hj";
this.bt_inv_hour_hj.Size = new System.Drawing.Size(143, 44); this.bt_inv_hour_hj.Size = new System.Drawing.Size(143, 44);
this.bt_inv_hour_hj.TabIndex = 7; this.bt_inv_hour_hj.TabIndex = 7;
@ -117,14 +117,14 @@ namespace solarApp
this.bt_clear_station.Name = "bt_clear_station"; this.bt_clear_station.Name = "bt_clear_station";
this.bt_clear_station.Size = new System.Drawing.Size(135, 44); this.bt_clear_station.Size = new System.Drawing.Size(135, 44);
this.bt_clear_station.TabIndex = 6; this.bt_clear_station.TabIndex = 6;
this.bt_clear_station.Text = "clear data"; this.bt_clear_station.Text = "AUO Insert ";
this.bt_clear_station.UseVisualStyleBackColor = true; this.bt_clear_station.UseVisualStyleBackColor = true;
this.bt_clear_station.Visible = false; this.bt_clear_station.Click += new System.EventHandler(this.bt_clear_station_Click);
// //
// bt_inv_day_hj // bt_inv_day_hj
// //
this.bt_inv_day_hj.Font = new System.Drawing.Font("Microsoft JhengHei UI", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_inv_day_hj.Font = new System.Drawing.Font("Microsoft JhengHei UI", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_inv_day_hj.Location = new System.Drawing.Point(5, 335); this.bt_inv_day_hj.Location = new System.Drawing.Point(0, 270);
this.bt_inv_day_hj.Name = "bt_inv_day_hj"; this.bt_inv_day_hj.Name = "bt_inv_day_hj";
this.bt_inv_day_hj.Size = new System.Drawing.Size(143, 44); this.bt_inv_day_hj.Size = new System.Drawing.Size(143, 44);
this.bt_inv_day_hj.TabIndex = 5; this.bt_inv_day_hj.TabIndex = 5;
@ -215,7 +215,7 @@ namespace solarApp
// bt_hour_archive_hj // bt_hour_archive_hj
// //
this.bt_hour_archive_hj.Font = new System.Drawing.Font("Microsoft JhengHei UI", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_hour_archive_hj.Font = new System.Drawing.Font("Microsoft JhengHei UI", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_hour_archive_hj.Location = new System.Drawing.Point(154, 251); this.bt_hour_archive_hj.Location = new System.Drawing.Point(149, 186);
this.bt_hour_archive_hj.Name = "bt_hour_archive_hj"; this.bt_hour_archive_hj.Name = "bt_hour_archive_hj";
this.bt_hour_archive_hj.Size = new System.Drawing.Size(146, 44); this.bt_hour_archive_hj.Size = new System.Drawing.Size(146, 44);
this.bt_hour_archive_hj.TabIndex = 10; this.bt_hour_archive_hj.TabIndex = 10;
@ -226,7 +226,7 @@ namespace solarApp
// bt_day_archive_hj // bt_day_archive_hj
// //
this.bt_day_archive_hj.Font = new System.Drawing.Font("Microsoft JhengHei UI", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.bt_day_archive_hj.Font = new System.Drawing.Font("Microsoft JhengHei UI", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_day_archive_hj.Location = new System.Drawing.Point(154, 335); this.bt_day_archive_hj.Location = new System.Drawing.Point(149, 270);
this.bt_day_archive_hj.Name = "bt_day_archive_hj"; this.bt_day_archive_hj.Name = "bt_day_archive_hj";
this.bt_day_archive_hj.Size = new System.Drawing.Size(119, 44); this.bt_day_archive_hj.Size = new System.Drawing.Size(119, 44);
this.bt_day_archive_hj.TabIndex = 9; this.bt_day_archive_hj.TabIndex = 9;
@ -257,13 +257,13 @@ namespace solarApp
this.tabControl.Size = new System.Drawing.Size(1782, 853); this.tabControl.Size = new System.Drawing.Size(1782, 853);
this.tabControl.TabIndex = 1; this.tabControl.TabIndex = 1;
// //
// bt_day_achive_hj // fmExcel
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 19F); this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 19F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1782, 853); this.ClientSize = new System.Drawing.Size(1782, 853);
this.Controls.Add(this.tabControl); this.Controls.Add(this.tabControl);
this.Name = "bt_day_achive_hj"; this.Name = "fmExcel";
this.Text = "fmExcel"; this.Text = "fmExcel";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.panel1.ResumeLayout(false); this.panel1.ResumeLayout(false);

View File

@ -67,67 +67,97 @@ namespace solarApp
dataGridView1.DataSource = csvSvc.insertHour2DB(ref dt); dataGridView1.DataSource = csvSvc.insertHour2DB(ref dt);
MessageBox.Show("OK"); MessageBox.Show("OK");
} }
protected void ImportFile() protected void ImporExcel()
{ {
string fname = ""; string fname = "";
OpenFileDialog fdlg = new OpenFileDialog(); OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Excel File Dialog"; //fdlg.Title = "Excel File Dialog";
fdlg.InitialDirectory = @"c:\"; //fdlg.InitialDirectory = @"d:\temp\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"; //fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2; //fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true; //fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK) //if (fdlg.ShowDialog() == DialogResult.OK)
{ //{
fname = fdlg.FileName; // fname = fdlg.FileName;
} //}
else return; //else return;
//取得選取檔案的路徑
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); //string dir = Path.GetDirectoryName(fname);
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fname); // 取得路徑下所有檔案
Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; //string[] fileEntries = System.IO.Directory.GetFiles(dir);
Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange; string[] fileEntries = Directory.GetFiles(@"D:\temp\AUO\台中太平\台中太平\inverter", "*.*", SearchOption.AllDirectories);
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
// dt.Column = colCount; // dt.Column = colCount;
dataGridView1.ColumnCount = colCount;
dataGridView1.RowCount = rowCount; System.Data.DataTable dt = new System.Data.DataTable();
DataColumn mydc;
for (int i = 1; i <= rowCount; i++) bool isFirst = true; bool isFirstData = true;
foreach (string fileName in fileEntries)
{ {
for (int j = 1; j <= colCount; j++) Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
{ Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileName);
//write the value to the Grid Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null) Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;
//dataGridView1.ColumnCount = colCount;
//dataGridView1.RowCount = rowCount;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
for (int i = 1; i <= rowCount; i++)
{
#region dt add column
if (isFirst)
{ {
dataGridView1.Rows[i - 1].Cells[j - 1].Value = xlRange.Cells[i, j].Value2.ToString(); for (int j = 1; j <= colCount; j++)
{
int col = 0;
col = j + 1;
mydc = new DataColumn(col.ToString());
dt.Columns.Add(mydc);
isFirst = false;
}
} }
// Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t"); #endregion
//add useful things here! DataRow row = dt.NewRow();
for (int j = 0; j <= colCount-1; j++)
{
if (isFirstData) // 第一筆資料為 欄位名稱
{
Microsoft.Office.Interop.Excel.Range range = xlRange.Cells[i, j+1] as Microsoft.Office.Interop.Excel.Range;
row[j] = range.Value.ToString();
isFirstData = false;
}
else if (i == 0) continue;
else //從第二個檔案 開始都從第二個 row 開始
{
Microsoft.Office.Interop.Excel.Range range = xlRange.Cells[i, j+1] as Microsoft.Office.Interop.Excel.Range;
row[j] = range.Value.ToString();
}
}
dt.Rows.Add(row);
} }
} //release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
//cleanup //cleanup
GC.Collect(); GC.Collect();
GC.WaitForPendingFinalizers(); GC.WaitForPendingFinalizers();
MessageBox.Show(" dt.Rows.Count = " + dt.Rows.Count.ToString());
//rule of thumb for releasing com objects: //rule of thumb for releasing com objects:
// never use two dots, all COM objects must be referenced and released individually // never use two dots, all COM objects must be referenced and released individually
// ex: [somthing].[something].[something] is bad // ex: [somthing].[something].[something] is bad
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
} }
private void bt_inv_day_hj_Click(object sender, EventArgs e) private void bt_inv_day_hj_Click(object sender, EventArgs e)
@ -191,5 +221,10 @@ namespace solarApp
csvSvc.archive_data("hour"); csvSvc.archive_data("hour");
MessageBox.Show("ok"); MessageBox.Show("ok");
} }
private void bt_clear_station_Click(object sender, EventArgs e)
{
ImporExcel();
}
} }
} }