有关于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();
}
写入成功.但是现在我想在写入的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();
}
相关文章推荐
- 关于C#中,采用excel com组件操作excel的问题
- C#使用OleDb操作excel数据源关于Select字段有空格问题
- 关于 c# 操作 Excel文件中遇到的保存出错问题
- 关于C#调用Excel的资源占用问题
- 关于C#打操作EXCEL
- 关于C#操作EXCEL,生成图表的全面应用之一(利用Microsoft.Office.Interop.Excel)【转载】
- C#操作Excel,调用ApplicationClass.Quit()关闭Excel时,发生异常:Microsoft Office Word 遇到问题需要关闭
- 关于在ASP.NET中以DCOM方式操作Excel的几个问题
- 关于C#对EXCEL的一些简单操作
- 解决C#使用Microsoft.Office.Interop.Excel操作Excel后进程一直存在的问题
- 最近写一个C#关于U盘的操作,在弹出U盘是遇到了些问题,C#调WINdoWs底层的东西太麻烦了,看样子要看看VC或C了
- window xp安装不了的问题,请各位大虾指教
- 关于OpenGL ES20显示YUV数据在不同手机要使用不同坐标系的问题,求教各位大虾
- 关于C#操作EXCEL,生成图表的全面应用之一(利用Microsoft.Office.Interop.Excel)
- C#:一些关于Excel的操作(设置单元格格式、合并单元格)
- C#关于Excel的简单操作
- 如何锻炼分析问题的方法?请各位大虾指教~~
- 关于c#执行mysql操作,传递参数的问题
- 关于EXCEL操作的问题
- C# 操作数据导出到Excel遇到的一个小问题