269 lines
11 KiB
C#
269 lines
11 KiB
C#
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;
|
|
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);
|
|
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 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();
|
|
// 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();
|
|
// }
|
|
//}
|
|
}
|
|
}
|