有没有使用 C# 将数组导出到 Excel 文件的快速方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1025985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is there quick way to export an array to Excel file using C#?
提问by mustafabar
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
谢谢
采纳答案by Sev
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 输出文件。
回答by Matthew Flaschen
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# 编程指南)。
回答by Rune Grimstad
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 互操作程序集与您的应用程序捆绑在一起。
MyXlsis a simple open-source component that does excel exports. It should cover your needs just fine.
MyXls是一个简单的开源组件,可以导出 excel。它应该可以很好地满足您的需求。
回答by Phil
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 来完成。下面的代码假定文件夹 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();
}
}
}
回答by Oorang
One of this nice things about range object is that you can assign a two dimensional array to directly to the value property. It isimportant 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;