C# 开发和使用中的33个技巧(1)

发表于:2009-12-04 11:07

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

  1)

  private void button1_Click(object sender, System.EventArgs e)
  {
  int row_index, col_index;
  row_index = 1;
  col_index = 1;
  Excel.ApplicationClass excel = new Excel.ApplicationClass();
  excel.Workbooks.Add(true);
  DataTable dt = ds.Tables["table"];
  foreach(DataColumn dcHeader in dt.Columns) excel.Cells[row_index, col_index++] = dcHeader.ColumnName;
  foreach(DataRow dr in dt.Rows)
  {
  col_index = 0;
  foreach(DataColumn dc in dt.Columns)
  {
  excel.Cells[row_index+1, col_index+1] = dr[dc];
  col_index++;
  }
  row_index++;
  }
  excel.Visible = true;
  }
  private void Form1_Load(object sender, System.EventArgs e)
  {
  SqlConnection conn = new SqlConnection("server=tao; uid=sa; pwd=; database=pubs");
  conn.Open();
  SqlDataAdapter da = new SqlDataAdapter("select * from authors", conn);
  ds = new DataSet();
  da.Fill(ds, "table");
  dataGrid1.DataSource = ds;
  dataGrid1.DataMember = "table";
  }

  2)dataGrid1.TableStyles[0].GridColumnStyles[index].HeaderText; //index可以从0~dataGrid1.TableStyles[0].GridColumnStyles.Count遍历。

  3)Excel.Range range;

  range=worksheet.get_Range(worksheet.Cells[1,1],xSt.Cells[ds.Tables[0].Rows.Count+1,ds.Tables[0].Columns.Count]);

  range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);

  range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

  range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;

  range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;

  4)range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter⑤ worksheet.PageSetup.PrintTitleRows = "$1:$1";

  5)worksheet.PageSetup.CenterFooter = "第&P页 / 共&N页";

  22.当把DataGrid的Cell内容赋值到Excel的过程中想在DataGrid的CaptionText上显示进度,但不显示。WHY?

  ... dataGrid1.CaptionText = "正在导出:" + (row + 1) + "/" + row_cnt;
  System.Windows.Forms.Application.DoEvents();
  ...

  处理当前在消息队列中的所有Windows消息。

  当运行Windows窗体时,它将创建新窗体,然后该窗体等待处理事件。该窗体在每次处理事件时,均将处理与该事件关联的所有代码。所有其他事件在队列中等待。在代码处理事件时,应用程序并不响应。如果在代码中调用DoEvents,则应用程序可以处理其他事件。

  如果从代码中移除DoEvents,那么在按钮的单机事件处理程序执行结束以前,窗体不会重新绘制。通常在循环中使用该方法来处理消息。

33/3<123
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号