是否有使用c#将数组导出到Excel文件的快速方法?

时间:2023-01-29 09:56:46

I have arrays of points that contain series data (x & y). Is there a quick way to output these arrays into an excel file?

我有一系列包含序列数据的点(x & y)。是否有一种快速的方法将这些数组输出到excel文件中?

Thanks

谢谢

5 个解决方案

#1


9  

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来输出文件。

#2


8  

One of this nice things about range object is that you can assign a two dimensional array to directly to the value property. It is important that the range be the same number of cells as the array has elements.

range对象的好处之一是可以将二维数组直接赋给value属性。重要的是,范围要与数组中元素的数量相同。

        //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;

#3


4  

If CSV is not satisfactory, you can use Microsoft.Office.Interop.Excel. An example is at How to: Use COM Interop to Create an Excel Spreadsheet (C# Programming Guide).

如果CSV不满意,可以使用Microsoft.Office.Interop.Excel。一个例子是如何:使用COM Interop创建一个Excel电子表格(c#编程指南)。

#4


2  

I would use a third-party xsl export component. This would save you the hassle of excel automation, and you wouldn't have to bundle the excel interop assemblies with your application.

我将使用第三方xsl导出组件。这样可以省去excel自动化的麻烦,也不必将excel互操作程序集与应用程序捆绑在一起。

MyXls is a simple open-source component that does excel exports. It should cover your needs just fine.

MyXls是一个简单的开源组件,它可以实现excel的导出。它应该能满足你的需要。

#5


2  

You could do it using 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.

您可以使用Ado.net完成。下面的代码假设有一个名为Book1的excel电子表格。文件夹C中的xls:\东西,而扩展表有一个名为Sheet1的页眉ID、名称和站点。

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"", ""*.com"")";
                conn.Open();
                command.ExecuteNonQuery();
            }
        }

    }

#1


9  

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来输出文件。

#2


8  

One of this nice things about range object is that you can assign a two dimensional array to directly to the value property. It is important that the range be the same number of cells as the array has elements.

range对象的好处之一是可以将二维数组直接赋给value属性。重要的是,范围要与数组中元素的数量相同。

        //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;

#3


4  

If CSV is not satisfactory, you can use Microsoft.Office.Interop.Excel. An example is at How to: Use COM Interop to Create an Excel Spreadsheet (C# Programming Guide).

如果CSV不满意,可以使用Microsoft.Office.Interop.Excel。一个例子是如何:使用COM Interop创建一个Excel电子表格(c#编程指南)。

#4


2  

I would use a third-party xsl export component. This would save you the hassle of excel automation, and you wouldn't have to bundle the excel interop assemblies with your application.

我将使用第三方xsl导出组件。这样可以省去excel自动化的麻烦,也不必将excel互操作程序集与应用程序捆绑在一起。

MyXls is a simple open-source component that does excel exports. It should cover your needs just fine.

MyXls是一个简单的开源组件,它可以实现excel的导出。它应该能满足你的需要。

#5


2  

You could do it using 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.

您可以使用Ado.net完成。下面的代码假设有一个名为Book1的excel电子表格。文件夹C中的xls:\东西,而扩展表有一个名为Sheet1的页眉ID、名称和站点。

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"", ""*.com"")";
                conn.Open();
                command.ExecuteNonQuery();
            }
        }

    }