简体   繁体   English

怎么配置出现“office version=15.0.0”报错

[英]How can ı configure "office version=15.0.0" error

I'm trying to create a datagridview from sql data, make the user changes and after that with one click user will have the dataridviews data as an excel but no matter what ı tried ı keep getting that error:我正在尝试从 sql 数据创建一个 datagridview,让用户进行更改,然后单击一下,用户就会将 dataridviews 数据作为 excel,但无论我尝试了什么,我都会不断收到该错误:

"System.IO.FileNotFoundException: 'Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. Sistem belirtilen dosyayı bulamıyor.'" “System.IO.FileNotFoundException:‘无法加载文件或程序集‘office,Version=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c’。Sistem belirtilen dosyayı bulamıyor。’”

btw ım using office 2016 pro and visual studio 2022 my codeblock is here:顺便说一句,我使用 Office 2016 Pro 和 Visual Studio 2022 我的代码块在这里:

# using System.Data;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Windows.Forms;


namespace DönüştürülecekApp
{
    public partial class Pro : Form
    {
        public Pro()
        {
            InitializeComponent();
        }

        public void Pro_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = true;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.EnableHeadersVisualStyles = false;

            var select = "Select ........";
            var c = new SqlConnection("Server= ..........."); 
            var dataAdapter = new SqlDataAdapter(select, c);

            var commandBuilder = new SqlCommandBuilder(dataAdapter);
            var ds = new DataSet();
            dataAdapter.Fill(ds);
            malzemeBindingSource.DataSource = ds.Tables[0];
        }


        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }
        private void ExportToExcel()
        {
            string connectionString = "Server= ......";
            string query = "Select ......";

            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand(query, connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            System.Data.DataTable dataTable = new System.Data.DataTable();
            adapter.Fill(dataTable);

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Workbook workbook = excel.Workbooks.Add(Type.Missing);
            Worksheet sheet = (Worksheet)workbook.ActiveSheet;

            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                sheet.Cells[1, i + 1] = dataTable.Columns[i].ColumnName;
            }

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                for (int j = 0; j < dataTable.Columns.Count; j++)
                {
                    sheet.Cells[i + 2, j + 1] = dataTable.Rows[i][j].ToString();
                }
            }

            workbook.SaveAs("YourFileName.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            excel.Quit();
        }
    }
}

I already checked dlls and it's in that folder我已经检查过 dll,它在那个文件夹中

:C:\Users.....nuget\packages\microsoft.office.interop.excel\15.0.4795.1001\lib.net20\Microsoft.Office.Interop.Excel.dll :C:\Users.....nuget\packages\microsoft.office.interop.excel\15.0.4795.1001\lib.net20\Microsoft.Office.Interop.Excel.dll

ı tried to google it and none of the solutions helped me我试着用谷歌搜索它,但没有一个解决方案对我有帮助

My test environment is VS 2022 17.4.4 Winforms(.Net Framework 4.8)我的测试环境是VS 2022 17.4.4 Winforms(.Net Framework 4.8)

Add the corresponding dll in Manage Nuget.在管理Nuget中添加对应的dll。

Because I am also using office2016, here I added MSOffice.Interop 16.0.55555.因为我也是用的office2016,所以这里我添加了MSOffice.Interop 16.0.55555。

在此处输入图像描述

With the right dll, you're done outputting.有了正确的dll,你就完成了输出。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用已安装的Office版本在WinForms应用程序中查看Microsoft Office文档? - How can I view Microsoft Office documents in a WinForms application using the installed version of Office? 如何确定客户端计算机上 Microsoft.Office.Interop.Excel 的版本? - How can I determine the version of Microsoft.Office.Interop.Excel on a client computer? 如何检查机器上安装的MS Office版本 - how to check MS office version installed on the machines 如何检测安装的 MS-Office 版本? - How to detect installed version of MS-Office? 如何配置swashbuckle以显示api版本而不是v {version}变量? - how can I configure swashbuckle to display the api version instead of the v{version} variable? Office插件版本不正确 - Incorrect version of office plugin 如何确定Office版本号的JavaScript API(适用于Office 2013项目的应用) - How do I determine JavaScript API for Office Version Number (App for Office 2013 Project) 如何在vs2010中为64位版本的办公室开发Microsoft office word addin - How to develop Microsoft office word addin for 64bit version office in vs2010 如何定位广泛版本的Microsoft Office互操作程序集? - How do I target a broad version of the Microsoft Office interop assemblies? 如何卸载我的Office加载项? - How can I uninstall my office add in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM