excel添加列下拉框票价_用C#创建Excel,添加下拉框列,并动态给下拉框赋值

这篇博客介绍了如何使用C#创建Excel文件,并在其中添加下拉框列,动态赋值。通过示例代码展示了如何设置验证规则、填充数据以及隐藏辅助列,最终生成带有下拉选项的Excel表格。
摘要由CSDN通过智能技术生成

public void simpleButton1_Click(object sender, EventArgs e)

{

KillProcess("Excel");//首先杀死进程

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;

Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

Microsoft.Office.Interop.Excel.Range range = worksheet.Cells;

//Excel.Range ran = range.get_Range("A5", "A5");

//ran.AddComment("批注");

Excel.Range ran1 = worksheet.Columns.get_Range("H:H", Type.Missing);//或者.get_Range("D1", "D1").get_Resize(50,1);//获取一整列D,行数为50

ran1.Validation.Delete();//有无都可以

ran1.Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop, Type.Missing, "1,2,3", Type.Missing);

//填充值

worksheet.Cells[1, 1] = "用户代码";

worksheet.Cells[1, 2] = "用户名称";

worksheet.Cells[1, 3] = "绩效积分项";

worksheet.Cells[1, 4] = "绩效积分时间";

worksheet.Cells[1, 5] = "描述";

//先把下拉框值赋值在Z列上

//杀死进程

private void KillProcess(string processName)

{

//获得进程对象,以用来操作

System.Diagnostics.Process myproc = new System.Diagnostics.Process();

//得到所有打开的进程

try

{

//获得需要杀死的进程名

foreach (Process thisproc in Process.GetProcessesByName(processName))

{

//立即杀死进程

thisproc.Kill();

}

}

catch (Exception Exc)

{

throw new Exception("", Exc);

}

}

worksheet.Cells[1, 26] = "a";

worksheet.Cells[2, 26] = "b";

worksheet.Cells[3, 26] = "c";

//在把Z列值赋值到下拉框列上

worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[10000, 3]).Validation.Add(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop, Type.Missing, "=$Z$1:$Z$5", Type.Missing);

//最后隐藏Z列

Excel.Range ranger = (Excel.Range)worksheet.Columns["Z:Z", System.Type.Missing];

ranger.Hidden = true;

//将文件保存到D:\\tt.xls

workbook.SaveCopyAs("D:\\111.xlsx");

workbook.Saved = true;

xlApp.Visible = true;//打开Excel文件

// ran1.Validation.Modify(Excel.XlDVType.xlValidateList, Excel.XlDVAlertStyle.xlValidAlertStop, Type.Missing, "4,3,2", Type.Missing);//用此法方(Modify)可以对前面的下拉框进行修改,

}

//当使用模板时,用下面的代码替换上面的几行

object missing = Missing.Value;

KillProcess("Excel");//首先杀死进程

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;

//Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

//Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

//Microsoft.Office.Interop.Excel.Range range = worksheet.Cells;

//打开模板文件,得到WorkBook对象

Microsoft.Office.Interop.Excel.Workbook workbook = xlApp.Workbooks.Open(HttpContext.Request.PhysicalApplicationPath + "Templates\\"+"IPalTempFile\\PDU资产模板1.xls", missing, missing, missing, missing, missing,

missing,missing,missing,missing,missing,missing,missing);

//得到WorkSheet对象

Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get_Item(1);

Microsoft.Office.Interop.Excel.Range range = worksheet.Cells;

//worksheet.Name = "过程资产平台导入建议模板";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值