Do we need to install Microsoft office in server f

2019-01-23 20:18发布

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

标签: c# .net import
3条回答
劳资没心,怎么记你
2楼-- · 2019-01-23 20:49

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

查看更多
虎瘦雄心在
3楼-- · 2019-01-23 20:53

As @Romil said you can use the .NET framework for that:

string ConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=""Excel 8.0;HDR=Yes"";", fileName);

using (OleDbConnection conn = new OleDbConnection(ConnectionString))
{
     conn.Open();
     DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

     foreach (DataRow schemaRow in schemaTable.Rows)
     {
          string sheet = schemaRow["TABLE_NAME"].ToString();
          using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn))
          {
               cmd.CommandType = CommandType.Text;
               DataTable outputTable = new DataTable(sheet);
               output.Tables.Add(outputTable);
               new OleDbDataAdapter(cmd).Fill(outputTable);
          }
     }
                conn.Close();
}
查看更多
小情绪 Triste *
4楼-- · 2019-01-23 20:58

The problem with jet, is you still have to install the data provider for it (though this is also free) or have office installed for it to work. If you simply need to read an excel file there are plenty of fully managed libs out there that will do the track just fine without the need to install anything.

I've listed a few here. I have used excelreader plenty with good results.

http://excelreader.codeplex.com/
http://epplus.codeplex.com/

Excel reader seem to be light on the documentation side of things. so here is an example of how to use it

IExcelDataReader reader = null;
try
{
    using (FileStream stream = new FileStream(ofd.FileName, FileMode.Open))
    {
        string ext = System.IO.Path.GetExtension(ofd.FileName).Replace(".", "").ToUpper();
        if (ext == "XLS")
            reader = ExcelReaderFactory.CreateBinaryReader(stream);
        else
            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        reader.IsFirstRowAsColumnNames = true;
        using (DataSet ds = reader.AsDataSet())
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ImportData toAdd = new ImportData()
                    {
                        Format = dr[0].ToString(),
                    };

                Database.Datastore.InsertObject(toAdd);
            }
        }
    }
}
查看更多
登录 后发表回答