我使用Devexpress Exporter创建Excel工作表,然后将文件保存在特定位置。即使在调用退出后,Excel进程仍然保持活着
创建文件后,我打开它,添加项目的下拉列表,然后再次将其保存在同一位置。
完成所有操作后,文件必须自动通过电子邮件发送到数据库的电子邮件地址。
现在,如果我有1000个电子邮件地址,并自动完成这一过程,它正在创建的Excel 10多个实例。
我怎样才能阻止这些实例的创建,我怎么可以使用Excel的操作,而无需使用更多的内存。
守则如下:
protected string CreateExcelFile(string FilterName)
{
Random ranNumber = new Random();
int number = ranNumber.Next(0, 10000000);
string FileName = "TestDoc"+DateTime.Now.Year.ToString()+number.ToString()+DateTime.Now.Second.ToString()+".xls";
string path = @"c:\TestDocuments\"+FileName;
Directory.CreateDirectory(Path.GetDirectoryName(path));
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
XlsExportOptions options = new XlsExportOptions();
options.ExportHyperlinks = false;
ASPxExporter.WriteXls(fs, options);
fs.Close();
AddDropDownToExcel(path);
return path;
}
//Adding The Dropdownlist Of Items TO Generated Excel Sheet
protected void AddDropDownToExcel(string path)
{
Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
string fileName = path.Replace("\\", "\\\\");
string RowCount = "F" + (testgrid.VisibleRowCount + 1).ToString();
// Open Excel and get first worksheet.
var workbook = application.Workbooks.Open(fileName);
var worksheet = workbook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
// Set range for dropdownlist
var rangeNewStatus = worksheet.get_Range("F2", RowCount);
rangeNewStatus.ColumnWidth = 20;
rangeNewStatus.Validation.Add(Microsoft.Office.Interop.Excel.XlDVType.xlValidateList, Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlValidAlertStop,
Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlBetween, "Item1,Item2,Item3,Item4");
// Save.
workbook.Save();
workbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
application.Quit();
}
我在VBA中使用下面的代码来附加到Excel的现有实例或创建一个新的。也许你头在正确的方向: 公用Sub InitializeExcel(aNewInstance正如布尔) 如果aNewInstance = false,那么 上的错误继续下一步 设置xlApp = GetObject的(“Excel.Application”) 结束如果 对错误转到0 如果xlApp不是那么 设置xlApp = CreateObject(“Excel。应用程序“) End If End Sub – 2012-11-16 18:08:41