1

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.");
}

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3063952
  • 97
  • 3
  • 11
  • What exactly happens when you use `Open`? Also, you'll find it much easier to troubleshoot your problems if you include `ex.ToString()` in your exception messages. – John Saunders Aug 15 '14 at 19:10

1 Answers1

2

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;
            }
Community
  • 1
  • 1
Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59