This commit is contained in:
Kai 2021-10-07 09:51:54 +08:00
commit 67feca8f0c
4 changed files with 802 additions and 40 deletions

View File

@ -6,11 +6,15 @@ using System.Text;
using System.Runtime.InteropServices;
using System.Data;
using Microsoft.Office.Interop;
using MySql.Data.MySqlClient;
using System.Configuration;
using Dapper;
namespace solarApp.Service
{
public class excelHelper
{
string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
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);
@ -60,6 +64,179 @@ namespace solarApp.Service
Marshal.ReleaseComObject(xlApp);
}
public bool insertDay2DB(DataTable dt, string filename, string inverterID)
{
bool result = false;
StringBuilder ss = new StringBuilder();
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
int i = 0;
foreach (DataRow row in dt.Rows)
{
//ss.Append(@"INSERT INTO solar_import.auo_taiping(`No.`, `雲端收到時間`, `資料取得時間`, `交流電壓`, `交流電流`, `交流功率`, `溫度`, `累積發電量`, `頻率`, `狀態`, saveDate, filename, inverterID)
ss.Append(@"INSERT INTO solar_import.auo_aimai(`No.`, `雲端收到時間`, `資料取得時間`, `交流電壓`, `交流電流`, `交流功率`, `溫度`, `累積發電量`, `頻率`, `狀態`, saveDate, filename, inverterID)
values( '"
+ row.Field<string>("1").ToString() + "' ,'"
+ row.Field<string>("2").ToString() + "' ,'"
+ row.Field<string>("3").ToString() + "' ,'"
+ row.Field<string>("4").ToString() + "' ,'"
+ row.Field<string>("5").ToString() + "' ,'"
+ row.Field<string>("6").ToString() + "' ,'"
+ row.Field<string>("7").ToString() + "' ,'"
+ row.Field<string>("8").ToString() + "' ,'"
+ row.Field<string>("9").ToString() + "' ,'"
+ row.Field<string>("10").ToString() + "', now(), '"+ filename + "', '" + inverterID + "');");
if (i % 10 == 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
i++;
}
if (ss.Length > 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
conn.Clone();
}
return result;
}
public bool insertFile2DB(string[] filename, string station_name)
{
bool result = false;
StringBuilder ss = new StringBuilder();
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
int i = 0;
foreach (var fullfileName in filename)
{
string[] ff = fullfileName.Split("\\");
string fname = string.Empty;
fname += ff[ff.Length - 1]; //只抓最後的檔名
string inverterID = string.Empty;
string[] str = fname.Split("_");
inverterID = str[str.Length - 3] + "_" + str[str.Length - 2];
str = inverterID.Split(" ");
inverterID = str[str.Length -1];
fname = fullfileName.Replace("\\", "/");
//if (fullfileName == "D:AUO台中太平inverter110") {
// str[0] = "123";
//}
ss.Append(@"insert into `solar_import`.`auo_file_list`( `station_name`, `filename`, inverterid, `inser_date`)
values( '"
+ station_name + "' ,'"
+ fname + "' ,'"
+ inverterID + "' , now() );");
if (i % 10 == 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
i++;
}
if (ss.Length > 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
conn.Clone();
}
return result;
}
public bool insertDay2DB_enlux_gong34(DataTable dt, string filename)
{
bool result = false;
StringBuilder ss = new StringBuilder();
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
int i = 0;
foreach (DataRow row in dt.Rows)
{
//ss.Append(@"INSERT INTO solar_import.auo_taiping(`No.`, `雲端收到時間`, `資料取得時間`, `交流電壓`, `交流電流`, `交流功率`, `溫度`, `累積發電量`, `頻率`, `狀態`, saveDate, filename, inverterID)
ss.Append(@"INSERT INTO solar_import.enlux_gong34(UploadTime, S1_AC, S1_DC, S2_AC, S2_DC, S3_AC, S3_DC, S4_AC, S4_DC, sunshine, temperature, saveDate, filename)
values( '"
+ row.Field<string>("1").ToString() + "' ,'"
+ row.Field<string>("2").ToString() + "' ,'"
+ row.Field<string>("3").ToString() + "' ,'"
+ row.Field<string>("4").ToString() + "' ,'"
+ row.Field<string>("5").ToString() + "' ,'"
+ row.Field<string>("6").ToString() + "' ,'"
+ row.Field<string>("7").ToString() + "' ,'"
+ row.Field<string>("8").ToString() + "' ,'"
+ row.Field<string>("9").ToString() + "' ,'"
+ row.Field<string>("10").ToString() + "' ,'"
+ row.Field<string>("11").ToString() + "' , now(), '" + filename + "');");
if (i % 10 == 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
i++;
}
if (ss.Length > 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
conn.Clone();
}
return result;
}
public bool insertFile2DB__enlux_gong34(string[] filename, string station_name)
{
bool result = false;
StringBuilder ss = new StringBuilder();
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
int i = 0;
foreach (var fullfileName in filename)
{
string[] ff = fullfileName.Split("\\");
string fname = string.Empty;
fname += ff[ff.Length - 1]; //只抓最後的檔名
//string inverterID = string.Empty;
//string[] str = fname.Split("_");
//inverterID = str[str.Length - 3] + "_" + str[str.Length - 2];
//str = inverterID.Split(" ");
//inverterID = str[str.Length - 1];
fname = fullfileName.Replace("\\", "/");
//if (fullfileName == "D:AUO台中太平inverter110") {
// str[0] = "123";
//}
ss.Append(@"insert into `solar_import`.`enlux_file_list`( `station_name`, `filename`, `inser_date`)
values( '"
+ station_name + "' ,'"
+ fname + "' , now() );");
if (i % 10 == 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
i++;
}
if (ss.Length > 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
conn.Clone();
}
return result;
}
//public void ReadSample()
//{
// Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

View File

@ -248,6 +248,100 @@ namespace solarApp.Service
}
public void taoYuan_createColumnHour(ref DataTable mycsvdt, string filepath)
{
string strpath = filepath; //csv檔案的路徑
try
{
int intColCount = 0;
bool blnFlag = true;
DataColumn mydc;
string strline;
string[] aryline;
StreamReader mysr = new StreamReader(strpath, System.Text.Encoding.Default);
int rowIndex = 0;
while ((strline = mysr.ReadLine()) != null)
{
rowIndex += 1;
if (rowIndex < 4) continue;
aryline = strline.Split(new char[] { ',' });
//給datatable加上列名
if (blnFlag)
{
blnFlag = false;
intColCount = aryline.Length;
int col = 0;
for (int i = 0; i < aryline.Length; i++)
{
col = i + 1;
mydc = new DataColumn(col.ToString());
mycsvdt.Columns.Add(mydc);
}
}
break;
}
}
catch (Exception e)
{
//throw (Stack.GetErrorStack(strpath + "讀取CSV檔案中的資料出錯." + e.Message, "OpenCSVFile("));
//return false;
}
}
/// <summary>
/// 太陽能光電 桃園全虹
/// </summary>
/// <param name="mycsvdt"></param>
/// <param name="filepath"></param>
/// <param name="InvID"></param>
/// <param name="intColCount"></param>
/// <param name="readTitle"></param>
/// <returns></returns>
public bool taoYuan_readCsvFile(ref DataTable mycsvdt, string filepath, int intColCount, bool readTitle)
{
string strpath = filepath; //csv檔案的路徑
try
{
DataColumn mydc;
DataRow mydr;
string strline;
string[] aryline;
StreamReader mysr = new StreamReader(strpath, System.Text.Encoding.Default);
int rowIndex = 0;
while ((strline = mysr.ReadLine()) != null)
{
rowIndex += 1;
if (readTitle && rowIndex == 1) {
readTitle = false;
continue;
}
aryline = strline.Split(new char[] { ',' });
//填充資料並加入到datatable中
mydr = mycsvdt.NewRow();
for (int i = 0; i < intColCount - 1; i++)
{
mydr[i] = aryline[i];
}
mycsvdt.Rows.Add(mydr);
}
}
catch (Exception ex)
{
//throw (Stack.GetErrorStack(strpath + "讀取CSV檔案中的資料出錯." + e.Message, " readCsvFile"));
throw ex;
return false;
}
return true;
}
public bool clear_inv(string timeType)
{
bool result = false;
@ -266,6 +360,98 @@ namespace solarApp.Service
return false;
}
public void taoYuan_insertHour2DB(ref DataTable dt, string dest_table)
{
List<src_inv_count> ds;
StringBuilder ss = new StringBuilder();
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
ss.Clear();
int i = 0;
foreach (DataRow row in dt.Rows) //sun_taoyuan
{
ss.Append(@"insert into solar_import."+ dest_table + @"(c1 , c2 , c3 , c4 , c5 , c6 , c7 , c8 , c9 , c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32, c33, c34, c35, c36, c37, c38, c39, c40, c41, c42, c43, c44, c45, c46, c47, c48, c49, c50, c51, c52, c53, c54, c55, c56, c57, c58)
values( '" + row.Field<string>("1").ToString() + "', " +
"'" + row.Field<string>("2").ToString() + "' , " +
"'" + row.Field<string>("3").ToString() + "' , " +
"'" + row.Field<string>("4").ToString() + "' , " +
"'" + row.Field<string>("5").ToString() + "' , " +
"'" + row.Field<string>("6").ToString() + "' , " +
"'" + row.Field<string>("7").ToString() + "' , " +
"'" + row.Field<string>("8").ToString() + "' , " +
"'" + row.Field<string>("9").ToString() + "' , " +
"'" + row.Field<string>("10").ToString() + "' ," +
"'" + row.Field<string>("11").ToString() + "' ," +
"'" + row.Field<string>("12").ToString() + "' ," +
"'" + row.Field<string>("13").ToString() + "' ," +
"'" + row.Field<string>("14").ToString() + "' ," +
"'" + row.Field<string>("15").ToString() + "' ," +
"'" + row.Field<string>("16").ToString() + "' ," +
"'" + row.Field<string>("17").ToString() + "' ," +
"'" + row.Field<string>("18").ToString() + "' ," +
"'" + row.Field<string>("19").ToString() + "' ," +
"'" + row.Field<string>("20").ToString() + "' ," +
"'" + row.Field<string>("21").ToString() + "' ," +
"'" + row.Field<string>("22").ToString() + "' ," +
"'" + row.Field<string>("23").ToString() + "' ," +
"'" + row.Field<string>("24").ToString() + "' ," +
"'" + row.Field<string>("25").ToString() + "' ," +
"'" + row.Field<string>("26").ToString() + "' ," +
"'" + row.Field<string>("27").ToString() + "' ," +
"'" + row.Field<string>("28").ToString() + "' ," +
"'" + row.Field<string>("29").ToString() + "' ," +
"'" + row.Field<string>("30").ToString() + "' ," +
"'" + row.Field<string>("31").ToString() + "' ," +
"'" + row.Field<string>("32").ToString() + "' ," +
"'" + row.Field<string>("33").ToString() + "' ," +
"'" + row.Field<string>("34").ToString() + "' ," +
"'" + row.Field<string>("35").ToString() + "' ," +
"'" + row.Field<string>("36").ToString() + "' ," +
"'" + row.Field<string>("37").ToString() + "' ," +
"'" + row.Field<string>("38").ToString() + "' ," +
"'" + row.Field<string>("39").ToString() + "' ," +
"'" + row.Field<string>("40").ToString() + "' ," +
"'" + row.Field<string>("41").ToString() + "' ," +
"'" + row.Field<string>("42").ToString() + "' ," +
"'" + row.Field<string>("43").ToString() + "' ," +
"'" + row.Field<string>("44").ToString() + "' ," +
"'" + row.Field<string>("45").ToString() + "' ," +
"'" + row.Field<string>("46").ToString() + "' ," +
"'" + row.Field<string>("47").ToString() + "' ," +
"'" + row.Field<string>("48").ToString() + "' ," +
"'" + row.Field<string>("49").ToString() + "' ," +
"'" + row.Field<string>("50").ToString() + "' ," +
"'" + row.Field<string>("51").ToString() + "' ," +
"'" + row.Field<string>("52").ToString() + "' ," +
"'" + row.Field<string>("53").ToString() + "' ," +
"'" + row.Field<string>("54").ToString() + "' ," +
"'" + row.Field<string>("55").ToString() + "' ," +
"'" + row.Field<string>("56").ToString() + "' ," +
"'" + row.Field<string>("57").ToString() + "' ," +
"'" + row.Field<string>("58").ToString() + "' );");
if (i % 100 == 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
i++;
}
if (ss.Length > 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
//ss.Clear();
//ss.Append(@"select inv, left(c1, 10) date, count(*) ct from src_inv where c1 <> '欄位名稱' group by inv, left(c1, 10)");
//ds = conn.Query<src_inv_count>(ss.ToString()).AsList<src_inv_count>();
conn.Clone();
}
//return ds;
}
public List<src_inv_count> insertHour2DB(ref DataTable dt) {
List<src_inv_count> ds;
StringBuilder ss = new StringBuilder();

View File

@ -34,7 +34,7 @@ namespace solarApp
this.lbSiteID_sensor = new System.Windows.Forms.Label();
this.bt_archive = new System.Windows.Forms.Button();
this.bt_inv_hour_hj = new System.Windows.Forms.Button();
this.bt_clear_station = new System.Windows.Forms.Button();
this.bt_read_taiping = new System.Windows.Forms.Button();
this.bt_inv_day_hj = new System.Windows.Forms.Button();
this.fp_site = new System.Windows.Forms.FlowLayoutPanel();
this.btVerifyData = new System.Windows.Forms.Button();
@ -42,10 +42,15 @@ namespace solarApp
this.panel1 = new System.Windows.Forms.Panel();
this.lbSiteName_sensor = new System.Windows.Forms.Label();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.bt_sun_taoyuan = new System.Windows.Forms.Button();
this.bt_gong34 = new System.Windows.Forms.Button();
this.bt_AUO_aimai = new System.Windows.Forms.Button();
this.bt_hour_archive_hj = new System.Windows.Forms.Button();
this.bt_day_archive_hj = new System.Windows.Forms.Button();
this.rt1 = new System.Windows.Forms.RichTextBox();
this.tb1 = new System.Windows.Forms.TabPage();
this.tabControl = new System.Windows.Forms.TabControl();
this.bt_sun_yadong = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
@ -91,13 +96,14 @@ namespace solarApp
// 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.Location = new System.Drawing.Point(5, 528);
this.bt_archive.Location = new System.Drawing.Point(3, 640);
this.bt_archive.Name = "bt_archive";
this.bt_archive.Size = new System.Drawing.Size(282, 100);
this.bt_archive.TabIndex = 8;
this.bt_archive.Text = "單日歸檔";
this.bt_archive.UseVisualStyleBackColor = true;
this.bt_archive.Visible = false;
this.bt_archive.Click += new System.EventHandler(this.bt_archive_Click);
//
// bt_inv_hour_hj
//
@ -110,16 +116,16 @@ namespace solarApp
this.bt_inv_hour_hj.UseVisualStyleBackColor = true;
this.bt_inv_hour_hj.Click += new System.EventHandler(this.bt_inv_hour_hj_Click);
//
// bt_clear_station
// bt_read_taiping
//
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(3, 418);
this.bt_clear_station.Name = "bt_clear_station";
this.bt_clear_station.Size = new System.Drawing.Size(135, 44);
this.bt_clear_station.TabIndex = 6;
this.bt_clear_station.Text = "AUO Insert ";
this.bt_clear_station.UseVisualStyleBackColor = true;
this.bt_clear_station.Click += new System.EventHandler(this.bt_clear_station_Click);
this.bt_read_taiping.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_read_taiping.Location = new System.Drawing.Point(0, 337);
this.bt_read_taiping.Name = "bt_read_taiping";
this.bt_read_taiping.Size = new System.Drawing.Size(192, 47);
this.bt_read_taiping.TabIndex = 6;
this.bt_read_taiping.Text = "AUO 太平 Read";
this.bt_read_taiping.UseVisualStyleBackColor = true;
this.bt_read_taiping.Click += new System.EventHandler(this.bt_clear_station_Click);
//
// bt_inv_day_hj
//
@ -154,12 +160,12 @@ namespace solarApp
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Left;
this.dataGridView1.Location = new System.Drawing.Point(0, 125);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 51;
this.dataGridView1.RowTemplate.Height = 29;
this.dataGridView1.Size = new System.Drawing.Size(1458, 687);
this.dataGridView1.Size = new System.Drawing.Size(629, 687);
this.dataGridView1.TabIndex = 1;
//
// panel1
@ -195,16 +201,21 @@ namespace solarApp
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.BackColor = System.Drawing.Color.PaleGoldenrod;
this.splitContainer1.Panel1.Controls.Add(this.bt_sun_yadong);
this.splitContainer1.Panel1.Controls.Add(this.bt_sun_taoyuan);
this.splitContainer1.Panel1.Controls.Add(this.bt_gong34);
this.splitContainer1.Panel1.Controls.Add(this.bt_AUO_aimai);
this.splitContainer1.Panel1.Controls.Add(this.bt_hour_archive_hj);
this.splitContainer1.Panel1.Controls.Add(this.bt_day_archive_hj);
this.splitContainer1.Panel1.Controls.Add(this.bt_archive);
this.splitContainer1.Panel1.Controls.Add(this.bt_inv_hour_hj);
this.splitContainer1.Panel1.Controls.Add(this.bt_clear_station);
this.splitContainer1.Panel1.Controls.Add(this.bt_read_taiping);
this.splitContainer1.Panel1.Controls.Add(this.bt_inv_day_hj);
this.splitContainer1.Panel1.Controls.Add(this.fp_site);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.rt1);
this.splitContainer1.Panel2.Controls.Add(this.dataGridView1);
this.splitContainer1.Panel2.Controls.Add(this.panel1);
this.splitContainer1.Size = new System.Drawing.Size(1768, 812);
@ -212,6 +223,39 @@ namespace solarApp
this.splitContainer1.SplitterWidth = 10;
this.splitContainer1.TabIndex = 0;
//
// bt_sun_taoyuan
//
this.bt_sun_taoyuan.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_sun_taoyuan.Location = new System.Drawing.Point(3, 523);
this.bt_sun_taoyuan.Name = "bt_sun_taoyuan";
this.bt_sun_taoyuan.Size = new System.Drawing.Size(213, 47);
this.bt_sun_taoyuan.TabIndex = 13;
this.bt_sun_taoyuan.Text = "太陽能光電-桃園全虹";
this.bt_sun_taoyuan.UseVisualStyleBackColor = true;
this.bt_sun_taoyuan.Click += new System.EventHandler(this.bt_sun_taoyuan_Click);
//
// bt_gong34
//
this.bt_gong34.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_gong34.Location = new System.Drawing.Point(0, 443);
this.bt_gong34.Name = "bt_gong34";
this.bt_gong34.Size = new System.Drawing.Size(189, 47);
this.bt_gong34.TabIndex = 12;
this.bt_gong34.Text = "Envision 工34";
this.bt_gong34.UseVisualStyleBackColor = true;
this.bt_gong34.Click += new System.EventHandler(this.bt_gong34_Click);
//
// bt_AUO_aimai
//
this.bt_AUO_aimai.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_AUO_aimai.Location = new System.Drawing.Point(0, 390);
this.bt_AUO_aimai.Name = "bt_AUO_aimai";
this.bt_AUO_aimai.Size = new System.Drawing.Size(189, 47);
this.bt_AUO_aimai.TabIndex = 11;
this.bt_AUO_aimai.Text = "AUO 愛買 read";
this.bt_AUO_aimai.UseVisualStyleBackColor = true;
this.bt_AUO_aimai.Click += new System.EventHandler(this.bt_AUO_Insert_Click);
//
// 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);
@ -234,6 +278,17 @@ namespace solarApp
this.bt_day_archive_hj.UseVisualStyleBackColor = true;
this.bt_day_archive_hj.Click += new System.EventHandler(this.bt_day_archive_hj_Click);
//
// rt1
//
this.rt1.BackColor = System.Drawing.SystemColors.MenuText;
this.rt1.Dock = System.Windows.Forms.DockStyle.Fill;
this.rt1.ForeColor = System.Drawing.SystemColors.Window;
this.rt1.Location = new System.Drawing.Point(629, 125);
this.rt1.Name = "rt1";
this.rt1.Size = new System.Drawing.Size(829, 687);
this.rt1.TabIndex = 2;
this.rt1.Text = "";
//
// tb1
//
this.tb1.Controls.Add(this.splitContainer1);
@ -257,6 +312,17 @@ namespace solarApp
this.tabControl.Size = new System.Drawing.Size(1782, 853);
this.tabControl.TabIndex = 1;
//
// bt_sun_yadong
//
this.bt_sun_yadong.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_sun_yadong.Location = new System.Drawing.Point(5, 576);
this.bt_sun_yadong.Name = "bt_sun_yadong";
this.bt_sun_yadong.Size = new System.Drawing.Size(213, 47);
this.bt_sun_yadong.TabIndex = 14;
this.bt_sun_yadong.Text = "太陽能光電-亞東觀音";
this.bt_sun_yadong.UseVisualStyleBackColor = true;
this.bt_sun_yadong.Click += new System.EventHandler(this.bt_sun_yadong_Click);
//
// fmExcel
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 19F);
@ -285,7 +351,7 @@ namespace solarApp
private System.Windows.Forms.Label lbSiteID_sensor;
private System.Windows.Forms.Button bt_archive;
private System.Windows.Forms.Button bt_inv_hour_hj;
private System.Windows.Forms.Button bt_clear_station;
private System.Windows.Forms.Button bt_read_taiping;
private System.Windows.Forms.Button bt_clear_inv;
private System.Windows.Forms.FlowLayoutPanel fp_site;
private System.Windows.Forms.Button btVerifyData;
@ -296,8 +362,12 @@ namespace solarApp
private System.Windows.Forms.TabPage tb1;
private System.Windows.Forms.TabControl tabControl;
private System.Windows.Forms.Button bt_inv_day_hj;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button bt_AUO_aimai;
private System.Windows.Forms.Button bt_hour_archive_hj;
private System.Windows.Forms.Button bt_day_archive_hj;
private System.Windows.Forms.RichTextBox rt1;
private System.Windows.Forms.Button bt_gong34;
private System.Windows.Forms.Button bt_sun_taoyuan;
private System.Windows.Forms.Button bt_sun_yadong;
}
}

View File

@ -67,40 +67,62 @@ namespace solarApp
dataGridView1.DataSource = csvSvc.insertHour2DB(ref dt);
MessageBox.Show("OK");
}
protected void ImporExcel()
protected void ImporExcel(string site_name)
{
string fname = "";
OpenFileDialog fdlg = new OpenFileDialog();
//OpenFileDialog fdlg = new OpenFileDialog();
//fdlg.Title = "Excel File Dialog";
//fdlg.InitialDirectory = @"d:\temp\";
//fdlg.InitialDirectory = @"d:\";
//fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
//fdlg.FilterIndex = 2;
//fdlg.RestoreDirectory = true;
//if (fdlg.ShowDialog() == DialogResult.OK)
//{
// fname = fdlg.FileName;
//}
//else return;
string[] fileEntries = new string[0];
Array.Clear(fileEntries, 0, fileEntries.Length);
//取得選取檔案的路徑
//string dir = Path.GetDirectoryName(fname);
string dir;//= Path.GetDirectoryName(fname);
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
//dir = Path.GetDirectoryName(fname);
//fileEntries = Directory.GetFiles(fbd.SelectedPath);
fileEntries = Directory.GetFiles(fbd.SelectedPath, "*.*", SearchOption.AllDirectories);
MessageBox.Show("Files found: " + fileEntries.Length.ToString(), "Message");
}
}
// 取得路徑下所有檔案
//string[] fileEntries = System.IO.Directory.GetFiles(dir);
string[] fileEntries = Directory.GetFiles(@"D:\temp\AUO\台中太平\台中太平\inverter", "*.*", SearchOption.AllDirectories);
//string[] fileEntries = System.IO.Directory.GetFiles(dir); //D:\temp\AUO\台中太平\台中太平\inverter
//string[] fileEntries = Directory.GetFiles(@"D:\AUO\台中太平\inverter", "*.*", SearchOption.AllDirectories);
// dt.Column = colCount;
solarApp.Service.excelHelper xlsSvc = new Service.excelHelper();
System.Data.DataTable dt = new System.Data.DataTable();
DataColumn mydc;
bool isFirst = true; bool isFirstData = true;
int x = 0;
foreach (string fileName in fileEntries)
#region save file list
xlsSvc.insertFile2DB(fileEntries, site_name);
#endregion
foreach (string fullfileName in fileEntries)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileName);
if (fullfileName.Contains("~$")) { rt1.AppendText("\n -- contain ~$ in filename: " + fullfileName.ToString()); continue; }
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fullfileName);
Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;
//dataGridView1.ColumnCount = colCount;
//dataGridView1.RowCount = rowCount;
string[] ff = fullfileName.Split("\\");
string filename = string.Empty;
filename += ff[ff.Length-1]; //只抓最後的檔名
rt1.AppendText(filename.ToString() + " ");
rt1.SelectionStart = rt1.Text.Length;
rt1.ScrollToCaret();
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
for (int i = 1; i <= rowCount; i++)
@ -111,7 +133,7 @@ namespace solarApp
for (int j = 1; j <= colCount; j++)
{
int col = 0;
col = j + 1;
col = j;
mydc = new DataColumn(col.ToString());
dt.Columns.Add(mydc);
isFirst = false;
@ -119,6 +141,11 @@ namespace solarApp
}
#endregion
DataRow row = dt.NewRow();
//只需要 10個欄位的版本 ---------------------------------
if (colCount != 10) {
rt1.AppendText("\n -- unable read file: " + fullfileName.ToString());
break;
}
for (int j = 0; j <= colCount-1; j++)
{
@ -126,16 +153,33 @@ namespace solarApp
{
Microsoft.Office.Interop.Excel.Range range = xlRange.Cells[i, j+1] as Microsoft.Office.Interop.Excel.Range;
row[j] = range.Value.ToString();
isFirstData = false;
if (j == 9) isFirstData = false;
}
else if (i == 0) continue;
// else if (i == 1) continue;// 第二個檔案之後的 firstRow 不需要, start from 1
else //從第二個檔案 開始都從第二個 row 開始
{
Microsoft.Office.Interop.Excel.Range range = xlRange.Cells[i, j+1] as Microsoft.Office.Interop.Excel.Range;
if (string.IsNullOrEmpty(range.Value) || string.IsNullOrWhiteSpace(range.Value))
MessageBox.Show("got it!");
//string ss = range.Value.ToString();
//ss = ss.Replace("[", "").Replace("]", "");
row[j] = range.Value.ToString();
}
if (j == colCount)
{
if (string.IsNullOrEmpty(row[j].ToString()) || string.IsNullOrWhiteSpace(row[j].ToString()))
MessageBox.Show("empty!");
}
}
if (string.IsNullOrEmpty(row[5].ToString()) || string.IsNullOrWhiteSpace(row[5].ToString()))
{
MessageBox.Show("empty!");
}
else
{
if (i == 1) continue;// 第二個檔案之後的 firstRow 不需要, start from 1
dt.Rows.Add(row);
}
dt.Rows.Add(row);
}
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
@ -148,13 +192,25 @@ namespace solarApp
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
//if (x ==3) break;
string[] str = filename.Split("_");
string inverterID = str[str.Length - 3] + "_" + str[str.Length - 2];
str = inverterID.Split(" ");
inverterID = str[str.Length - 1];
xlsSvc.insertDay2DB(dt, filename, inverterID);
dt.Rows.Clear();
x++;
}
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
dataGridView1.DataSource = dt;
//MessageBox.Show(" dt.Rows.Count = " + dt.Rows.Count.ToString());
MessageBox.Show(" dt.Rows.Count = " + dt.Rows.Count.ToString());
MessageBox.Show(" ok ");
//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
@ -224,7 +280,280 @@ namespace solarApp
private void bt_clear_station_Click(object sender, EventArgs e)
{
ImporExcel();
ImporExcel("auo_taiping");
}
private void bt_archive_Click(object sender, EventArgs e)
{
}
private void bt_AUO_Insert_Click(object sender, EventArgs e)
{
ImporExcel("auo_aimai");
}
/// <summary>
/// 台中工34
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bt_gong34_Click(object sender, EventArgs e)
{
string fname = "";
//OpenFileDialog fdlg = new OpenFileDialog();
//fdlg.Title = "Excel File Dialog";
//fdlg.InitialDirectory = @"d:\";
//fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
//fdlg.FilterIndex = 2;
//fdlg.RestoreDirectory = true;
string[] fileEntries = new string[0];
Array.Clear(fileEntries, 0, fileEntries.Length);
//取得選取檔案的路徑
string dir;//= Path.GetDirectoryName(fname);
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
//dir = Path.GetDirectoryName(fname);
//fileEntries = Directory.GetFiles(fbd.SelectedPath);
fileEntries = Directory.GetFiles(fbd.SelectedPath, "*.*", SearchOption.AllDirectories);
MessageBox.Show("Files found: " + fileEntries.Length.ToString(), "Message");
}
}
// 取得路徑下所有檔案
//string[] fileEntries = System.IO.Directory.GetFiles(dir); //D:\temp\AUO\台中太平\台中太平\inverter
//string[] fileEntries = Directory.GetFiles(@"D:\AUO\台中太平\inverter", "*.*", SearchOption.AllDirectories);
// dt.Column = colCount;
solarApp.Service.excelHelper xlsSvc = new Service.excelHelper();
System.Data.DataTable dt = new System.Data.DataTable();
DataColumn mydc;
bool isFirst = true; bool isFirstData = true;
int x = 0;
#region save file list
xlsSvc.insertFile2DB__enlux_gong34(fileEntries, "gong34");
#endregion
foreach (string fullfileName in fileEntries)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (fullfileName.Contains("~$")) { rt1.AppendText("\n -- contain ~$ in filename: " + fullfileName.ToString()); continue; }
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fullfileName);
//Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
//xl.Worksheet ws = (xl.Worksheet)wb.Sheets[1];
//foreach (xl.Worksheet ws1 in wb.Sheets)
//{
// MessageBox.Show(ws1.Name);
// // or whatever you want to do with the worksheet
//}
string[] ff = fullfileName.Split("\\");
string filename = string.Empty;
filename += ff[ff.Length - 1]; //只抓最後的檔名
rt1.AppendText(filename.ToString() + " ");
rt1.SelectionStart = rt1.Text.Length;
rt1.ScrollToCaret();
foreach (Microsoft.Office.Interop.Excel._Worksheet xlWorksheet in xlWorkbook.Sheets)
{
// MessageBox.Show(xlWorksheet.Name);
Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;
//dataGridView1.ColumnCount = colCount;
//dataGridView1.RowCount = rowCount;
rt1.AppendText(" - " + xlWorksheet.Name);
rt1.SelectionStart = rt1.Text.Length;
rt1.ScrollToCaret();
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
for (int i = 1; i <= rowCount; i++)
{
#region dt add column
if (isFirst)
{
for (int j = 1; j <= colCount; j++)
{
int col = 0;
col = j;
mydc = new DataColumn(col.ToString());
dt.Columns.Add(mydc);
isFirst = false;
}
}
#endregion
DataRow row = dt.NewRow();
//只需要 11個欄位的版本 ---------------------------------
if (colCount != 11)
{
rt1.AppendText("\n -- unable read file: " + fullfileName.ToString());
break;
}
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();
if (j == 11) isFirstData = false;
}
// else if (i == 1) continue;// 第二個檔案之後的 firstRow 不需要, start from 1
else //從第二個檔案 開始都從第二個 row 開始
{
Microsoft.Office.Interop.Excel.Range range = xlRange.Cells[i, j + 1] as Microsoft.Office.Interop.Excel.Range;
if (string.IsNullOrEmpty(range.Value) || string.IsNullOrWhiteSpace(range.Value))
MessageBox.Show("got it!");
//string ss = range.Value.ToString();
//ss = ss.Replace("[", "").Replace("]", "");
row[j] = range.Value.ToString();
}
if (j == colCount)
{
if (string.IsNullOrEmpty(row[j].ToString()) || string.IsNullOrWhiteSpace(row[j].ToString()))
MessageBox.Show("empty!");
}
}
if (string.IsNullOrEmpty(row[5].ToString()) || string.IsNullOrWhiteSpace(row[5].ToString()))
{
MessageBox.Show("empty!");
}
else
{
if (i == 1) continue;// 第二個檔案之後的 firstRow 不需要, start from 1
dt.Rows.Add(row);
}
}
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
xlsSvc.insertDay2DB_enlux_gong34(dt, filename);
dt.Rows.Clear();
} //foreach excel.sheet
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
x++;
}// foreach files
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
dataGridView1.DataSource = dt;
//MessageBox.Show(" dt.Rows.Count = " + dt.Rows.Count.ToString());
MessageBox.Show(" ok ");
//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
}
private void bt_sun_taoyuan_Click(object sender, EventArgs e)
{
string fname = "";
string[] fileEntries = new string[0];
Array.Clear(fileEntries, 0, fileEntries.Length);
//取得選取檔案的路徑
string dir;//= Path.GetDirectoryName(fname);
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
//dir = Path.GetDirectoryName(fname);
//fileEntries = Directory.GetFiles(fbd.SelectedPath);
fileEntries = Directory.GetFiles(fbd.SelectedPath, "*.*", SearchOption.AllDirectories);
MessageBox.Show("Files found: " + fileEntries.Length.ToString(), "Message");
}
}
Service.operateCSV csvSvc = new Service.operateCSV(); // readCsvTxt
System.Data.DataTable dt = new System.Data.DataTable();
bool isFirst = true;
foreach (string fileName in fileEntries)
{
#region filename InvID
string fName = Path.GetFileName(fileName);
rt1.AppendText(fName + " ");
rt1.SelectionStart = rt1.Text.Length;
rt1.ScrollToCaret();
#endregion
if (isFirst)
{
//csvSvc.clear_inv("hour");
csvSvc.taoYuan_createColumnHour(ref dt, fileName);
csvSvc.taoYuan_readCsvFile(ref dt, fileName, dt.Columns.Count, isFirst);
isFirst = false;
}
else
csvSvc.taoYuan_readCsvFile(ref dt, fileName, dt.Columns.Count, isFirst);
}
MessageBox.Show(" 共 " + dt.Rows.Count.ToString());
//System.Data.DataTable dt = solarApp.Service.csvHelper.OpenCSV(fname);
csvSvc.taoYuan_insertHour2DB(ref dt, "sun_taoyuan");
MessageBox.Show("OK");
}
private void bt_sun_yadong_Click(object sender, EventArgs e)
{
string fname = "";
string[] fileEntries = new string[0];
Array.Clear(fileEntries, 0, fileEntries.Length);
//取得選取檔案的路徑
string dir;//= Path.GetDirectoryName(fname);
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
//dir = Path.GetDirectoryName(fname);
//fileEntries = Directory.GetFiles(fbd.SelectedPath);
fileEntries = Directory.GetFiles(fbd.SelectedPath, "*.*", SearchOption.AllDirectories);
MessageBox.Show("Files found: " + fileEntries.Length.ToString(), "Message");
}
}
Service.operateCSV csvSvc = new Service.operateCSV(); // readCsvTxt
System.Data.DataTable dt = new System.Data.DataTable();
bool isFirst = true;
foreach (string fileName in fileEntries)
{
#region filename InvID
string fName = Path.GetFileName(fileName);
rt1.AppendText(fName + " ");
rt1.SelectionStart = rt1.Text.Length;
rt1.ScrollToCaret();
#endregion
if (isFirst)
{
//csvSvc.clear_inv("hour");
csvSvc.taoYuan_createColumnHour(ref dt, fileName);
csvSvc.taoYuan_readCsvFile(ref dt, fileName, dt.Columns.Count, isFirst);
isFirst = false;
}
else
csvSvc.taoYuan_readCsvFile(ref dt, fileName, dt.Columns.Count, isFirst);
}
//MessageBox.Show(" 共 " + dt.Rows.Count.ToString());
lbSiteName_sensor.Text = " sun 亞東觀音 共:" + dt.Rows.Count.ToString();
//System.Data.DataTable dt = solarApp.Service.csvHelper.OpenCSV(fname);
csvSvc.taoYuan_insertHour2DB(ref dt, "sun_yadong");
MessageBox.Show("OK");
}
}
}