C# 操作 Excel 如何设置格式

① 怎样把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(); 

  ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值