给了我一段代码照着改写,但是之前没有接触过C#所以看不太懂,能不能帮我写下注释,把新程序写一下看看。谢谢了!
using System;
using System.Configuration;
using System.IO;
//using System.Configuration;
using Excel;
using System.Runtime.InteropServices;
namespace mes001
{
/// <summary>
/// PIQ_STADAT_INPUT 的摘要说明。
/// </summary>
public class PIQ_STADAT_INPUT : mes001.input_base
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
private LogAndMail logger = new LogAndMail();
private Edi_stadat_tag S_stadat;
private edi_stadat C_stadat;
private string cust_id="CRY";
private string factory="TEST";
private string update_user_name="MESPLUS";
//zhou dong bo 2014-08-21 start
//private LogAndMail logger = new LogAndMail();
private static string str_cry_piq_alarm_mail = ConfigurationManager.AppSettings["cry_piq_alarm_mail"].ToString();
//zhou dong bo 2014-08-21 end
public PIQ_STADAT_INPUT()
{
//
// TODO: 在此处添加构造函数逻辑
//
#if (DEBUG)
SourceDir = @"D:\vs\MES_LOADER_2014\sln_mes_loader_2014\sln_mes_loader_2014\bin\Debug\docs\CRY_PIQ\";
BackupDir = @"D:\vs\MES_LOADER_2014\sln_mes_loader_2014\sln_mes_loader_2014\bin\Debug\docs\CRY_PIQ\bak\";
#else
SourceDir = @"\\MESREPT\H101_share\EDI_IN\CRY_PIQ\";
BackupDir = @"\\MESREPT\H101_share\EDI_IN\CRY_PIQ\uploaded\";
#endif
}
public override int processfile(string input_file)
{
logger.DayLog("PIQ_STADAT_INPUT.cs - processfile - " + input_file);
string now = DateTime.Now.ToString("yyyyMMddHHmmss");
string up_str="";
int ret_i = 0;
string sfilename = Path.GetFileName(input_file);
if (sfilename.IndexOf("_TIQ") > 0)
{
logger.DayLog("Skip TIQ file ");
ret_i=0;
}
string err_loc = "0";
object filename = input_file.ToString();//must be object for wb.close use this as the 2nd parameter
object MissingValue = Type.Missing; //设置默认值
//wei peng 2014-09-12 for open excel must close it start
//Excel.Application xlsApp = new Excel.ApplicationClass();
//Excel.Workbook xlsFile = xlsApp.Workbooks.Open(filename.ToString(), MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue);
//Excel.Workbook wb ;
//Excel.Worksheet ws;
//wei peng 2014-09-12 for open excel must close it end
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
try
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
err_loc = "1";
Excel.Application xlsApp = new Excel.ApplicationClass();
xlsApp.DisplayAlerts = false;
err_loc = "2";
if (xlsApp != null)
{
err_loc = "3";
Excel.Workbook wb = xlsApp.Workbooks.Open(filename.ToString(), 0, true, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue);
err_loc = "4";
if (wb != null)
{
err_loc = "5";
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1]; //2010-9-9. customer require only import SHEET 1 data.
err_loc = "6";
int iRowCnt = 0, iBgnRow;
iRowCnt = 0 + ws.UsedRange.Cells.Rows.Count; //记录数
iBgnRow = (ws.UsedRange.Cells.Row > 1) ? ws.UsedRange.Cells.Row - 1 : ws.UsedRange.Cells.Row; //起始行
for (int i = iBgnRow + 1; i < iRowCnt + iBgnRow; i++)//排除第一行
{
S_stadat.CUST_ID = cust_id;
S_stadat.DATA_TYPE = "CRY_PIQ";
S_stadat.FACTORY = factory;
S_stadat.KEY1 = ((Excel.Range)ws.UsedRange.Cells[i, 3]).Text.ToString().Trim();//c:DEVICE
S_stadat.DATA1 = ((Excel.Range)ws.UsedRange.Cells[i, 9]).Text.ToString().Trim();//i:MSL
S_stadat.DATA2 = ((Excel.Range)ws.UsedRange.Cells[i, 10]).Text.ToString().Trim();//j:BAKE TIME
S_stadat.DATA3 = ((Excel.Range)ws.UsedRange.Cells[i, 14]).Text.ToString().Trim().ToUpper().Replace('C', ' ');//n:PeakReflowTemp
S_stadat.DATA4 = ((Excel.Range)ws.UsedRange.Cells[i, 15]).Text.ToString().Trim().ToUpper().Replace("DAYS", "");//o:expire days
S_stadat.DATA5 = ((Excel.Range)ws.UsedRange.Cells[i, 25]).Text.ToString().Trim(); //y:rohs
S_stadat.DATA6 = ((Excel.Range)ws.UsedRange.Cells[i, 24]).Text.ToString().Trim(); //x:ROHS Compliant
S_stadat.UPDATE_TIME = now;
S_stadat.UPDATE_USER_NAME = update_user_name;
C_stadat = new edi_stadat(S_stadat);
//logger.DayLog(i.ToString() + " - into C_stadat.store_db()");
C_stadat.store_db();
//2014-08-21 zhou dongbo start
if (filename.ToString().IndexOf("SCC_PIQ") > 0)
{
if (C_stadat.store_db() == 2)
{
up_str += "\n" + S_stadat.KEY1;
}
}
//2014-08-21 zhoudongbo end
//*********************************weipeng 2014-09-12 for open excel must close it start
//System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 3]);
//System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 9]);
//System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 10]);
//System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 14]);
//System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 15]);
//System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 25]);
//System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 24]);
//*********************************weipeng 2014-09-12 for open excel must close it end
}
err_loc = "7";
wb.Close(false, filename, MissingValue);
xlsApp.ActiveWorkbook.Close(false, MissingValue, MissingValue);
System.Threading.Thread.Sleep(3000);
err_loc = "8";
ret_i = 0;
//*********************************YC 2014-12-04 start
try
{
if (wb != null)
{
err_loc = "9";
wb.Close(MissingValue, filename, MissingValue);
err_loc = "9.0";
Marshal.ReleaseComObject(ws);
err_loc = "9.1";
Marshal.ReleaseComObject(wb);
err_loc = "9.2";
xlsApp.Quit();
err_loc = "9.3";
}
}
catch (Exception ex1)
{
logger.DayLog("err_loc : " + err_loc);
logger.DayLog("wb != null : " + ex1.Message);
}
logger.DayLog("err_loc : " + err_loc);
3 个解决方案
#1
//*********************************YC 2014-12-04 end
//*********************************weipeng 2014-09-12 for open excel must close it start
//wb.Close(false, Type.Missing, Type.Missing);//wb.Close(MissingValue, filename, MissingValue);
//xlsApp.Workbooks.Close();
//xlsApp.Quit();
//System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
//ws = null;
//wb = null;
//xlsApp = null;
//GC.Collect();
//*********************************weipeng 2014-12-16 start
try
{
int k = 0;
err_loc = "10";
IntPtr t = new IntPtr(xlsApp.Hwnd);
err_loc = "10.1";
GetWindowThreadProcessId(t, out k);
err_loc = "10.2";
if (k != 0)
{
err_loc = "10.3";
logger.DayLog("Excel process num: " + k.ToString() + " closing it");
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
else
{
err_loc = "10.4";
logger.DayLog("Excel process num: " + k.ToString() + " CANNOT CLOSE IT !!!!!");
}
}
catch (Exception ex2)
{
logger.DayLog("err_loc : " + err_loc);
logger.DayLog("IntPtr t = new IntPtr(xlsApp.Hwnd) Err : " + ex2.Message);
}
logger.DayLog("err_loc : " + err_loc);
//*********************************weipeng 2014-12-16 end
//*********************************weipeng 2014-09-12 for open excel must close it end
}
else
{
logger.DayLog("Excel.Workbook wb = xlsApp.Workbooks.Open(filename.ToString() Err");
}
}
else
{
xlsApp.Quit();
logger.DayLog(" Excel.Application xlsApp = new Excel.ApplicationClass(); ERR");
}
}
catch (Exception ex)
{
logger.DayLog("err_loc : " + err_loc);
logger.DayLog("PIQ_STADAT_INPUT.cs - processfile Err: " + ex.Message);
SendMailOut("Can't process " + input_file + "\n" + ex.ToString());
ret_i = 1;
}
finally
{
logger.DayLog("here finally");
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
//wb = null;
//xlsApp = null;
//input_file = null;
}
//zhou dong bo 2014-08-21 start
if (up_str.Length > 0)
{
logger.DayLog("cry piq changed : " + up_str);
SendMailOut("Changed Device: " + up_str + "\n", str_cry_piq_alarm_mail);
ret_i = 0;
}
//zhou dong bo 2014-08-21 end
return ret_i;
}
}
}
代码写不下来两段写
#2
你这代码 没人会看的..
思路就是 2个xls分别读取 到2个datatable中.
然后在一个xls创建2个sheet 分别把2个datatable写入进去就OK了..
所以你这个问题.归咎就是"导入导出".
不如看看 http://bbs.csdn.net/topics/390830774
思路就是 2个xls分别读取 到2个datatable中.
然后在一个xls创建2个sheet 分别把2个datatable写入进去就OK了..
所以你这个问题.归咎就是"导入导出".
不如看看 http://bbs.csdn.net/topics/390830774
#3
你好,真的是刚接触C#一点都不明白能不能留个联系方式我具体问一下。
#1
//*********************************YC 2014-12-04 end
//*********************************weipeng 2014-09-12 for open excel must close it start
//wb.Close(false, Type.Missing, Type.Missing);//wb.Close(MissingValue, filename, MissingValue);
//xlsApp.Workbooks.Close();
//xlsApp.Quit();
//System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
//ws = null;
//wb = null;
//xlsApp = null;
//GC.Collect();
//*********************************weipeng 2014-12-16 start
try
{
int k = 0;
err_loc = "10";
IntPtr t = new IntPtr(xlsApp.Hwnd);
err_loc = "10.1";
GetWindowThreadProcessId(t, out k);
err_loc = "10.2";
if (k != 0)
{
err_loc = "10.3";
logger.DayLog("Excel process num: " + k.ToString() + " closing it");
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
else
{
err_loc = "10.4";
logger.DayLog("Excel process num: " + k.ToString() + " CANNOT CLOSE IT !!!!!");
}
}
catch (Exception ex2)
{
logger.DayLog("err_loc : " + err_loc);
logger.DayLog("IntPtr t = new IntPtr(xlsApp.Hwnd) Err : " + ex2.Message);
}
logger.DayLog("err_loc : " + err_loc);
//*********************************weipeng 2014-12-16 end
//*********************************weipeng 2014-09-12 for open excel must close it end
}
else
{
logger.DayLog("Excel.Workbook wb = xlsApp.Workbooks.Open(filename.ToString() Err");
}
}
else
{
xlsApp.Quit();
logger.DayLog(" Excel.Application xlsApp = new Excel.ApplicationClass(); ERR");
}
}
catch (Exception ex)
{
logger.DayLog("err_loc : " + err_loc);
logger.DayLog("PIQ_STADAT_INPUT.cs - processfile Err: " + ex.Message);
SendMailOut("Can't process " + input_file + "\n" + ex.ToString());
ret_i = 1;
}
finally
{
logger.DayLog("here finally");
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
//wb = null;
//xlsApp = null;
//input_file = null;
}
//zhou dong bo 2014-08-21 start
if (up_str.Length > 0)
{
logger.DayLog("cry piq changed : " + up_str);
SendMailOut("Changed Device: " + up_str + "\n", str_cry_piq_alarm_mail);
ret_i = 0;
}
//zhou dong bo 2014-08-21 end
return ret_i;
}
}
}
代码写不下来两段写
#2
你这代码 没人会看的..
思路就是 2个xls分别读取 到2个datatable中.
然后在一个xls创建2个sheet 分别把2个datatable写入进去就OK了..
所以你这个问题.归咎就是"导入导出".
不如看看 http://bbs.csdn.net/topics/390830774
思路就是 2个xls分别读取 到2个datatable中.
然后在一个xls创建2个sheet 分别把2个datatable写入进去就OK了..
所以你这个问题.归咎就是"导入导出".
不如看看 http://bbs.csdn.net/topics/390830774
#3
你好,真的是刚接触C#一点都不明白能不能留个联系方式我具体问一下。