您的位置:首页 > 编程语言 > C#

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

2006-02-02 15:14 567 查看
我用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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: