我们是否需要安装Microsoft Office服务器在Asp.net Excel导入? [英] Do we need to install Microsoft office in server for Excel import in Asp.net?

查看:322
本文介绍了我们是否需要安装Microsoft Office服务器在Asp.net Excel导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要安装Microsoft Office中的服务器来运行从Excel文件中的数据导入到MSSQL数据库的应用程序?

任何建议或想法?

在code,我用

 公共部分类_Default:System.Web.UI.Page
{
私人字符串strConnection =数据源= MYCBJ017550027;初始目录= MySamplesDB;集成安全性=真;
保护无效的Page_Load(对象发件人,EventArgs的)
{

}
保护无效btnSend_Click(对象发件人,EventArgs的)
{
字符串路径= fileuploadExcel.PostedFile.FileName;
字符串excelConnectionString = @供应商= Microsoft.ACE.OLEDB.12.0;数据源=+路径+;扩展属性= Excel的12.0;坚持安全信息=假;
OleDbConnection的excelConnection =新的OleDbConnection(excelConnectionString);
OleDbCommand的CMD =新的OleDbCommand(,excelConnection,[名],[指定]从[工作表Sheet1 $]选择[ID]);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy的sqlBulk =新SqlBulkCopy的(strConnection);
sqlBulk.DestinationTableName =Excel_table;
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
}
}
 

解决方案

如果你正在读只XLS文件,然后用 Microsoft.Jet.OLEDB.4.0 是内置有您的.NET框架。

如果你正在阅读的xlsx文件,然后用 Microsoft.ACE.OLEDB.12.0 。司机这个可以自由地从微软网站上下载。你并不需要安装微软官员interoping。

使用下面的连接字符串

 字符串excelConnectionString = @供应商= Microsoft.ACE.OLEDB.12.0;
数据源=+路径+;扩展属性= Excel的12.0; HDR = YES;
 

从这里下载驱动程序

<一个href="http://social.msdn.microsoft.com/Forums/en-US/vbinterop/thread/3e9e8441-36be-4f70-826b-51badb9a9bb3/"相对=nofollow>请参阅此运行例如

do we need to install Microsoft office in server to run a application to import data from excel file to mssql database ?

any suggestions or ideas ?

the code i used

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();
}
}

解决方案

If you are reading only xls files then use Microsoft.Jet.OLEDB.4.0 that is inbuilt with your .net framework.

If you are reading xlsx files then use Microsoft.ACE.OLEDB.12.0. The drivers for this can be download freely from Microsoft site. You don't need to install Microsoft officer for interoping.

Use following connection string

    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

这篇关于我们是否需要安装Microsoft Office服务器在Asp.net Excel导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆