C#导数带格式EXCEL

 

代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  Excel = Microsoft.Office.Interop.Excel;
using  System.Reflection;

namespace  WindowsApplication2
{
    
public   partial   class  Form1 : Form
    {
        
public  Form1()
        {
            InitializeComponent();
        }

        
private   void  button1_Click( object  sender, EventArgs e)
        {

            Excel.Application app 
=   new  Microsoft.Office.Interop.Excel.Application();
            
if  (app  ==   null )
                
return ;
            app.Visible 
=   false ;
            app.UserControl 
=   true ;
            Excel.Workbooks workbooks 
=  app.Workbooks;
            Excel._Workbook workbook 
=  workbooks.Add( @" C:\Model.xls " ); //
            Excel.Sheets sheets  =  workbook.Worksheets;
            Excel._Worksheet worksheet 
=  (Excel._Worksheet)sheets.get_Item( 1 );
            
if  (worksheet  ==   null )
                
return ;
            DataSet ds 
=  SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringLocalTransaction,  " select * from [Sheet1$] " );
            DataSet ds2 
=  SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringLocalTransaction,  " select 支行,sum(交易金额) from [sheet1$] group by 支行 " );
            
int  j  =   0 ;
            
int  i  =   0 ;
            
double  total  =   0 ;
            
if  (ds2.Tables[ 0 ].Rows.Count  >   0 )
            {
                DataRow []ds3;
                
for (i = 0 ;i < ds2.Tables[ 0 ].Rows.Count;i ++ )
                {
                    ds3
= ds.Tables[ 0 ].Select( " 支行=' " + ds2.Tables[ 0 ].Rows[i][ 0 ] + " ' " );
                    
for  ( int  k = 0 ; k  <  ds3.Length ; k ++ )
                    {
                        worksheet.Cells[
2   +  j,  1 =  ds3[k][ " 商户编号 " ].ToString();
                        worksheet.Cells[
2   +  j,  2 =  ds3[k][ " 交易金额 " ].ToString();
                        worksheet.Cells[
2   +  j,  3 =  ds3[k][ " 支行 " ].ToString();
                        worksheet.Cells[
2   +  j,  4 =  ds3[k][ " 奖励标准 " ].ToString();
                        worksheet.Cells[
2   +  j,  5 =  ds3[k][ " 备注 " ].ToString();
                        j
++ ;
                    }
                    
// 汇总信息
                    worksheet.Cells[ 2   +  j,  2 =  ds2.Tables[ 0 ].Rows[i][ 1 ].ToString() ;
                    total 
+=  Convert.ToDouble(ds2.Tables[ 0 ].Rows[i][ 1 ]);
                    worksheet.Cells[
2   +  j,  3 =  ds2.Tables[ 0 ].Rows[i][ 0 ].ToString()  +   "  汇总 " ;
                    
// 下一次填充开始位置
                    j ++ ;
                }
                worksheet.Cells[
2   +  j,  2 =  total;
                worksheet.Cells[
2   +  j,  3 =   " 总计 " ;
                
            }
            workbook.SaveAs(
" C:\\aa.xls " , Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            app.Quit();
        }


    }
}

 

转载于:https://www.cnblogs.com/ChuttySonic/archive/2009/11/28/1612421.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值