有关于c#操作excel的问题 请各位大虾指教

我用c#写了个程序是将DataGrid中的数据写入excel的
写入成功.但是现在我想在写入的excel中加一行标题
也就是如何用程序在excel中设置显示格式(合并单元格,设置字体)

public void CM_WriteDSToExcel( string dtime ) {
            string strFile = "";
            string path = "";
            OleDbDataAdapter adapter = new OleDbDataAdapter( "SELECT Company, Software, [Key], [Percent], [Date Time] FROM [Key] WHERE ([Date Time] = '" + dtime + "')", conn );
            DataSet ds = new DataSet();

            adapter.Fill( ds );

            DataTable dt = ds.Tables[0];

            //文件信息设置
            strFile = strFile + "Key   ";
            strFile = strFile + DateTime.Now.ToString( "yyyy-MM-dd hh-mm-ss" );
            strFile = strFile + ".xls";
            path = Application.StartupPath + "//" + strFile;

            System.IO.FileStream fs = new FileStream( path, System.IO.FileMode.Create, System.IO.FileAccess.Write );
            StreamWriter sw = new StreamWriter( fs, new System.Text.UnicodeEncoding() );
            //画表头
            for ( int i = 0; i < dt.Columns.Count; i++ ) {
                sw.Write( dt.Columns[i].ColumnName );
                sw.Write( "/t" );
            }
            sw.WriteLine( "" );
            //画表体
            for ( int i = 0; i < dt.Rows.Count; i++ ) {
                sw.Write( dt.Rows[i]["Company"].ToString() );
                sw.Write( "/t" );
                sw.Write( dt.Rows[i]["Software"].ToString() );
                sw.Write( "/t" );
                sw.Write( dt.Rows[i]["Key"].ToString() );
                sw.Write( "/t" );
                sw.Write( dt.Rows[i]["Percent"].ToString() );
                sw.Write( "/t" );
                sw.Write( dt.Rows[i]["Date Time"].ToString() );
                sw.Write( "/t" );
                sw.WriteLine( "" );
            }
            sw.Flush();
            sw.Close();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值