创建固定表头、表格体滚动的DataGrid(c#版) 转自CX12CN

本文提供了一段C#代码,用于创建一个DataGrid,具有固定表头和可滚动的表格体,适用于.NET开发。示例中详细展示了如何在ASP.NET页面上设置和填充DataGrid。
摘要由CSDN通过智能技术生成
在使用DataGrid时,有时候需要表头表头固定、表格体的内容可以滚动,下面的代码就是实现这个功能的代码。

ShowFixedHeader.aspx内容:

<% @ Page language = " c# "  Codebehind = " ShowFixedHeader.aspx.cs "  AutoEventWireup = " false "  Inherits = " bShowFixedHeader.ShowFixedHeader "   %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.0 Transitional//EN " >
< HTML >
 
< HEAD >
  
< title id = " lucky_elove " ></ title >
  
< meta name = " GENERATOR "  content = " Microsoft Visual Studio.NET 7.0 " >
  
< meta name = " CODE_LANGUAGE "  content = " Visual Basic 7.0 " >
  
< meta name = " vs_defaultClientScript "  content = " JavaScript " >
  
< meta name = " vs_targetSchema "  content = " http://schemas.microsoft.com/intellisense/ie5 " >
 
</ HEAD >
 
< body MS_POSITIONING = " GridLayout "  leftmargin = " 0 " >
  
< table align = " center " >
   
< tr >
    
< td >
     
< form id = " Form1 "  method = " post "  runat = " server " >
      
< TABLE id = " Table1 "  runat = " server "  cellSpacing = " 1 "  cellPadding = " 2 "  width = " 736 "  border = " 1 "
       bgcolor
= " #cc6633 "  bordercolor = " #cc9966 "  style = " FONT-SIZE:9pt;WIDTH:736px;BORDER-BOTTOM:0px;HEIGHT:46px " >
       
< TR align = " center " >
        
< TD colspan = " 2 "
可以使用 Microsoft.Office.Interop.Excel 库来实现将 DataGrid 中的数据写入到 Excel 中对应的表头行列中。 首先需要引用 Microsoft.Office.Interop.Excel 库,然后可以按照以下步骤进行操作: 1. 创建 Excel 应用程序对象和工作簿对象 ``` Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Add(); ``` 2. 获取工作簿中的第一个工作表对象 ``` Excel.Worksheet worksheet = workbook.Worksheets[1]; ``` 3. 将 DataGrid 中的表头写入到 Excel 的第一行中 ``` for (int i = 0; i < dataGrid.Columns.Count; i++) { worksheet.Cells[1, i + 1] = dataGrid.Columns[i].Header.ToString(); } ``` 4. 将 DataGrid 中的数据写入到 Excel 中对应的行列中 ``` for (int i = 0; i < dataGrid.Items.Count; i++) { for (int j = 0; j < dataGrid.Columns.Count; j++) { string value = (dataGrid.Items[i] as DataRowView)[j].ToString(); worksheet.Cells[i + 2, j + 1] = value; } } ``` 5. 保存 Excel 文件并关闭应用程序对象 ``` workbook.SaveAs("output.xlsx"); excelApp.Quit(); ``` 完整代码示例: ``` using System.Data; using Excel = Microsoft.Office.Interop.Excel; private void ExportToExcel(DataGrid dataGrid) { Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Add(); Excel.Worksheet worksheet = workbook.Worksheets[1]; // 写入表头 for (int i = 0; i < dataGrid.Columns.Count; i++) { worksheet.Cells[1, i + 1] = dataGrid.Columns[i].Header.ToString(); } // 写入数据 for (int i = 0; i < dataGrid.Items.Count; i++) { for (int j = 0; j < dataGrid.Columns.Count; j++) { string value = (dataGrid.Items[i] as DataRowView)[j].ToString(); worksheet.Cells[i + 2, j + 1] = value; } } // 保存并关闭 Excel 应用程序 workbook.SaveAs("output.xlsx"); excelApp.Quit(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值