export datagrid's data to excel


 

ExpandedBlockStart.gif 代码
     protected   void  export_Click( object  sender, EventArgs e)
    {
        BusinessLayer bl 
=   new  BusinessLayer();
        DataTable dt 
=  bl.GetAllUnpaidOrders().Tables[ 0 ];
        
byte [] dataBuffer  =   GetTimesheetReporteStream(dt);
        
string  fileName  =   " TimesheetDefaulterReport.csv " ;
        
this .Response.Clear();
        
this .Response.Charset  =   " GB2312 " ;
        
this .Response.ContentEncoding  =  System.Text.Encoding.GetEncoding( " GB2312 " );
        
this .Response.ContentType  =   " application/vnd.ms-excel " //  "application/octet-stream";  // "application/ms-excel";  // vnd.
         this .Response.AppendHeader( " content-disposition " " attachment; filename= "   +  HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).Replace( " + " " %20 " ));
        
this .Response.OutputStream.Write(dataBuffer,  0 , dataBuffer.Length);
        
this .Response.Flush();
        
this .Response.End();
    }
    
public   byte [] GetTimesheetReporteStream(DataTable dt)
    {
        StringBuilder builder 
=   new  StringBuilder();
        
foreach  (DataColumn column  in  dt.Columns)
        {
            builder.Append(column.ColumnName 
+   " , " );
        }
        builder.Append(Environment.NewLine);
        
foreach  (DataRow row  in  dt.Rows)
        {
            
for  ( int  i  =   0 ; i  <  dt.Columns.Count; i ++ )
            {
                builder.Append(ConvertToCsvCell(row[i].ToString()) 
+   " , " );
            }
            builder.Append(Environment.NewLine);
        }
        
byte [] dataBuffer  =  Encoding.Default.GetBytes(builder.ToString());
        
return  dataBuffer;
    }
    
public   static   string  ConvertToCsvCell( string  cell)
    {
        cell 
=  SBCToDBC(cell);
        
if  (cell.Contains( @" , " ||  cell.Contains( " \ "" ))
        {
            cell 
=  cell.Replace( " \ "" " \ " \ "" );
            cell  =   " \ ""  + cell +  " \ "" ;
        }
        
else
        {
            
if  (cell.Contains( " \r " ||  cell.Contains( " \n " ))
            {
                cell 
=   " \ ""  + cell +  " \ "" ;
            }
        }
        
return  cell;
    }
    
public   static   string  SBCToDBC( string  input)
    {
        
char [] cc  =  input.ToCharArray();
        
for  ( int  i  =   0 ; i  <  cc.Length; i ++ )
        {
            
if  (cc[i]  ==   8220   ||  cc[i]  ==   8221 )
            {
                cc[i] 
=  ( char ) 34 ;
                
continue ;
            }
            
if  (cc[i]  ==   8216   ||  cc[i]  ==   8217 )
            {
                cc[i] 
=  ( char ) 39 ;
                
continue ;
            }
            
if  (cc[i]  ==   12288 )
            {
                cc[i] 
=  ( char ) 32 ;
                
continue ;
            }
            
if  (cc[i]  >   65280   &&  cc[i]  <   65375 )
            {
                cc[i] 
=  ( char )(cc[i]  -   65248 );
            }

        }
        
return   new   string (cc);
    }

 

 

转载于:https://www.cnblogs.com/xpwilson/archive/2010/01/26/1656815.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在wpf中,可以使用Microsoft.Office.Interop.Excel库来实现将DataGrid中的数据导出为Excel文件的功能。首先,我们需要创建一个新的Excel应用程序实例,并且添加一个工作簿和工作表。接着,我们可以利用DataGrid中的数据来填充Excel表格,并设置单元格的格式和样式。最后,我们需要保存这个工作簿为一个Excel文件,并关闭Excel应用程序实例。以下是一个简单的示例代码: ```csharp private void ExportToExcel(DataGrid dataGrid, string filePath) { Microsoft.Office.Interop.Excel._Application excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook workbook = excelApp.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel._Worksheet worksheet = null; try { // 获取DataGrid中的列数和行数 int columnsCount = dataGrid.Columns.Count; int rowsCount = dataGrid.Items.Count; worksheet = workbook.ActiveSheet; // 将DataGrid中的数据填充到Excel表格中 for (int i = 0; i < columnsCount; i++) { worksheet.Cells[1, i + 1] = dataGrid.Columns[i].Header; for (int j = 0; j < rowsCount; j++) { worksheet.Cells[j + 2, i + 1] = ((TextBlock)dataGrid.Columns[i].GetCellContent(dataGrid.Items[j])).Text; } } // 保存工作簿为Excel文件 workbook.SaveAs(filePath); } catch (Exception ex) { // 处理异常 } finally { // 关闭Excel应用程序实例 excelApp.Quit(); workbook = null; worksheet = null; } } ``` 需要注意的是,由于这里使用了Interop库,所以需要确保目标机器上安装了Excel应用程序。另外,导出数据量较大时需要注意性能问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值