อ่านข้อมูลไฟล์ใน excel พออ่านเสร็จ โปรแกรมไม่ยอมปิดไฟล์ excel นั้นๆให้
Code (VB.NET)
MyConnection.Dispose()
น่าจะเป็น Dispose() แทนครับ
Date :
2015-03-23 20:32:17
By :
mr.win
ปิดมันแบบหน้าด้านๆเลยได้ป่าวครับ
Code (C#)
foreach (System.Diagnostics.Process clsProcess in System.Diagnostics.Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains("Excel"))
{
clsProcess.Kill();
}
}
Date :
2015-03-24 09:12:31
By :
lamaka.tor
Date :
2015-03-24 15:59:25
By :
lamaka.tor
เรียก excel interop ใน background worker
ใช้เสร็จแล้วปิด dispose excel แล้วก็ปิด background worker ทิ้ง process จะไม่ค้าง
Date :
2015-03-24 16:09:31
By :
เคยตอบไปแล้ว ไปค้นของเก่าเอง
Code (C#)
public static System.Data.DataTable GetTableFormExcel(string _SQL, string FileExcel)
{
string _ConnectionString = @"Data Source=" + FileExcel + "; Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;";
try
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(_ConnectionString);
conn.Open();
string strQuery = _SQL;
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
return ds.Tables[0];
}
catch { return null; }
}
ผมใช้แบบนี้อยู่อ่าครับ
ไม่เห็นมันขึ้น excel.exe มาให้ลบเลยนิครับ
Code (C#)
private void RefreshData()
{
Text = "กรุณารอ...";
Cursor = Cursors.WaitCursor;
pathTemp = System.IO.Path.GetTempPath() + "\\Report" + String.Format("{0:ddMMyyyy HHmmss}", DateTime.Now);
TORServices.clsFile_Path.CreatePath(pathTemp);
List<string> _file = new List<string>();
_file = TORServices.clsFile_Path.GetFilesRecursive(TORServices.clsPathData.Path_AllReport, "*.Report");///@"H:\REPORT"
if (_file.Count <= 0) { Cursor = Cursors.Default; return; }
if (dt != null) { dt = null; }
dt = new DataTable();
dt.Columns.Add(new DataColumn("SampleID", typeof(string)));
dt.Columns.Add(new DataColumn("Parameter", typeof(string)));
dt.Columns.Add(new DataColumn("Value", typeof(string)));
dt.Columns.Add(new DataColumn("Date", typeof(string)));
dt.Columns.Add(new DataColumn("ผู้วิเคราะห์", typeof(string)));
dt.Columns.Add(new DataColumn("Seleced", typeof(Boolean)));
string FileExcel;
foreach (string _f in _file)
{
//เช็คการเปิดไฟล์ถ้าเปิดให้ก๊อบไปวางที่ Temp
if (System.IO.File.GetCreationTime(_f) >= dateTimePicker1.Value && System.IO.File.GetCreationTime(_f) <= dateTimePicker2.Value)
{
//MessageBox.Show(_f);
if (!(TORServices.clsFile_Path.IsFileExcelOpen(_f)))
{
FileExcel = System.IO.Path.GetTempPath() + String.Format("{0:ddMMyyyy HHmmss}", DateTime.Now) + ".Report";
System.IO.File.Copy(_f, FileExcel);
}
else
{
FileExcel = _f;
}
// FileExcel = _f;
DataTable _dt1 = TORServices.clsDatabaseServices.GetTableFormExcel("SELECT * FROM `mg_Kg$Print_Area`", FileExcel);
DataTable _dt2 = TORServices.clsDatabaseServices.GetTableFormExcel("SELECT * FROM `mg_L$Print_Area`", FileExcel);
if (_dt1 != null) { dgvmgKg.DataSource = _dt1; }
if (_dt2 != null) { dgvmgL.DataSource = _dt2; }
for (int i = 0; i < this.dgvmgL.RowCount - 1; i++)
{
if (this.dgvmgL[this.dgvmgL.ColumnCount - 1, i].Value.ToString().Trim().Length != 0)
{
DataRow dr = dt.NewRow();
dr["SampleID"] = this.dgvmgL[this.dgvmgL.ColumnCount - 6, i].Value.ToString().Trim();
dr["Parameter"] = this.dgvmgL[this.dgvmgL.ColumnCount - 5, i].Value.ToString().Trim();
dr["Value"] = this.dgvmgL[this.dgvmgL.ColumnCount - 4, i].Value.ToString().Trim();
dr["Date"] = this.dgvmgL[this.dgvmgL.ColumnCount - 3, i].Value.ToString().Trim();
dr["ผู้วิเคราะห์"] = this.dgvmgL[this.dgvmgL.ColumnCount - 2, i].Value.ToString().Trim();
dr["Seleced"] = false;
dt.Rows.Add(dr);
}
}
for (int i = 0; i < this.dgvmgKg.RowCount - 1; i++)
{
if (this.dgvmgKg[this.dgvmgKg.ColumnCount - 1, i].Value.ToString().Trim().Length != 0)
{
DataRow dr = dt.NewRow();
dr["SampleID"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 6, i].Value.ToString().Trim();
dr["Parameter"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 5, i].Value.ToString().Trim();
dr["Value"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 4, i].Value.ToString().Trim();
dr["Date"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 3, i].Value.ToString().Trim();
dr["ผู้วิเคราะห์"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 2, i].Value.ToString().Trim();
dr["Seleced"] = false;
dt.Rows.Add(dr);
}
}
dataGridView1.DataSource = dt;
dataGridView1.Columns[3].Width = 120;
dataGridView1.Columns[4].Width = 150;
dataGridView1.Columns[5].Width = 60;
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Ascending);
Cursor = Cursors.Default;
Text = "Report HM";
}
}
}
Date :
2015-03-25 08:58:53
By :
lamaka.tor
Code (C#)
public static System.Data.DataTable GetTableFormExcel(string _SQL, string FileExcel)
{
string _ConnectionString = @"Data Source=" + FileExcel + "; Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;";
try
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(_ConnectionString);
conn.Open();
string strQuery = _SQL;
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
return ds.Tables[0];
}
catch { return null; }
}
ผมใช้แบบนี้อยู่อ่าครับ
ไม่เห็นมันขึ้น excel.exe มาให้ลบเลยนิครับ
Code (C#)
private void RefreshData()
{
Text = "กรุณารอ...";
Cursor = Cursors.WaitCursor;
pathTemp = System.IO.Path.GetTempPath() + "\\Report" + String.Format("{0:ddMMyyyy HHmmss}", DateTime.Now);
TORServices.clsFile_Path.CreatePath(pathTemp);
List<string> _file = new List<string>();
_file = TORServices.clsFile_Path.GetFilesRecursive(TORServices.clsPathData.Path_AllReport, "*.Report");///@"H:\REPORT"
if (_file.Count <= 0) { Cursor = Cursors.Default; return; }
if (dt != null) { dt = null; }
dt = new DataTable();
dt.Columns.Add(new DataColumn("SampleID", typeof(string)));
dt.Columns.Add(new DataColumn("Parameter", typeof(string)));
dt.Columns.Add(new DataColumn("Value", typeof(string)));
dt.Columns.Add(new DataColumn("Date", typeof(string)));
dt.Columns.Add(new DataColumn("ผู้วิเคราะห์", typeof(string)));
dt.Columns.Add(new DataColumn("Seleced", typeof(Boolean)));
string FileExcel;
foreach (string _f in _file)
{
//เช็คการเปิดไฟล์ถ้าเปิดให้ก๊อบไปวางที่ Temp
if (System.IO.File.GetCreationTime(_f) >= dateTimePicker1.Value && System.IO.File.GetCreationTime(_f) <= dateTimePicker2.Value)
{
//MessageBox.Show(_f);
if (!(TORServices.clsFile_Path.IsFileExcelOpen(_f)))
{
FileExcel = System.IO.Path.GetTempPath() + String.Format("{0:ddMMyyyy HHmmss}", DateTime.Now) + ".Report";
System.IO.File.Copy(_f, FileExcel);
}
else
{
FileExcel = _f;
}
// FileExcel = _f;
DataTable _dt1 = TORServices.clsDatabaseServices.GetTableFormExcel("SELECT * FROM `mg_Kg$Print_Area`", FileExcel);
DataTable _dt2 = TORServices.clsDatabaseServices.GetTableFormExcel("SELECT * FROM `mg_L$Print_Area`", FileExcel);
if (_dt1 != null) { dgvmgKg.DataSource = _dt1; }
if (_dt2 != null) { dgvmgL.DataSource = _dt2; }
for (int i = 0; i < this.dgvmgL.RowCount - 1; i++)
{
if (this.dgvmgL[this.dgvmgL.ColumnCount - 1, i].Value.ToString().Trim().Length != 0)
{
DataRow dr = dt.NewRow();
dr["SampleID"] = this.dgvmgL[this.dgvmgL.ColumnCount - 6, i].Value.ToString().Trim();
dr["Parameter"] = this.dgvmgL[this.dgvmgL.ColumnCount - 5, i].Value.ToString().Trim();
dr["Value"] = this.dgvmgL[this.dgvmgL.ColumnCount - 4, i].Value.ToString().Trim();
dr["Date"] = this.dgvmgL[this.dgvmgL.ColumnCount - 3, i].Value.ToString().Trim();
dr["ผู้วิเคราะห์"] = this.dgvmgL[this.dgvmgL.ColumnCount - 2, i].Value.ToString().Trim();
dr["Seleced"] = false;
dt.Rows.Add(dr);
}
}
for (int i = 0; i < this.dgvmgKg.RowCount - 1; i++)
{
if (this.dgvmgKg[this.dgvmgKg.ColumnCount - 1, i].Value.ToString().Trim().Length != 0)
{
DataRow dr = dt.NewRow();
dr["SampleID"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 6, i].Value.ToString().Trim();
dr["Parameter"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 5, i].Value.ToString().Trim();
dr["Value"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 4, i].Value.ToString().Trim();
dr["Date"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 3, i].Value.ToString().Trim();
dr["ผู้วิเคราะห์"] = this.dgvmgKg[this.dgvmgKg.ColumnCount - 2, i].Value.ToString().Trim();
dr["Seleced"] = false;
dt.Rows.Add(dr);
}
}
dataGridView1.DataSource = dt;
dataGridView1.Columns[3].Width = 120;
dataGridView1.Columns[4].Width = 150;
dataGridView1.Columns[5].Width = 60;
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Ascending);
Cursor = Cursors.Default;
Text = "Report HM";
}
}
}
Date :
2015-03-25 08:59:37
By :
lamaka.tor
Load balance : Server 04