Server read Excel does not install Office how to implement _ Practical tips

Source: Internet
Author: User
Tags microsoft sql server ole
Some time ago with ASP.net do a simple game management background, which involves uploading Excel import data function, originally in the local development is good, can have uploaded the server on the tragedy. Server is Aliyun Windows Server 2008 R2 (X64), after troubleshooting the server does not install Office this thing, there will be no OLE DB driver, do not want to install Office, especially Office 2010, looking at the size of the headache, how to do?

Baidu Google found that in fact, as long as the installation of Microsoft Access 2010 Database Engine Redistributable Package to implement the Microsoft Office System file and non-Microsoft Office applications transfer data between, support the existing Microsoft Office files (such as Microsoft Office Access *.mdb and *.accdb) files and Microsoft Office Excel (*.xls, *.xlsx, and *.XLSB) files) to transfer data between other data sources, such as Microsoft SQL Server. also supports establishing connections to existing text files. In addition, ODBC and OLE DB drivers are installed for application developers to use when developing applications that are connected to Office file formats. The installation package download address: http://www.microsoft.com/zh-cn/download/details.aspx?id=13255.

After loading the package, a piece of code is done:
Copy Code code as follows:

<summary>
Connect Excel to read Excel data and return to DataSet data collection
</summary>
<param name= "filepath" >excel server path </param>
<param name= "tablename" >excel table name </param>
<returns></returns>
public static System.Data.DataSet Excelsqlconnection (string filepath, string tablename)
{
String Strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + filepath + "; Extended properties= ' Excel 8.0; Hdr=yes;imex=1 ' ";
String Strcon = "Provider=microsoft.ace.oledb.12.0;data source=" + filepath + "; Extended properties= ' Excel 12.0; Hdr=yes;imex=1 ' ";
OleDbConnection excelconn = new OleDbConnection (Strcon);
Try
{
String strcom = String. Format ("select * from [sheet1$]");
Excelconn.open ();
OleDbDataAdapter mycommand = new OleDbDataAdapter (strcom, excelconn);
DataSet ds = new DataSet ();
Mycommand.fill (ds, "[" + tablename + "$]");
Excelconn.close ();
return DS;
}
Catch
{
Excelconn.close ();
return null;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.