C# OLEDB连接到 Excel
我正在将 Excel 工作表复制到数据表中,如下所示:
OleDbCommand command = new OleDbCommand();
command = new OleDbCommand("Select * from [working sheet$]", oleDBConnection);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = command;
dataAdapter.Fill(dt);
是否有类似的方法可以将数据表简单地复制回 Excel 工作表?我不断发现的例子是逐个单元地复制,但是对于大型数据集来说,这可能会明显很慢。
谢谢
I am copying an Excel sheet into a Datatable as such:
OleDbCommand command = new OleDbCommand();
command = new OleDbCommand("Select * from [working sheet$]", oleDBConnection);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = command;
dataAdapter.Fill(dt);
Is there a similar method where I can just simply copy the datatable back to an Excel sheet? The examples I keep finding are copying cell by cell, but this can be noticably slow with large data sets.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找
DataAdapter。 Update
方法,它将 DataTable 中所做的任何更改应用到数据库(或电子表格,在本例中)You're looking for the
DataAdapter.Update
method, which applies any changes made in the DataTable to the database (or spreadsheet, in this case)对 Excel 中的 OleDB 不太了解,但既然您提到了数据库,我假设它在服务器上运行? Microsoft 实际上不建议在服务器上运行 Excel。
我会使用 OpenXML 来完成这样的任务。稍微复杂一点,但是保存稳定。
Don't really know much about OleDB with Excel, but since you mentioned a Database, I assume this runs on a server? Microsoft actually does not recomment running Excel on a server.
I would use OpenXML for tasks like this. It's a bit more complicated, but it's save and stable.