資料匯入 - 日報表匯入

This commit is contained in:
JiaHao Liu 2021-11-01 09:50:38 +08:00
parent 2ab5c096ee
commit 636ca21e3b
3 changed files with 288 additions and 12 deletions

View File

@ -132,7 +132,105 @@ values( '"
values( '"
+ station_name + "' ,'"
+ fname + "' ,'"
+ inverterID + "' , now() );");
+ 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 insert_dailyReport2DB(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() );");
ss.Append(@"insert into `solar_import`.`iPVita_file_list`( `station_name`, `filename`, inverterid, `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 bool insert_dailyReport2DB(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.iPVita_temp (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, insertDate)
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>("Column1").ToString() + "', now());");
if (i % 10 == 0)
{

View File

@ -42,6 +42,7 @@ 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_yadong = new System.Windows.Forms.Button();
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();
@ -50,7 +51,7 @@ namespace solarApp
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();
this.bt_dailyReport = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
@ -98,7 +99,7 @@ namespace solarApp
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(3, 640);
this.bt_archive.Name = "bt_archive";
this.bt_archive.Size = new System.Drawing.Size(282, 100);
this.bt_archive.Size = new System.Drawing.Size(226, 60);
this.bt_archive.TabIndex = 8;
this.bt_archive.Text = "單日歸檔";
this.bt_archive.UseVisualStyleBackColor = true;
@ -201,6 +202,7 @@ namespace solarApp
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.BackColor = System.Drawing.Color.PaleGoldenrod;
this.splitContainer1.Panel1.Controls.Add(this.bt_dailyReport);
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);
@ -223,6 +225,17 @@ namespace solarApp
this.splitContainer1.SplitterWidth = 10;
this.splitContainer1.TabIndex = 0;
//
// 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);
//
// bt_sun_taoyuan
//
this.bt_sun_taoyuan.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
@ -312,16 +325,16 @@ namespace solarApp
this.tabControl.Size = new System.Drawing.Size(1782, 853);
this.tabControl.TabIndex = 1;
//
// bt_sun_yadong
// bt_dailyReport
//
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);
this.bt_dailyReport.Font = new System.Drawing.Font("Microsoft JhengHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.bt_dailyReport.Location = new System.Drawing.Point(5, 706);
this.bt_dailyReport.Name = "bt_dailyReport";
this.bt_dailyReport.Size = new System.Drawing.Size(237, 74);
this.bt_dailyReport.TabIndex = 15;
this.bt_dailyReport.Text = "daily_report 太陽能光電-桃園全虹";
this.bt_dailyReport.UseVisualStyleBackColor = true;
this.bt_dailyReport.Click += new System.EventHandler(this.bt_dailyReport_Click);
//
// fmExcel
//
@ -369,5 +382,6 @@ namespace solarApp
private System.Windows.Forms.Button bt_gong34;
private System.Windows.Forms.Button bt_sun_taoyuan;
private System.Windows.Forms.Button bt_sun_yadong;
private System.Windows.Forms.Button bt_dailyReport;
}
}

View File

@ -555,5 +555,169 @@ namespace solarApp
csvSvc.taoYuan_insertHour2DB(ref dt, "sun_yadong");
MessageBox.Show("OK");
}
protected void ImporExcel_dailyReport(string site_name)
{
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(fileEntries, site_name);
xlsSvc.insert_dailyReport2DB(fileEntries, site_name);
#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];
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].Replace(".xlsx", ""); //只抓最後的檔名
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++)
{
#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;
}
mydc = new DataColumn("dataDate");
dt.Columns.Add("");
}
#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++)
{
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 == 15) 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!");
}
}//欄位 for
if (string.IsNullOrEmpty(row[5].ToString()) || string.IsNullOrWhiteSpace(row[5].ToString()))
{
MessageBox.Show("empty!");
}
else
{
if (i == 1) continue;// 第二個檔案之後的 firstRow 不需要, start from 1
else
{
//最後一個欄位:日期
row[row.ItemArray.Length-1] = filename;
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);
//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.insert_dailyReport2DB(dt, filename);
dt.Rows.Clear();
x++;
}
//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_dailyReport_Click(object sender, EventArgs e)
{
ImporExcel_dailyReport("");
}
}
}