How to store data to Excel from DataSet without going cell-by-cell?

大憨熊 提交于 2019-12-25 04:37:12

问题


Duplicate of: What’s the simplest way to import a System.Data.DataSet into Excel?

Using c# under VS2008, we can create an excel app, workbook, and then worksheet fine by doing this:

Application excelApp = new Application();
Workbook excelWb = excelApp.Workbooks.Add(template);
Worksheet excelWs = (Worksheet)this.Application.ActiveSheet;

Then we can access each cell by "excelWs.Cells[i,j]" and write/save without problems. However with large numbers of rows/columns, we are expecting a loss in efficiency.

Is there a way to "data bind" from a DataSet object into the worksheet without using the cell-by-cell approach? Most of the methods we have seen at some point revert to the cell-by-cell approach. Thanks for any suggestions.


回答1:


Why not use ADO.NET using the OLEDB provider?

See: Tips for reading Excel spreadsheets using ADO.NET




回答2:


This is a duplicate question.

See: What's the simplest way to import a DataSet into Excel

(The answer is, use the OleDbConnection and read the dataset from Excel).




回答3:


Instead of going cell by cell, it is faster to set a 2-dimensional array of objects to an excel range.

object[][] values = ...
ws.Range("A1:F2000").Value = values;

However, the OleDb way is still much faster than above.




回答4:


You could also render a GridView to a string and save the results to the client as HTML or XLS. If you use the XLS file extension to associate the data with Excel, you'll see an error when you open the file in Excel. It doesn't hurt anything and your HTML data will open and look perfect in Excel...



来源:https://stackoverflow.com/questions/918102/how-to-store-data-to-excel-from-dataset-without-going-cell-by-cell

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