我有幾個測試運行三次,並有平均通過C#代碼計算。我能夠寫入三個測試時間,並有平均的xls文件,如果創建之前在下面的圖片格式。但是現在我必須每天通過使用Windows調度程序的批處理文件運行每個測試。我想在下面提到的格式中每小時動態地創建xls文件,並使用特定名稱,以便在第一次迭代時創建文件,並且對於接下來的19次迭代,它應該在同一個文件中寫入,然後在下一小時內使用特定名稱。如何動態創建和寫入excel文件? 如果還有其他簡單的程序請參考。我是用寫在已創建的XLS文件的代碼是:`/ *如何創建和使用C#編寫Excel .xls文件
using System;
using System.IO;
using Ranorex;
namespace PEPI_Performance.Utility
{
/// <summary>
/// Description of ExcelWriter.
/// </summary>
public class ExcelWriter
{
/// <summary>
/// Constructs a new instance.
/// </summary>
public ExcelWriter()
{
// Do not delete - a parameterless constructor is required!
}
public void Driver(int row , int col, string time, string sheetName){
string sDataFile = "Ranorex_Reports.xls";
string sFilePath = Path.GetFullPath(sDataFile);
string sOldvalue = "Automation\\bin\\Debug\\" + sDataFile;
sFilePath = sFilePath.Replace(sOldvalue,"")+
"PEPI_Performance\\ExecutionReport\\" + sDataFile;
fnOpenExcel(sFilePath,sheetName);
writeExcel(row,col,time);
fnCloseExcel();
}
Excel.Application exlApp ;
Excel.Workbook exlWB ;
Excel.Sheets excelSheets ;
Excel.Worksheet exlWS;
//Open Excel file
public int fnOpenExcel(string sPath, string iSheet){
int functionReturnValue = 0;
try {
exlApp = new Excel.ApplicationClass();
exlApp.Visible = true;
exlWB=
exlApp.Workbooks.Open(sPath,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
// get all sheets in workbook
excelSheets = exlWB.Worksheets;
// get some sheet
//string currentSheet = "Cycle1";
exlWS = (Excel.Worksheet)excelSheets.get_Item(iSheet);
functionReturnValue = 0;
}
catch (Exception ex) {
functionReturnValue = -1;
Report.Error(ex.Message);
}
return functionReturnValue;
}
// Close the excel file and release objects.
public int fnCloseExcel(){
//exlWB.Close();
try{
exlApp.ActiveWorkbook.Save();
exlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(exlWS);
System.Runtime.InteropServices.Marshal.ReleaseComObject(exlWB);
System.Runtime.InteropServices.Marshal.ReleaseComObject(exlApp);
GC.GetTotalMemory(false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.GetTotalMemory(true);
}catch(Exception ex){
Report.Error(ex.Message);
}
return 0;
}
public void writeExcel(int i, int j , string time){
Excel.Range exlRange = null;
exlRange = (Excel.Range)exlWS.UsedRange;
((Excel.Range)exlRange.Cells[i,j]).Formula = time;
}
}
}
`
是你的問題如何生成一個文件名與日期和時間在裏面? – CodeCaster 2014-08-28 11:23:34
如何創建一個excel文件,該excel文件名可以與日期和時間。在裏面我想寫作附圖。 @CodeCaster – Mudit 2014-08-28 11:29:22
它們是什麼樣的單元測試?視覺工作室? NUnit的? – 2014-08-28 11:37:49