gpt4 book ai didi

c# - 在 Asp.net 中导入 Excel 需要在服务器上安装 Microsoft office 吗?

转载 作者:太空狗 更新时间:2023-10-29 22:27:41 25 4
gpt4 key购买 nike

我们是否需要在服务器上安装 Microsoft office 才能运行应用程序将数据从 excel 文件导入 mssql 数据库?

有什么建议或想法吗?

我使用的代码

public partial class _Default : System.Web.UI.Page
{
private String strConnection = "Data Source=MYCBJ017550027;Initial Catalog=MySamplesDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSend_Click(object sender, EventArgs e)
{
string path = fileuploadExcel.PostedFile.FileName;
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]",excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
sqlBulk.DestinationTableName = "Excel_table";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
}
}

最佳答案

如果您只读取 xls 文件,则使用 .net 框架内置的 Microsoft.Jet.OLEDB.4.0

如果您正在阅读 xlsx 文件,请使用 Microsoft.ACE.OLEDB.12.0。可以从 Microsoft 站点免费下载此驱动程序。您无需安装 Microsoft officer 即可进行互操作。

使用以下连接字符串

    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;  
Data Source=" + path + ";Extended Properties=Excel 12.0;HDR=YES";

Download drivers from here

Refer this for running example

关于c# - 在 Asp.net 中导入 Excel 需要在服务器上安装 Microsoft office 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10702788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com