使用Interop创建Excel文件时阻止Excel打开 [英] Prevent Excel from opening while creating excel file using interop

查看:128
本文介绍了使用Interop创建Excel文件时阻止Excel打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在使用Microsoft Office interop创建一个excel,并且成功创建了文件,但是问题是,当它创建文件时,只要打开excel就会将excel中的值添加到excel中,并以指定的名称保存它.当时输入结果会导致异常.Am从数据库中创建了近75个文件,其中有许多行,因此需要时间.在处理过程中无法执行任何任务,因为如果在excel中键入异常会创建异常.在后台运行该过程,以使excel应用程序不会在每次创建文件时都打开.

Hi all am creating a excel using Microsoft office interop.and it creates files successfully.But the problem is that when it creates a files it just opens excel adds the value in to excel and saves it in the specified name.Any accidental typing at that time results leads to a exception.Am creating nearly 75 files with many rows from database and hence takes time.During the processing am unable to do any task since it creates exception if its typed in the excel.Is there any way to run the process in background so that excel application does not open for each file creation.

Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
Excel.Range oRange;

// Start Excel and get Application object. 
oXL = new Excel.Application();

// Set some properties 
oXL.Visible = true;
oXL.DisplayAlerts = false;

// Get a new workbook. 
oWB = oXL.Workbooks.Add(Missing.Value);

// Get the active sheet 
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "Sales";

// Process the DataTable 
// BE SURE TO CHANGE THIS LINE TO USE *YOUR* DATATABLE 
DataTable dt = dtt;

int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
    rowCount += 1;
    for (int i = 1; i < dt.Columns.Count + 1; i++)
    {
        // Add the header the first time through 
        if (rowCount == 2)
        {
            oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
        }
        oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
    }
}

// Resize the columns 
//oRange = oSheet.get_Range(oSheet.Cells[1, 1],
//              oSheet.Cells[rowCount, dt.Columns.Count]);


oRange = oSheet.Range[oSheet.Cells[1, 1], oSheet.Cells[rowCount, dt.Columns.Count]];
oRange.EntireColumn.AutoFit();

// Save the sheet and close 
// oSheet = null;
oRange = null;

oWB.SaveAs("" + username + " .xls", Excel.XlFileFormat.xlWorkbookNormal,
    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Excel.XlSaveAsAccessMode.xlExclusive,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value);
oWB.Close(Missing.Value, Missing.Value, Missing.Value);
oWB = null;
oXL.Quit();

// Clean up 
// NOTE: When in release mode, this does the trick 
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

推荐答案

尝试一下...

//启动Excel并获取Application对象. oXL =新的Excel.Application { Visible = false };

// Start Excel and get Application object. oXL = new Excel.Application {Visible = false};

  • 或- //设置一些属性oXL.Visible = false ;
  • OR - // Set some properties oXL.Visible = false;

这篇关于使用Interop创建Excel文件时阻止Excel打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆