C# 数组导入Excel中
时间: 2023-09-20 10:11:47 浏览: 56
你可以使用 C# 中的 Microsoft.Office.Interop.Excel 库来导入数组到 Excel 中。以下是一个简单的示例代码:
```csharp
using Excel = Microsoft.Office.Interop.Excel;
// 创建一个 Excel 应用程序对象
Excel.Application excel = new Excel.Application();
// 创建一个新的工作簿
Excel.Workbook workbook = excel.Workbooks.Add();
// 获取工作簿中的第一个工作表
Excel.Worksheet worksheet = workbook.ActiveSheet;
// 定义一个数组
int[] myArray = { 1, 2, 3, 4, 5 };
// 将数组中的元素逐一写入 Excel 中的单元格
for (int i = 0; i < myArray.Length; i++)
{
worksheet.Cells[i+1, 1] = myArray[i];
}
// 保存工作簿
workbook.SaveAs("myArray.xlsx");
// 关闭 Excel 应用程序对象
excel.Quit();
```
这个示例代码将一个整数数组写入 Excel 中的第一列,并将工作簿保存为 "myArray.xlsx" 文件。你可以根据需要修改代码来适应你的具体要求。
相关问题
C# 窗体程序中将数组数据导入Excel中
你可以使用 C# 中的 Microsoft.Office.Interop.Excel 库来将窗体程序中的数组数据导入到 Excel 中。以下是一个简单的示例代码:
```csharp
using Excel = Microsoft.Office.Interop.Excel;
// 创建一个 Excel 应用程序对象
Excel.Application excel = new Excel.Application();
// 创建一个新的工作簿
Excel.Workbook workbook = excel.Workbooks.Add();
// 获取工作簿中的第一个工作表
Excel.Worksheet worksheet = workbook.ActiveSheet;
// 获取窗体程序中的数组数据
int[] myArray = { 1, 2, 3, 4, 5 };
// 将数组中的元素逐一写入 Excel 中的单元格
for (int i = 0; i < myArray.Length; i++)
{
worksheet.Cells[i+1, 1] = myArray[i];
}
// 保存工作簿
workbook.SaveAs("myArray.xlsx");
// 关闭 Excel 应用程序对象
excel.Quit();
```
这个示例代码将一个整数数组写入 Excel 中的第一列,并将工作簿保存为 "myArray.xlsx" 文件。你可以根据需要修改代码来适应你的具体要求。如果你想在窗体程序中显示 Excel 文件,可以使用 Excel 库的另外一些功能来实现。
用c# 导入excel 但是导入字段是服务器中数据来的
您可以按照以下步骤使用C#从服务器中获取数据并将其导入到Excel中:
1. 使用您选择的编程语言(例如C#)编写与服务器的连接代码。您可以使用ADO.NET等数据访问技术来建立到服务器的连接并检索数据。
2. 对检索到的数据进行处理。您可以将数据存储在一个数据结构(例如数组或列表)中,并对其进行排序、筛选和转换等操作。
3. 创建一个新的Excel文件或打开一个现有的Excel文件。
4. 将处理后的数据导入到Excel文件中。您可以使用第三方库(例如EPPlus)或使用.NET框架提供的COM对象(例如Microsoft.Office.Interop.Excel)来编写代码将数据插入到Excel单元格中。
以下是一个示例代码,演示了如何从SQL Server中检索数据并将其导入到Excel中:
```csharp
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Connect to SQL Server
SqlConnection conn = new SqlConnection("Data Source=server;Initial Catalog=database;Integrated Security=True");
conn.Open();
// Retrieve data from the server
SqlCommand cmd = new SqlCommand("SELECT * FROM table", conn);
SqlDataReader reader = cmd.ExecuteReader();
// Create a new Excel workbook and worksheet
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook = excel.Workbooks.Add();
Worksheet worksheet = workbook.ActiveSheet;
// Write the column headers to the first row of the worksheet
int col = 1;
foreach (var column in reader.GetSchemaTable().Columns)
{
worksheet.Cells[1, col] = column.ToString();
col++;
}
// Write the data to the worksheet
int row = 2;
while (reader.Read())
{
col = 1;
foreach (var field in reader)
{
worksheet.Cells[row, col] = field.ToString();
col++;
}
row++;
}
// Save the workbook
workbook.SaveAs("C:\\data.xlsx");
// Clean up
workbook.Close();
excel.Quit();
conn.Close();
}
}
}
```
请注意,此示例代码仅用于演示目的。在实际应用程序中,您应该添加错误处理和异常处理代码以确保代码的稳定性和可靠性。