C# 操作 Excel 如何設定格式

來源:互聯網
上載者:User

① 怎樣把DataGrid的資料匯出到Excel以供列印?

  ② 之前已經為DataGrid設定了TableStyle,即自訂了欄位標題和要顯示的列,如果想以自訂的視圖匯出資料該怎麼辦?

  ③ 把資料匯出到Excel後,怎樣為它設定邊框啊?

  ④ 怎樣使從DataGrid匯出到Excel的某個列置中對齊?

  ⑤ 資料從DataGrid匯出到Excel後,怎樣使標題列在列印時出現在每一頁?

  ⑥ DataGrid資料匯出到Excel後列印時每一頁顯示’當前頁/共幾頁’,怎樣實現?

  ①

  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"; 

  }

  ②dataGrid1.TableStyles[0].GridColumnStyles[index].HeaderText; //index可以從0~dataGrid1.TableStyles[0].GridColumnStyles.Count遍曆。

  ③ 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; 

  ④ range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter

  ⑤ worksheet.PageSetup.PrintTitleRows = "$1:$1"; 

  ⑥ worksheet.PageSetup.CenterFooter = "第&P頁 / 共&N頁"; 

  22.當把DataGrid的Cell內容賦值到Excel的過程中想在DataGrid的CaptionText上顯示進度,但不顯示。WHY?

  ...

  dataGrid1.CaptionText = "正在匯出:" + (row + 1) + "/" + row_cnt; 

  System.Windows.Forms.Application.DoEvents(); 

  ...

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.