C# OLEDBConnection to Excel

我的未来我决定 提交于 2019-12-25 01:44:50

问题


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


回答1:


You're looking for the DataAdapter.Update method, which applies any changes made in the DataTable to the database (or spreadsheet, in this case)




回答2:


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.



来源:https://stackoverflow.com/questions/5648308/c-sharp-oledbconnection-to-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!