How to store data to Excel from DataSet without go

2019-08-20 11:17发布

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.

4条回答
Rolldiameter
2楼-- · 2019-08-20 11:49

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...

查看更多
闹够了就滚
3楼-- · 2019-08-20 11:50

Why not use ADO.NET using the OLEDB provider?

See: Tips for reading Excel spreadsheets using ADO.NET

查看更多
该账号已被封号
4楼-- · 2019-08-20 11:52

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).

查看更多
混吃等死
5楼-- · 2019-08-20 12:02

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.

查看更多
登录 后发表回答