简体   繁体   English

有没有使用C#将数组导出到Excel文件的快捷方法?

[英]is there quick way to export an array to Excel file using C#?

I have arrays of points that contain series data (x & y). 我有包含系列数据(x和y)的点数组。 Is there a quick way to output these arrays into an excel file? 有没有快速的方法将这些数组输出到excel文件?

Thanks 谢谢

Output the data to a file, separating the array elements with commas. 将数据输出到文件,用逗号分隔数组元素。 Then save the file as name.csv 然后将文件另存为name.csv

Use FileWriter to output the file. 使用FileWriter输出文件。

One of this nice things about range object is that you can assign a two dimensional array to directly to the value property. 关于范围对象的一个​​好处是,您可以将二维数组直接指定给value属性。 It is important that the range be the same number of cells as the array has elements. 该范围是相同数量的细胞作为阵列具有元件是很重要的。

        //using Excel = Microsoft.Office.Interop.Excel;
        String[,] myArr = new string[10, 10];
        for (int x = 0; x < 10; x++)
        {
            for (int y = 0; y < 10; y++)
            {
                myArr[x, y] = "Test " + y.ToString() + x.ToString();
            }
        }
        Excel.Application xlApp = new Excel.Application();
        xlApp.Visible = true;
        Excel.Workbook wb = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
        Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.get_Item(1);
        Excel.Range rng = ws.Cells.get_Resize(myArr.GetLength(0), myArr.GetLength(1));
        rng.Value2 = myArr;

If CSV is not satisfactory, you can use Microsoft.Office.Interop.Excel. 如果CSV不满意,您可以使用Microsoft.Office.Interop.Excel。 An example is at How to: Use COM Interop to Create an Excel Spreadsheet (C# Programming Guide) . 示例是如何:使用COM Interop创建Excel电子表格(C#编程指南)

I would use a third-party xsl export component. 我会使用第三方xsl导出组件。 This would save you the hassle of excel automation, and you wouldn't have to bundle the excel interop assemblies with your application. 这样可以省去excel自动化的麻烦,而且您不必将excel互操作程序集与应用程序捆绑在一起。

MyXls is a simple open-source component that does excel exports. MyXls是一个简单的开源组件,可以实现卓越的导出。 It should cover your needs just fine. 它应该可以满足您的需求。

You could do it using Ado.net. 你可以使用Ado.net做到这一点。 My code below assumes that there's an excel spreadsheet called Book1.xls in a folder C:\\Stuff\\ and that the spread sheet has the headers ID, Name, Site already present in a sheet called Sheet1. 我的代码假设在文件夹C:\\ Stuff \\中有一个名为Book1.xls的Excel电子表格,并且电子表格中有标题ID,名称,网站已存在于名为Sheet1的工作表中。

private void button1_Click(object sender, EventArgs e)
    {
        string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
                    Data Source=C:\Stuff\Book1.xls;Extended Properties=
                    ""Excel 8.0;HDR=YES;""";

        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {

            using (OleDbCommand command = conn.CreateCommand())
            {
                command.CommandText = @"INSERT INTO [Sheet1$] (ID, Name, Site) VALUES(1, ""Phil"", ""StackOverflow.com"")";
                conn.Open();
                command.ExecuteNonQuery();
            }
        }

    }

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

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