5

我们是否需要在服务器中安装 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();
}
}
4

3 回答 3

8

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

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

使用以下连接字符串

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

从这里下载驱动程序

请参阅this以获取运行示例

于 2012-05-22T13:18:25.023 回答
2

正如@Romil 所说,您可以为此使用 .NET 框架:

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();
}
于 2012-05-22T13:22:06.407 回答
0

jet 的问题是,您仍然必须为其安装数据提供程序(尽管这也是免费的)或安装了办公室才能使其工作。如果您只需要读取一个 excel 文件,那么有很多完全托管的库可以很好地完成跟踪,而无需安装任何东西。

我在这里列出了一些。我用过很多excelreader,效果很好。

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

Excel 阅读器在文档方面似乎很轻松。所以这是一个如何使用它的例子

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);
            }
        }
    }
}
于 2012-05-22T13:31:59.457 回答