我有一组包含系列数据(x和y)的点数组。是否有快速的方法将这些数组输出到Excel文件中?
谢谢。
谢谢。
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;
如果CSV不够满意,你可以使用Microsoft.Office.Interop.Excel。一个例子在如何:使用COM互操作创建Excel电子表格(C#编程指南)。
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();
}
}
}