修改Interop Excel以打开现有文件 [英] Modify Interop Excel for Opening Existing File

查看:94
本文介绍了修改Interop Excel以打开现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Excel Interop for C#将datagridview导出到Excel并进行打印.我正在使用此代码:

I'm using Excel Interop for C# to export a datagridview to Excel and print it. I'm using this code:

try
{
    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();                                
    excel.Application.Workbooks.Add(true);

    int ColumnIndex = 0;
    int rowIndex = -1;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        rowIndex++;
        ColumnIndex = 0;

        foreach (DataGridViewColumn col in dataGridView1.Columns)
        {
            ColumnIndex++;
            excel.Cells[rowIndex + 1, ColumnIndex] = row.Cells[col.Name].Value;
        }
    }                

    excel.Visible = true;
    excel.DisplayAlerts = false;                
    Worksheet worksheet = (Worksheet)excel.ActiveSheet;
    worksheet.Activate();
    worksheet.Cells.Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
    worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, 7]].Merge();
    worksheet.Range[worksheet.Cells[2, 1], worksheet.Cells[2, 7]].Merge();
    worksheet.Range[worksheet.Cells[3, 1], worksheet.Cells[3, 7]].Merge();
    worksheet.Range[worksheet.Cells[4, 1], worksheet.Cells[4, 4]].Merge();      
    worksheet.Cells[1, 1].Font.Bold = true;
    worksheet.Range["A1"].Cells.Font.Size = 15;
    worksheet.Range["A4"].Cells.Font.Size = 15;
    worksheet.Range["B7"].Cells.Font.Size = 15;
    worksheet.Range["B8"].Cells.Font.Size = 15;
    worksheet.Range["B9"].Cells.Font.Size = 15;
    worksheet.Range["A11"].Cells.Font.Size = 15;
    worksheet.Range["B13"].Cells.Font.Size = 15;

    worksheet.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    GC.Collect();
    GC.WaitForPendingFinalizers();

    excel.Quit();
}

catch (Exception ex)
{
    MessageBox.Show("Error de exportación.");
}

它可以正常工作,但是我需要打开一个现有文件来执行相同的操作,以避免重新配置打印边距.我检查了其他类似的问题,并尝试应用"workBook = oXL.Workbooks.Open("path",...);但它可以使它工作.有什么想法吗?

It works fine, but I need to open an existing file to do this same thing to avoid reconfiguration of the printing margins. I checked other similar questions and tried to apply the "workBook = oXL.Workbooks.Open("path", ...);" but it can make it work. Any thoughts?

推荐答案

要使用open方法打开文件.

To open a file use the open method.

        Microsoft.Office.Interop.Excel.Workbooks wkbks = null;
        Microsoft.Office.Interop.Excel.Workbook wkbk = null;
        wkbks = excel.Workbooks;
        wkbk = wkbks.Open(xlsFileName);

在方法结束时,您应该对所有计算机执行清理您的互操作变量:

At the end of your method you should do a cleanup on all your interop variables:

            if (wkbk != null)
            {
                try
                {
                    wkbk.Close(false);
                }
                catch
                {
                }

                Marshal.FinalReleaseComObject(wkbk);
                wkbk = null;
            }

这篇关于修改Interop Excel以打开现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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