[英]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.