C# winfrom中要把窗体改成XP窗体的样式!!
C# winfrom中导出DataGrid表生成csv,xls文件!以下这个不要!
// public virtual void ExcelMethod()
// {
// DataTable DT = (DataTable)this.dataGrid1.DataSource;
// int intRows = DT.Rows.Count;
// if(intRows == 0)
// {
// return;
// }
//
// this.Cursor = Cursors.WaitCursor ;
//
// try
// {
// int intCols = DT.Columns.Count;
// Excel.ApplicationClass xlsApp = new Excel.ApplicationClass ();
// xlsApp.Workbooks.Add("");
// DataGridTableStyle dts1 = this.dataGrid1.TableStyles[0];
//
// for(int j=0; j<intCols; j++)
// {
// xlsApp.Cells[1,j+1] = dts1.GridColumnStyles[j].HeaderText.ToString ();
// }
//
// for(int i=0; i<intRows; i++)
// {
// for(int j=0; j<intCols; j++)
// {
// xlsApp.Cells[i+2,j+1] = DT.Rows[i][j].ToString ();
// }
// }
// xlsApp.Visible = true;
// }
// finally
// {
// this.Cursor = Cursors.Default ;
// }
// }
C# winfrom中导出DataGrid表生成csv,xls文件!以下这个不要!
// public virtual void ExcelMethod()
// {
// DataTable DT = (DataTable)this.dataGrid1.DataSource;
// int intRows = DT.Rows.Count;
// if(intRows == 0)
// {
// return;
// }
//
// this.Cursor = Cursors.WaitCursor ;
//
// try
// {
// int intCols = DT.Columns.Count;
// Excel.ApplicationClass xlsApp = new Excel.ApplicationClass ();
// xlsApp.Workbooks.Add("");
// DataGridTableStyle dts1 = this.dataGrid1.TableStyles[0];
//
// for(int j=0; j<intCols; j++)
// {
// xlsApp.Cells[1,j+1] = dts1.GridColumnStyles[j].HeaderText.ToString ();
// }
//
// for(int i=0; i<intRows; i++)
// {
// for(int j=0; j<intCols; j++)
// {
// xlsApp.Cells[i+2,j+1] = DT.Rows[i][j].ToString ();
// }
// }
// xlsApp.Visible = true;
// }
// finally
// {
// this.Cursor = Cursors.Default ;
// }
// }
2、你嵌入个Excel表格再把datagrid表里的内容生成Excel文件,再导出不就生成了么
我發那個文件給你了,你試一下
那個文件是調用系統桌面主題樣式的,桌面主題改變,程序的樣式也改變
難道C# Winform不支持?關注......
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection cn=new SqlConnection("server=127.0.0.1;uid=sa;pwd=sa;database=pubs");
SqlDataAdapter da=new SqlDataAdapter("select * from jobs",cn);
DataSet ds=new DataSet();
da.Fill(ds,"jobs");
this.DataGrid1.DataSource=ds.Tables["jobs"].DefaultView;
this.DataGrid1.DataBind();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
}
就OK了