Home > Software design >  Exporting DataGridView to Excel file but i get System.IO.FileNotFoundException
Exporting DataGridView to Excel file but i get System.IO.FileNotFoundException

Time:06-02

I'm trying to export my datagridview to excel file but with every code I try it gives me this exception:

Exception thrown: 'System.IO.FileNotFoundException' in System.Windows.Forms.dll An unhandled exception of type 'System.IO.FileNotFoundException' occurred in System.Windows.Forms.dll Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.

Code down below:

if (dataGridView2.Rows.Count > 0)
{
    Microsoft.Office.Interop.Excel.Application xcelApp = new Microsoft.Office.Interop.Excel.Application();
    xcelApp.Application.Workbooks.Add(Type.Missing);

    for (int i = 1; i < dataGridView2.Columns.Count   1; i  )
    {
        xcelApp.Cells[1, i] = dataGridView2.Columns[i - 1].HeaderText;
    }

    for (int i = 0; i < dataGridView2.Rows.Count; i  )
    {
        for (int j = 0; j < dataGridView2.Columns.Count; j  )
        {
            xcelApp.Cells[i   2, j   1] = dataGridView2.Rows[i].Cells[j].Value.ToString();
        }
    }
    xcelApp.Columns.AutoFit();
    xcelApp.Visible = true;

CodePudding user response:

Here is a sample code to generate excel file with Microsoft.Office.Interop Any question ask in comment

using System;
using Excel = Microsoft.Office.Interop.Excel;
using PathIO = System.IO.Path;
using System.Drawing;
using System.Runtime.InteropServices;

namespace ConsoleApp6
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var missing = Type.Missing;

        Excel._Application excel = new Excel.Application();
        Excel.Workbooks workBooks = excel.Workbooks;
        Excel._Workbook workBook = workBooks.Add(missing);
        Excel._Worksheet workSheet = null;
        Excel.Range chartRange = null;
        Excel.Font font = null;
        Excel.Interior interior = null;
        Excel.Range cells = null;

        int excelRowIndex = 1;
        int excelColumnIndex = 1;

        int percentStartRowIndex;

        string startCell = string.Empty;
        string endCell = string.Empty;

        string startCellDesiFormat = string.Empty;
        string endCellDesiFormat = string.Empty;

        int charInt = 65;
        int charIntCounter = 0;

        workSheet = (Excel._Worksheet)workBook.ActiveSheet;
        workSheet.Name = "S and SE Summary";

        //Report Name
        chartRange = workSheet.get_Range("a"   excelRowIndex, "ai"   excelRowIndex);
        chartRange.Merge(false);
        chartRange.RowHeight = 18;
        font = chartRange.Font;
        font.Bold = FontStyle.Bold;
        font.Size = 13;
        font.Name = "Calibri";
        chartRange.Value = "MyReport";
        chartRange.HorizontalAlignment = 1;
        chartRange.VerticalAlignment = 3;
        chartRange.BorderAround2();
        interior = chartRange.Interior;
        interior.Color = Color.FromArgb(228, 223, 236);

        _ = Marshal.FinalReleaseComObject(font);
        _ = Marshal.FinalReleaseComObject(interior);
        _ = Marshal.FinalReleaseComObject(chartRange);

        excelRowIndex  = 1;

        //Start Date Heading
        chartRange = workSheet.get_Range("a"   excelRowIndex, "b"   excelRowIndex);
        chartRange.Merge(false);
        font = chartRange.Font;
        font.Bold = FontStyle.Bold;
        font.Size = 10;
        font.Name = "Calibri";
        chartRange.Value = "Start Date: ";
        chartRange.HorizontalAlignment = 1;
        chartRange.VerticalAlignment = 3;

        _ = Marshal.FinalReleaseComObject(font);
        _ = Marshal.FinalReleaseComObject(chartRange);

        //Start Date 
        chartRange = workSheet.get_Range("c"   excelRowIndex, "c"   excelRowIndex);
        font = chartRange.Font;
        font.Size = 10;
        font.Name = "Calibri";
        chartRange.NumberFormat = "@";
        chartRange.Value = DateTime.Now.ToString("dd-MM-yyyy");
        chartRange.HorizontalAlignment = 2;
        chartRange.VerticalAlignment = 3;

        _ = Marshal.FinalReleaseComObject(font);
        _ = Marshal.FinalReleaseComObject(chartRange);

        //File will be saved in documents folder

        var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        var path = PathIO.Combine(docPath, DateTime.Now.ToString("dd-MM-yyyyHH-mm-ss-ffff")   ".xlsx");

        workBook.SaveAs(path);
        workBook.Close(false, missing, missing);
        workBooks.Close();
        excel.Application.Quit();
        excel.Quit();

        if (interior != null) _ = Marshal.FinalReleaseComObject(interior);
        if (font != null) _ = Marshal.FinalReleaseComObject(font);
        if (cells != null) _ = Marshal.FinalReleaseComObject(cells);
        if (chartRange != null) _ = Marshal.FinalReleaseComObject(chartRange);
        if (workSheet != null) _ = Marshal.FinalReleaseComObject(workSheet);
        if (workBook != null) _ = Marshal.FinalReleaseComObject(workBook);
        if (workBooks != null) _ = Marshal.FinalReleaseComObject(workBooks);
        if (excel != null) _ = Marshal.FinalReleaseComObject(excel);
        }
    }
}

CodePudding user response:

You need to install the corresponding version of Office. 15.0.0.0 should correspond to Office 2013. Have a look at this post.

  • Related