Visual Studio 如何配置“office版本=15.0.0”错误

bxjv4tth  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(246)

我正试图从SQL数据创建一个datagridview,使用户更改,然后单击一下用户将拥有datagridviews数据作为Excel,但无论尝试什么,总是得到那个错误:
“系统.IO.文件未找到异常:“未能加载文件或程序集”office,版本=15.0.0.0,区域性=中性,PublicKeyToken= 71e9bce111e9429c'。系统错误。“”
使用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();
        }
    }
}

我已经查过DLL了就在那个文件夹里
:C:\用户数据库\数据包\办公软件互操作性
我试着谷歌它,没有一个解决方案帮助我

c9qzyr3d

c9qzyr3d1#

我的测试环境是VS 2022 17.4.4 Winforms(.Net Framework 4.8)
在管理Nuget中添加相应的dll。
因为我也在使用office2016,所以我在这里添加了MSOffice.Interop 16.0.55555。

使用正确的dll,就可以完成输出了。

相关问题