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,那么在按钮的单机事件处理程序执行结束以前,窗体不会重新绘制。通常在循环中使用该方法来处理消息。