我们是否需要在伺服器上安装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();
}
}发布于 2012-05-22 21:18:25
如果您只读取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
发布于 2012-05-22 21:22:06
正如@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-22 21:31:59
jet的问题是,你仍然需要为它安装数据提供程序(尽管这也是免费的),或者安装office才能工作。如果你只需要读取一个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);
}
}
}
}https://stackoverflow.com/questions/10702788
复制相似问题