231 lines
9.1 KiB
C#
231 lines
9.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
// 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.IO;
|
|
|
|
namespace solarApp
|
|
{
|
|
public partial class fmExcel : Form
|
|
{
|
|
System.Data.DataTable dt_cap1 = new System.Data.DataTable();
|
|
|
|
public fmExcel()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void bt_inv_hour_hj_Click(object sender, EventArgs e)
|
|
{
|
|
string fname = "";
|
|
OpenFileDialog fdlg = new OpenFileDialog();
|
|
fdlg.Title = "Excel File Dialog";
|
|
fdlg.InitialDirectory = @"c:\";
|
|
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
|
|
fdlg.FilterIndex = 2;
|
|
fdlg.RestoreDirectory = true;
|
|
if (fdlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
fname = fdlg.FileName;
|
|
}
|
|
else return;
|
|
//取得選取檔案的路徑
|
|
string dir = Path.GetDirectoryName(fname);
|
|
// 取得路徑下所有檔案
|
|
string[] fileEntries = System.IO.Directory.GetFiles(dir);
|
|
|
|
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);
|
|
string[] ss = fName.Split("_");
|
|
string InvID = ss[0];
|
|
#endregion
|
|
|
|
if (isFirst)
|
|
{
|
|
csvSvc.clear_inv("hour");
|
|
csvSvc.createColumnHour(ref dt, fileName);
|
|
csvSvc.readCsvFile(ref dt, fileName, InvID, dt.Columns.Count, isFirst);
|
|
isFirst = false;
|
|
}
|
|
else
|
|
csvSvc.readCsvFile(ref dt, fileName, InvID, dt.Columns.Count, isFirst);
|
|
}
|
|
MessageBox.Show(" 共 "+ dt.Rows.Count.ToString());
|
|
|
|
//System.Data.DataTable dt = solarApp.Service.csvHelper.OpenCSV(fname);
|
|
dataGridView1.DataSource = csvSvc.insertHour2DB(ref dt);
|
|
MessageBox.Show("OK");
|
|
}
|
|
protected void ImporExcel()
|
|
{
|
|
string fname = "";
|
|
OpenFileDialog fdlg = new OpenFileDialog();
|
|
//fdlg.Title = "Excel File Dialog";
|
|
//fdlg.InitialDirectory = @"d:\temp\";
|
|
//fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
|
|
//fdlg.FilterIndex = 2;
|
|
//fdlg.RestoreDirectory = true;
|
|
//if (fdlg.ShowDialog() == DialogResult.OK)
|
|
//{
|
|
// fname = fdlg.FileName;
|
|
//}
|
|
//else return;
|
|
//取得選取檔案的路徑
|
|
//string dir = Path.GetDirectoryName(fname);
|
|
// 取得路徑下所有檔案
|
|
//string[] fileEntries = System.IO.Directory.GetFiles(dir);
|
|
string[] fileEntries = Directory.GetFiles(@"D:\temp\AUO\台中太平\台中太平\inverter", "*.*", SearchOption.AllDirectories);
|
|
|
|
// dt.Column = colCount;
|
|
|
|
System.Data.DataTable dt = new System.Data.DataTable();
|
|
DataColumn mydc;
|
|
bool isFirst = true; bool isFirstData = true;
|
|
|
|
foreach (string fileName in fileEntries)
|
|
{
|
|
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
|
|
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileName);
|
|
Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
|
|
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)
|
|
{
|
|
for (int j = 1; j <= colCount; j++)
|
|
{
|
|
int col = 0;
|
|
col = j + 1;
|
|
mydc = new DataColumn(col.ToString());
|
|
dt.Columns.Add(mydc);
|
|
isFirst = false;
|
|
}
|
|
}
|
|
#endregion
|
|
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
|
|
GC.Collect();
|
|
GC.WaitForPendingFinalizers();
|
|
|
|
MessageBox.Show(" dt.Rows.Count = " + dt.Rows.Count.ToString());
|
|
//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_inv_day_hj_Click(object sender, EventArgs e)
|
|
{
|
|
string fname = "";
|
|
OpenFileDialog fdlg = new OpenFileDialog();
|
|
fdlg.Title = "Excel File Dialog";
|
|
fdlg.InitialDirectory = @"c:\";
|
|
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
|
|
fdlg.FilterIndex = 2;
|
|
fdlg.RestoreDirectory = true;
|
|
if (fdlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
fname = fdlg.FileName;
|
|
}
|
|
else return;
|
|
//取得選取檔案的路徑
|
|
string dir = Path.GetDirectoryName(fname);
|
|
// 取得路徑下所有檔案
|
|
string[] fileEntries = System.IO.Directory.GetFiles(dir);
|
|
|
|
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);
|
|
string[] ss = fName.Split("_");
|
|
string InvID = ss[0];
|
|
#endregion
|
|
|
|
if (isFirst)
|
|
{
|
|
csvSvc.clear_inv("day");
|
|
csvSvc.createColumnDay(ref dt, fileName);
|
|
csvSvc.readCsvFile(ref dt, fileName, "", dt.Columns.Count, isFirst);
|
|
isFirst = false;
|
|
}
|
|
else
|
|
csvSvc.readCsvFile(ref dt, fileName, "", dt.Columns.Count, isFirst);
|
|
}
|
|
MessageBox.Show(" 共 " + dt.Rows.Count.ToString());
|
|
|
|
//System.Data.DataTable dt = solarApp.Service.csvHelper.OpenCSV(fname);
|
|
dataGridView1.DataSource = dt;
|
|
csvSvc.insertDay2DB(ref dt);
|
|
MessageBox.Show("OK");
|
|
}
|
|
|
|
private void bt_day_archive_hj_Click(object sender, EventArgs e)
|
|
{
|
|
Service.operateCSV csvSvc = new Service.operateCSV(); // readCsvTxt
|
|
csvSvc.archive_data("day");
|
|
MessageBox.Show("ok");
|
|
}
|
|
|
|
private void bt_hour_archive_hj_Click(object sender, EventArgs e)
|
|
{
|
|
Service.operateCSV csvSvc = new Service.operateCSV(); // readCsvTxt
|
|
csvSvc.archive_data("hour");
|
|
MessageBox.Show("ok");
|
|
}
|
|
|
|
private void bt_clear_station_Click(object sender, EventArgs e)
|
|
{
|
|
ImporExcel();
|
|
}
|
|
}
|
|
}
|