简体   繁体   English

C#:如何导出到Excel电子表格? 使用XSLT / LINQ /其他方法?

[英]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. 我需要能够导出从SQL Server 2008中的存储过程接收的一些数据。一旦返回数据,我需要能够输出它或将其导出到新的Excel电子表格。

What is the easiest way of doing this, Can LINQ do this? 这样做最简单的方法是什么,LINQ可以这样做吗? or am i forced to use XSLT? 还是我被迫使用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. 我假设我必须首先转换返回到XML的数据然后应用XSLT - 因为XSLT对XML文档起作用。

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? VS 2008中没有XSLT 2,所以我们仍然需要使用XSLT 1 - 但这真的是最佳选择吗?

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. 为了输出到csv或xml,你真的不需要任何不在xpath 1.0中的功能......我很少遇到需要更复杂的情况。

you could select into an xelement with linq ... however doing this in one statement would mean you cannot validate your data. 你可以用linq选择一个xelement ...但是在一个语句中这样做意味着你无法验证你的数据。 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. 作为csv使用更容易,并且比xml占用更少的空间......我认为xml被过度使用了。

An alternative (*and i dont recommend it) would be to query sql server from inside the excel document. 另一种选择(*和我不推荐它)是从excel文档内部查询sql server。 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 : 这个代码可以将一个对象数组(您可以轻松地将其填充数据)导出到excel spreadheat:

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. 它可以从SQL Server直接推送到Excel。

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. ADO.NET还有一个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. 如果要进行格式化并利用Excel的电子表格功能,可以使用Excel互操作,但这对于简单的数据传输来说可能太“混乱”了。

Also, as dtb points out, if it was a simple one-table data file, you could use CSV file. 另外,正如dtb所指出的,如果它是一个简单的单表数据文件,你可以使用CSV文件。 Although not native Excel ,it can be readily imported and is usually the easiest way of getting external data into Excel. 虽然不是原生Excel,但它可以很容易地导入,通常是将外部数据导入Excel的最简单方法。

If you need a .NET package for writing Excel files, try 如果您需要.NET包来编写Excel文件,请尝试

NExcelAPI NExcelAPI

for old Excel file format (<= 2003), or 对于旧的Excel文件格式(<= 2003),或

ExcelPackage ExcelPackage

for the newer Office Open XML format. 对于较新的Office Open XML格式。 For both libraries you don't need to have Excel installed. 对于这两个库,您不需要安装Excel。

EDIT: here is another one for the older (Excel 2002/2003) XML based file format 编辑:这是另一个旧的(Excel 2002/2003)基于XML的文件格式

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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