在使用 Microsoft.Office.Interop.Excel
进行操作之前,请确保已经安装了 Microsoft Office(包括 Excel)并具备相应的许可或授权。此外,还需要添加对 Microsoft.Office.Interop.Excel
程序集的引用。
可以按照以下步骤进行设置:
- 在 Visual Studio 中打开你的项目。
- 在“解决方案资源管理器”窗口中,右键单击项目,然后选择“添加” > “引用”。
- 在“引用管理器”对话框中,选择“浏览”选项卡。
- 导航到 Microsoft Office 安装目录下的文件夹(比如
C:\Program Files\Microsoft Office\root\Office16
),找到并选择Microsoft.Office.Interop.Excel.dll
,然后点击“确定”按钮添加该引用。 - 添加引用后,可以通过添加对
using Microsoft.Office.Interop.Excel;
的命名空间声明来使用相关的类和方法。 - 对程序集的使用
在程序调用时,添加using Excel = Microsoft.Office.Interop.Excel;
创建一个ExcelUtils类,
public static void CreateControlsFromExcel(string filePath, Control container)
{...
}
在里面读取文件
Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//读取文件
Excel.Workbook workbook = excelApp.Workbooks.Open(filePath);
//选择表格1
Excel.Worksheet worksheet = workbook.Sheets[1];Excel.Range range = worksheet.UsedRange;
int rowCount = range.Rows.Count;// 获取列索引
int textColumnIndex = GetColumnIndexByHeader("Text", range); //控件名称
int controlColumnIndex = GetColumnIndexByHeader("Controls", range);
int nameColumnIndex =GetColumnIndexByHeader("Name", range);
为了让创建的页面美观,定义坐标轴,如果创建的控件也比较多,可以调用Location去定位坐标
int startX = 50; // 起始 X 坐标
int startY = 50; // 起始 Y 坐标
int spacingX = 10; // 控件之间的水平间距
int spacingY = 10; // 控件之间的垂直间距
int currentX = startX; // 当前 X 坐标
int currentY = startY; // 当前 Y 坐标
默认起始的 X 和 Y 坐标为 (50, 50)
,水平间距和垂直间距分别为 10
。每次添加一个控件后,我们通过比较当前坐标加上控件的宽度是否超过容器的宽度,来判断是否需要换行。如果超过了容器的宽度,则将 X 坐标重置为起始 X 坐标,同时将 Y 坐标增加一个控件的高度和垂直间距。这样就可以实现控件自动换行的效果。
主体代码:
for (int row = 2; row <= rowCount; row++)
{
string name = Convert.ToString((range.Cells[row, nameColumnIndex] as Excel.Range)?.Value2);
string controlType = Convert.ToString((range.Cells[row, controlColumnIndex] as Excel.Range)?.Value2);
string text = Convert.ToString((range.Cells[row, textColumnIndex] as Excel.Range)?.Value2);
Control control;
switch (controlType)
{
case "Button":
control = new Button();
break;
case "TextBox":
control = new TextBox();
break;
// 添加其他控件类型的处理逻辑
default:
control = null;
break;
}
if (control != null)
{
//从表格读取的name列与对应的Text
control.Name = name;
control.Text = text;
container.Controls.Add(control);
control.Location = new Point(currentX, currentY);
// 更新当前坐标
currentX += control.Width + spacingX;
// 判断是否换行
if (currentX + control.Width > container.Width)
{
currentX = startX;
currentY += control.Height + spacingY;
}
}
}
最后,关闭连接
workbook.Close(false);
excelApp.Quit();}
上面代码中,增加一个方法,查找表头为指定字符串header的列索引。该方法通过循环每个列来查找表头,并与指定的字符串进行比较。一旦找到了匹配的表头,就返回该列的索引值。如果未找到匹配的表头,则返回-1
private static int GetColumnIndexByHeader(string header, Excel.Range range)
{
int columnIndex = -1;
int columnCount = range.Columns.Count;
for (int column = 1; column <= columnCount; column++)
{
string cellValue = Convert.ToString((range.Cells[1, column] as Excel.Range)?.Value2);
if (cellValue == header)
{
columnIndex = column;
break;
}
}
return columnIndex;
}
这样类方法就完成了,这时候需要在主窗体调用:
public Form1()
{
InitializeComponent();
// 假设您有一个名为 "form" 的窗体对象,表格位置根据自己位置修改
string filePath = "C:\\Users\\Desktop\\controlsAndName.xlsx";
ExcelUtils.CreateControlsFromExcel(filePath, this);
}
最终,运行结果如图所示:
以上不对的地方请指正,需要源代码的可以私信我,文档供大家参考与学习。