久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

C#導(dǎo)出EXCEL 并解決EXCEL進(jìn)程問題!

 COPY&PASTE 2009-10-05
C#導(dǎo)出EXCEL 并解決EXCEL進(jìn)程問題,!
2008年08月04日 星期一 14:01
 

大家看下我的完整代碼吧:
public void daochu_all(DataSet exl_target, DataSet exl_workRTC, DataSet exl_workType, DataSet exl_rateAdd, DataSet exl_ratePhase, DataSet exl_bugRemove, DataSet exl_reviewSpeed, string path)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlBook = null;
string fullpath = path.Substring(0, path.Length - 4) + "fileupload ";
string templatepath = fullpath + "\\BaselineTemplate ";
string productpath = fullpath + "\\BaselineProduct ";
string userpath = templatepath + "\\excel.xls ";

DirectoryInfo dtemplate = new DirectoryInfo(templatepath);
DirectoryInfo dproduct = new DirectoryInfo(productpath);

if (dtemplate.Exists)
{
}
else
{
Directory.CreateDirectory(templatepath);
}

if (dproduct.Exists)
{
}
else
{
Directory.CreateDirectory(productpath);
}

if (File.Exists(userpath))
{
try
{
xlBook = xlApp.Workbooks.Add(userpath);
//xlSheet = (Excel.Worksheet)xlApp.Worksheets.get_Item( "項(xiàng)目性能查詢 (性能基線表) ");

System.Data.DataTable Table_target = exl_target.Tables[0];
System.Data.DataTable Table_workRTC = exl_workRTC.Tables[0];
System.Data.DataTable Table_workType = exl_workType.Tables[0];
System.Data.DataTable Table_rateAdd = exl_rateAdd.Tables[0];
System.Data.DataTable Table_ratePhase = exl_ratePhase.Tables[0];
System.Data.DataTable Table_bugRemove = exl_bugRemove.Tables[0];
System.Data.DataTable Table_reviewSpeed = exl_reviewSpeed.Tables[0];
double a = Convert.ToDouble(Table_target.Rows[0][2]);
double a1 = Convert.ToDouble(Table_target.Rows[0][3]);
double a2 = Convert.ToDouble(Table_target.Rows[0][4]);

double a3 = Convert.ToDouble(Table_target.Rows[1][2]);
double a4 = Convert.ToDouble(Table_target.Rows[1][3]);
double a5 = Convert.ToDouble(Table_target.Rows[1][4]);

//--------------------整體性能-------------------

int Tablerow = 5;


for (int i = 0; i < Table_target.Rows.Count; i++)//取記錄值
{
string tmpName = Table_target.Rows[i][0].ToString();

for (int j = 0; j < (Table_target.Columns.Count - 2); j++)
{

if (tmpName == "1 " || tmpName == "2 " || tmpName == "5 ")
{
xlApp.Cells[Tablerow, j + 2] = Convert.ToDouble((Table_target.Rows[i][j + 2].ToString())) / 100;
}
else
{
xlApp.Cells[Tablerow, j + 2] = Table_target.Rows[i][j + 2].ToString();
}

}
Tablerow++;
}

//--------------------工作量比重-相對于編碼-------------------

int Table2row = 16;

for (int i = 0; i < Table_workRTC.Rows.Count; i++)//取記錄值
{

for (int j = 0; j < (Table_workRTC.Columns.Count - 2); j++)
{

xlApp.Cells[Table2row, j + 2] = Table_workRTC.Rows[i][j + 2].ToString();

}
Table2row++;
}

//--------------------各類工作比例-------------------

int Table3row = 32;

for (int i = 0; i < Table_workType.Rows.Count; i++)//取記錄值
{

for (int j = 0; j < (Table_workType.Columns.Count - 2); j++)
{

xlApp.Cells[Table3row, j + 2] = Convert.ToDouble((Table_workType.Rows[i][j + 2].ToString())) / 100;

}
Table3row++;
}

//--------------------缺陷排除率-累計(jì)-------------------

int Table4row = 56;

for (int i = 0; i < Table_rateAdd.Rows.Count; i++)//取記錄值
{

for (int j = 0; j < (Table_rateAdd.Columns.Count - 2); j++)
{

xlApp.Cells[Table4row, j + 2] = Convert.ToDouble((Table_rateAdd.Rows[i][j + 2].ToString())) / 100;

}
Table4row++;
}

//--------------------缺陷排除率-當(dāng)前階段-------------------

int Table5row = 71;

for (int i = 0; i < Table_ratePhase.Rows.Count; i++)//取記錄值
{

for (int j = 0; j < (Table_ratePhase.Columns.Count - 2); j++)
{

xlApp.Cells[Table5row, j + 2] = Convert.ToDouble((Table_ratePhase.Rows[i][j + 2].ToString())) / 100;

}
Table5row++;
}

//--------------------缺陷排除密度-------------------

int Table6row = 84;

for (int i = 0; i < Table_bugRemove.Rows.Count; i++)//取記錄值
{

for (int j = 0; j < (Table_bugRemove.Columns.Count - 2); j++)
{

xlApp.Cells[Table6row, j + 2] = Table_bugRemove.Rows[i][j + 2].ToString();

}
Table6row++;
}

//--------------------評(píng)審速度-------------------

int Table7row = 99;

for (int i = 0; i < Table_reviewSpeed.Rows.Count; i++)//取記錄值
{

for (int j = 0; j < (Table_reviewSpeed.Columns.Count - 2); j++)
{

xlApp.Cells[Table7row, j + 2] = Table_reviewSpeed.Rows[i][j + 2].ToString();

}
Table7row++;
}

xlApp.Application.DisplayAlerts = false;
xlBook.SaveAs(productpath + "\\BaselineProduct.xls ", xlApp.ActiveWorkbook.FileFormat, " ", " ", xlApp.ActiveWorkbook.ReadOnlyRecommended, xlApp.ActiveWorkbook.CreateBackup, XlSaveAsAccessMode.xlNoChange, xlApp.ActiveWorkbook.ConflictResolution, false, " ", " ", " ");
}
catch
{
throw new Exception( "生成性能基線所需模板或數(shù)據(jù)庫中數(shù)據(jù)有誤 ");
}
finally
{
Process[] processes = Process.GetProcesses;
Process process;
foreach (process in processes)
{

}

if (xlBook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
xlBook = null;
}
if (xlApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp.Quit();
xlApp = null;
}
GC.Collect();
}
}
else
{
throw new Exception( "服務(wù)器BaselineTemplate中未發(fā)現(xiàn)模板文件 ");
}
}

 

我也碰到過,,解決的方法和JoneG(瓊瓊) 說的一樣,,只是我是把所有excel操作放到一個(gè)方法里(包括xlApp.Quit();等),然后在方法外面再GC.Collect();
如 public voic excel操作處理(string path)
{
........
}

調(diào)用處
excel操作處理(strpath;
GC.Collect();
這樣應(yīng)該就可以了

最好不要?dú)⑦M(jìn)程

public void aaa()
{
bbb();
System.GC.Collect(); //在這里調(diào)用垃圾收集
}

public void bbb()
{
操作excel();
該怎么處理還是怎么處理
}

就OK了

public void DestoryExcel()
{
oBook.Close( false, null, null );
oExcel.Workbooks.Close();
oExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject( oRange );
System.Runtime.InteropServices.Marshal.ReleaseComObject( oWorkSheet );
System.Runtime.InteropServices.Marshal.ReleaseComObject( oSheet );
System.Runtime.InteropServices.Marshal.ReleaseComObject( oBook );
System.Runtime.InteropServices.Marshal.ReleaseComObject( oExcel );
oRange = null;
oWorkSheet = null;
oSheet = null;
oBook = null;
oMissing = null;
oExcel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}

如果是WinForm程式的話

Excel.ApplicationClass exc = new Excel.ApplicationClass ();

if ( exc == null )
{
MessageBox.Show ( "ERROR: EXCEL couldn 't be started ");
}

Excel.Workbooks exBooks=null;
Excel._Workbook exBook=null;
Excel._Worksheet exSheet=null;
object mo=System.Reflection.Missing.Value;

try
{
exc=new Excel.ApplicationClass();
exBooks=exc.Workbooks;
exBook=exBooks.Open(ExcelFile.Text,mo,mo,mo,mo,mo,mo,mo,mo,mo,mo,mo,mo);
exSheet = (Excel._Worksheet)exBook.Worksheets[ "Equipments "];//Sheet1為你工作表的名稱,,也可以是數(shù)字,那就是你工作表的序號(hào)
....
}
catch( System.Exception exp)
{
MessageBox.Show (exp.Message );
}
finally
{
//exBook.Save();

exBook.Close(true,mo,mo);
exBooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(exSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(exBook);

System.Runtime.InteropServices.Marshal.ReleaseComObject(exBooks);
exBooks=null;
exBook = null;
exSheet = null;
exc.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(exc);
exc = null;
GC.Collect();

}

就可以直接結(jié)束掉了.

如果是Web程式

System.DateTime begintime ; //啟動(dòng)之前的時(shí)間
System.DateTime afterTime; //啟動(dòng)之后的時(shí)間

在Excel對象初始化前記錄啟動(dòng)之前的時(shí)間

//初始化
===========================================================================
begintime = System.DateTime .Now ;

Excel.ApplicationClass exc = null;

....
==========================================================================

操作完成后
=============================================================================

afterTime = System.DateTime .Now ; //啟動(dòng)之后的時(shí)間
//釋放資源
KillExcelProcess(begintime,afterTime); //強(qiáng)行結(jié)束Excel進(jìn)程
=============================================================================
KillExcelProcess()函數(shù)

public void KillExcelProcess(System.DateTime beginTime, System.DateTime afterTime)
{
try
{
System.Diagnostics.Process[] excelProcess = System.Diagnostics.Process.GetProcessesByName( "Excel ");

foreach(System.Diagnostics.Process process in excelProcess)
{
if (process != null)
{
if ( process.StartTime > beginTime && process.StartTime < afterTime )
{
process.Kill();
}
}
}
}
catch(System.Exception exp)
{
Response.Write ( " <script> alert( ' "+exp.Message + " '); </script> ");
}
}

 


// <summary>
/// 退出系統(tǒng)殺進(jìn)程
/// </summary>
private void KillProcess()
{
System.Diagnostics.Process CurrentProcess = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] AllProcess = System.Diagnostics.Process.GetProcesses();

foreach (System.Diagnostics.Process p in AllProcess)
{
if (p.Id == CurrentProcess.Id)
p.Kill();
}

}

 


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約