FIC_Solar/solarApp/Service/operateCSV.cs
2021-08-31 23:51:34 +08:00

383 lines
15 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using MySql.Data.MySqlClient;
using System.Configuration;
using Dapper;
namespace solarApp.Service
{
public class operateCSV
{
string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
/// <summary>
/// 讀取CSV檔案通過文字格式
/// </summary>
/// <param name="strpath"></param>
/// <returns></returns>
public DataTable readCsvTxt(string strpath)
{
int intColCount = 0;
bool blnFlag = true;
DataTable mydt = new DataTable("myTableName");
DataColumn mydc;
DataRow mydr;
string strline;
string[] aryline;
System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);
while ((strline = mysr.ReadLine()) != null)
{
aryline = strline.Split(',');
if (blnFlag)
{
blnFlag = false;
intColCount = aryline.Length;
for (int i = 0; i < aryline.Length; i++)
{
mydc = new DataColumn(aryline[i]);
mydt.Columns.Add(mydc);
}
}
mydr = mydt.NewRow();
for (int i = 0; i < intColCount; i++)
{
mydr[i] = aryline[i];
}
mydt.Rows.Add(mydr);
}
return mydt;
}
/// <summary>
/// 利用SQL查詢CSV
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
//public DataTable readCsvSql(string path, string filename, string deviceName)
//{
// string strconn = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;", path);
// string sql = string.Format("SELECT * FROM [{0}]", filename);
// using (OleDbConnection conn = new OleDbConnection(strconn))
// {
// DataTable dtTable = new DataTable();
// OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
// try
// {
// adapter.Fill(dtTable);
// logHelper.WriteLog(string.Format("[裝置]:{0}-->讀取檔案-->結果:成功", deviceName));
// }
// catch (Exception ex)
// {
// dtTable = new DataTable();
// logHelper.WriteLog(string.Format("[裝置]:{0}-->讀取檔案-->結果:{1}", deviceName, ex.Message));
// throw ex;
// }
// return dtTable;
// }
//}
/// <summary>
/// 讀取CSV檔案
/// </summary>
/// <param name="mycsvdt"></param>
/// <param name="filepath"></param>
/// <returns></returns>
public void 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);
}
//最後一欄 放 Inv編號
mydc = new DataColumn("inv");
mycsvdt.Columns.Add(mydc);
}
break;
}
}
catch (Exception e)
{
//throw (Stack.GetErrorStack(strpath + "讀取CSV檔案中的資料出錯." + e.Message, "OpenCSVFile("));
//return false;
}
}
public void createColumnDay(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 < 3) continue;
aryline = strline.Split(new char[] { ',' });
//給datatable加上列名
if (blnFlag)
{
blnFlag = false;
intColCount = aryline.Length;
int col = 0; int col6 = 0;
for (int i = 0; i < aryline.Length; i++)
{
col = i + 1;
mydc = new DataColumn(col.ToString());
mycsvdt.Columns.Add(mydc);
}
////最後一欄 放 Inv編號
//mydc = new DataColumn("inv");
//mycsvdt.Columns.Add(mydc);
}
break;
}
}
catch (Exception e)
{
//throw (Stack.GetErrorStack(strpath + "讀取CSV檔案中的資料出錯." + e.Message, "OpenCSVFile("));
//return false;
}
}
public bool readCsvFile(ref DataTable mycsvdt, string filepath, string InvID, int intColCount, bool readTitle)
{
string strpath = filepath; //csv檔案的路徑
try
{
bool blnFlag = true;
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)
{
if (rowIndex < 3)
{
continue;
// 欄位需要取row 3 invId , row 4 欄位名稱也需要讀取完
}
else if (rowIndex <= 4)
{
readTitle = false;
}
}
else {
if (rowIndex < 5) continue;
}
aryline = strline.Split(new char[] { ',' });
//填充資料並加入到datatable中
mydr = mycsvdt.NewRow();
for (int i = 0; i < intColCount; i++)
{
mydr[i] = aryline[i];
}
//mydr["inv"] = InvID;
mycsvdt.Rows.Add(mydr);
}
return true;
}
catch (Exception e)
{
//throw (Stack.GetErrorStack(strpath + "讀取CSV檔案中的資料出錯." + e.Message, "OpenCSVFile("));
return false;
}
}
public bool insertHour2DB(ref DataTable dt) {
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.src_inv(c1 ,c2 ,c3 ,c4 ,c5 ,c6 ,c7 ,c8 ,c9 ,c10 ,c11 ,c12 ,c13 ,c14 ,c15 ,c16 ,c17 ,c18 ,c19 ,inv)
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>("inv").ToString() + "' );");
if (i % 10 == 0)
{
conn.Execute(ss.ToString());
ss.Clear();
//ss.Append(@"insert into solar_import.src_inv(c1 ,c2 ,c3 ,c4 ,c5 ,c6 ,c7 ,c8 ,c9 ,c10 ,c11 ,c12 ,c13 ,c14 ,c15 ,c16 ,c17 ,c18 ,c19 ,inv)
// values( @c1 ,@c2 ,@c3 ,@c4 ,@c5 ,@c6 ,@c7 ,@c8 ,@c9 ,@c10 ,@c11 ,@c12 ,@c13 ,@c14 ,@c15 , @c16, @c17, @c18,@c19 ,@inv );");
//conn.Execute(ss.ToString(), new
//{
// c1 = row.Field<string>("1").ToString(), c2 = row.Field<string>("2").ToString(),
// c3 = row.Field<string>("3").ToString(), c4 = row.Field<string>("4").ToString(),
// c5 = row.Field<string>("5").ToString(), c6 = row.Field<string>("6").ToString(),
// c7 = row.Field<string>("7").ToString(), c8 = row.Field<string>("8").ToString(),
// c9 = row.Field<string>("9").ToString(), c10 = row.Field<string>("10").ToString(),
// c11 = row.Field<string>("11").ToString(), c12 = row.Field<string>("12").ToString(),
// c13 = row.Field<string>("13").ToString(), c14 = row.Field<string>("14").ToString(),
// c15 = row.Field<string>("15").ToString(), c16 = row.Field<string>("16").ToString(),
// c17 = row.Field<string>("17").ToString(), c18 = row.Field<string>("18").ToString(),
// c19 = row.Field<string>("19").ToString(), inv = row.Field<string>("inv").ToString()
//});
}
i++;
}
if (ss.Length > 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
conn.Clone();
}
return false;
}
public bool insertDay2DB(ref DataTable dt)
{
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.src_inv_day( 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)
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() + "');");
if (i % 10 == 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
i++;
}
if (ss.Length > 0)
{
conn.Execute(ss.ToString());
ss.Clear();
}
conn.Clone();
}
return false;
}
}
}