asp.net 到出Excel 示例

  1. private DataSet bangding()
  2.     {
  3.         using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=pubs"))
  4.         {
  5.             conn.Open();
  6.             string sql = "select job_id as 编号,job_desc as 排序,min_lvl as 最小, max_lvl as 最大 from jobs";
  7.             SqlDataAdapter dar = new SqlDataAdapter(sql, conn);
  8.             DataSet ds = new DataSet();
  9.             dar.Fill(ds);
  10.             this.GridView1.DataSource = ds;
  11.             this.GridView1.DataBind();
  12.             return ds;
  13.         }
  14.     }
  15.     private int num()
  16.     {
  17.         int num=0;
  18.         for (int i = 0; i < bangding().Tables[0].Rows.Count;i++ )
  19.         {
  20.             num += Convert.ToInt32(bangding().Tables[0].Rows[i][3].ToString());
  21.         }
  22.         return num;
  23.     }
  24.     protected void Button1_Click(object sender, EventArgs e)
  25.     {
  26.         CreateExcel(bangding(), "D:/aa.xls""标题""合计:" + num());
  27.     }
  28.     public void CreateExcel(DataSet dv, string path, string title1, string heji)
  29.     {
  30.         try
  31.         {
  32.             FileInfo fi = new FileInfo(path);
  33.             fi.Delete();
  34.         }
  35.         catch
  36.         {
  37.         }
  38.         Microsoft.Office.Interop.Excel.Application excel = new Application();
  39.         object oMissing = System.Reflection.Missing.Value;
  40.         Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Add(oMissing);
  41.         int rowIndex = 3;
  42.         int colIndex = 0;
  43.         excel.Cells[1, 1] = title1;
  44.         foreach (DataColumn dc in dv.Tables[0].Columns)
  45.         {
  46.             colIndex++;
  47.             excel.Cells[rowIndex, colIndex] = dc.ColumnName;
  48.             ((Microsoft.Office.Interop.Excel.Range)excel.Cells[rowIndex, colIndex]).ColumnWidth = 18.8;
  49.         }
  50.         foreach (DataRow row in dv.Tables[0].Rows)
  51.         {
  52.             rowIndex++;
  53.             colIndex = 0;
  54.             foreach (DataColumn dc in dv.Tables[0].Columns)
  55.             {
  56.                 colIndex++;
  57.                 excel.Cells[rowIndex, colIndex] = row[dc.ColumnName].ToString();
  58.                 ((Microsoft.Office.Interop.Excel.Range)excel.Cells[rowIndex, colIndex]).ColumnWidth = 18.8;
  59.             }
  60.         }
  61.         excel.Cells[rowIndex + 1, colIndex] = heji;
  62.         Microsoft.Office.Interop.Excel.Sheets sheets = book.Worksheets;
  63.         Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(1);
  64.         Microsoft.Office.Interop.Excel.Range range;
  65.         range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, colIndex]);
  66.         range.MergeCells = true;//合并   
  67.         range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中   
  68.         range.Font.Bold = true;
  69.         range.Font.Size = 14;
  70.         book.Saved = true;
  71.         excel.UserControl = false;
  72.         worksheet.SaveAs(path, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
  73.         excel.Quit();
  74.         oMissing = null;
  75.         sheets = null;
  76.         worksheet = null;
  77.         book = null;
  78.         excel = null;
  79.         GC.Collect();
  80.     }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值