简体   繁体   中英

C#: How to export to an excel spreadsheet? using XSLT / LINQ / Other methods?

I need to be able to export some data that is received from a stored procedure in SQL Server 2008. Once the data is returned I need to be able to output it or export it to a new excel spreadsheet.

What is the easiest way of doing this, Can LINQ do this? or am i forced to use XSLT? I presume that i must first convert my data that is returned to XML and then apply XSLT - as XSLT works against XML documents.

XSLT 2 is not available in VS 2008 so we still have to use XSLT 1 - but is this really the way to go or best option?

I would think that it would be possible using an alternative method but maybe i am wrong.

I would really appreciate any advice, tutorials etc

Thanks

for outputting to csv or xml you really don't need any functionality that is not in xpath 1.0 ... its rare that i ran into a situation that required anything more complex.

you could select into an xelement with linq ... however doing this in one statement would mean you cannot validate your data. I usually end up iterating over a collection of elements to handle the edge cases.

HOwever out putting as csv is easier and takes less space than xml ... i think xml is overused tbh.

An alternative (*and i dont recommend it) would be to query sql server from inside the excel document. That was you can select your data directly into a spread sheet. This is fairly old and I don't much like it tbh.

this a code that export an array of object (you can easily fill it with your data) to an excel spreadsheat :

public static void SaveToExcel(object[,] data) 
{ 
    Excel = Microsoft.VisualBasic.Interaction.CreateObject("Excel.Application", String.Empty); 
    Excel.ScreenUpdating = false; 
    dynamic workbook = Excel.workbooks; 
    workbook.Add(); 

    dynamic worksheet = Excel.ActiveSheet; 

    const int left = 1; 
    const int top = 1; 
    int height = data.GetLength(0); 
    int width = data.GetLength(1); 
    int bottom = top + height - 1; 
    int right = left + width - 1; 

    if (height == 0 || width == 0) 
        return; 

    dynamic rg = worksheet.Range[worksheet.Cells[top, left], worksheet.Cells[bottom, right]]; 
    rg.Value = data; 

    // Set borders 
    for (var i = 1; i <= 4; i++) 
        rg.Borders[i].LineStyle = 1; 

    // Set header view 
    dynamic rgHeader = worksheet.Range[worksheet.Cells[top, left], worksheet.Cells[top, right]]; 
    rgHeader.Font.Bold = true; 
    rgHeader.Interior.Color = 189 * (int)Math.Pow(16, 4) + 129 * (int)Math.Pow(16, 2) + 78; 
    rg.EntireColumn.AutoFit(); 

    // Show excel app 
    Excel.ScreenUpdating = true; 
    Excel.Visible = true; 
} 

Its possible to push it straight out to Excel from SQL Server.

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
    'Excel 8.0;Database=D:\testing.xls;', 
    'SELECT * FROM [SheetName$]') select * from SQLServerTable

This and more examples are available from this source .

ADO.NET also has a driver for Excel. So if your data is naturally a database in "shape" then I'd probably use that.

You could use the Excel interop if you wanted to do formatting and to leverage Excel's spreadsheet capabilities, but this is probably too "messy" for simple data transfer.

Also, as dtb points out, if it was a simple one-table data file, you could use CSV file. Although not native Excel ,it can be readily imported and is usually the easiest way of getting external data into Excel.

If you need a .NET package for writing Excel files, try

NExcelAPI

for old Excel file format (<= 2003), or

ExcelPackage

for the newer Office Open XML format. For both libraries you don't need to have Excel installed.

EDIT: here is another one for the older (Excel 2002/2003) XML based file format

http://www.carlosag.net/Tools/ExcelXmlWriter/

如果你花了几块钱,我在过去的几年里使用过xPort Tools并对它很满意。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM