提问人:Rishmitha Tatineni 提问时间:10/26/2023 最后编辑:marc_sRishmitha Tatineni 更新时间:10/26/2023 访问量:28
如何从Excel工作表的下拉列表中获取所选项目以在 ASP.NET 的网格视图中显示
How to get selected item from drop down in excel sheet to show in grid view in ASP.NET
问:
我正在编写一个网页,该网页导入一个 Excel 文件并在网格视图中显示其内容。我的 Excel 工作表有 3 个下拉列表框。我希望在下拉列表中选择的值显示在网格视图中。我有一些合并的单元格,我希望它们在网格视图中保持合并
protected DataTable YourExcelFileProcessingMethod(Stream excelFileStream)
{
ExcelPackage.LicenseContext = LicenseContext.Commercial;
using (ExcelPackage package = new ExcelPackage(excelFileStream))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
DataTable dt = new DataTable();
foreach (var firstRowCell in worksheet.Cells[1, 1, 1, worksheet.Dimension.End.Column])
{
dt.Columns.Add(firstRowCell.Text);
}
for (int row = 2; row <= worksheet.Dimension.End.Row; row++)
{
var wsRow = worksheet.Cells[row, 1, row, worksheet.Dimension.End.Column];
var newRow = dt.NewRow();
foreach (var cell in wsRow)
{
newRow[cell.Start.Column - 1] = cell.Text;
}
dt.Rows.Add(newRow);
}
return dt;
}
}
protected void btnImportData_Click(object sender, EventArgs e)
{
if (FileUploadAttendance.HasFile)
{
HttpPostedFile file = FileUploadAttendance.PostedFile;
if (file.FileName.EndsWith(".xls") || file.FileName.EndsWith(".xlsx"))
{
DataTable dt = YourExcelFileProcessingMethod(file.InputStream);
GridAttendance.DataSource = dt;
GridAttendance.DataBind();
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "FailureAlert", "alert('Only Excel files are accepted');", true);
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "FailureAlert", "alert('Please Select a file to import');", true);
}
}
这是从网格视图获取数据,但未显示下拉列表的值。对于合并的单元格,它显示两个不同的单元格。
答: 暂无答案
评论